split random COFF asmprinter state out to X86COFFMachineModuleInfo.h.

Make dllexport directives come out in determinstic order.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@82381 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2009-09-20 06:45:52 +00:00
parent b13efdafec
commit 67c6b6ee71
6 changed files with 238 additions and 137 deletions

View File

@ -19,6 +19,7 @@
#include "X86MCInstLower.h" #include "X86MCInstLower.h"
#include "X86.h" #include "X86.h"
#include "X86COFF.h" #include "X86COFF.h"
#include "X86COFFMachineModuleInfo.h"
#include "X86MachineFunctionInfo.h" #include "X86MachineFunctionInfo.h"
#include "X86TargetMachine.h" #include "X86TargetMachine.h"
#include "llvm/CallingConv.h" #include "llvm/CallingConv.h"
@ -58,101 +59,15 @@ void X86ATTAsmPrinter::PrintPICBaseSymbol() const {
X86MCInstLower(OutContext, 0, *AP).GetPICBaseSymbol()->print(O, MAI); X86MCInstLower(OutContext, 0, *AP).GetPICBaseSymbol()->print(O, MAI);
} }
static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
const TargetData *TD) {
X86MachineFunctionInfo Info;
uint64_t Size = 0;
switch (F->getCallingConv()) {
case CallingConv::X86_StdCall:
Info.setDecorationStyle(StdCall);
break;
case CallingConv::X86_FastCall:
Info.setDecorationStyle(FastCall);
break;
default:
return Info;
}
unsigned argNum = 1;
for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
AI != AE; ++AI, ++argNum) {
const Type* Ty = AI->getType();
// 'Dereference' type in case of byval parameter attribute
if (F->paramHasAttr(argNum, Attribute::ByVal))
Ty = cast<PointerType>(Ty)->getElementType();
// Size should be aligned to DWORD boundary
Size += ((TD->getTypeAllocSize(Ty) + 3)/4)*4;
}
// We're not supporting tooooo huge arguments :)
Info.setBytesToPopOnReturn((unsigned int)Size);
return Info;
}
/// DecorateCygMingName - Query FunctionInfoMap and use this information for
/// various name decorations for Cygwin and MingW.
void X86ATTAsmPrinter::DecorateCygMingName(SmallVectorImpl<char> &Name,
const GlobalValue *GV) {
assert(Subtarget->isTargetCygMing() && "This is only for cygwin and mingw");
const Function *F = dyn_cast<Function>(GV);
if (!F) return;
// Save function name for later type emission.
if (F->isDeclaration())
CygMingStubs.insert(StringRef(Name.data(), Name.size()));
// We don't want to decorate non-stdcall or non-fastcall functions right now
CallingConv::ID CC = F->getCallingConv();
if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
return;
const X86MachineFunctionInfo *Info;
FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
if (info_item == FunctionInfoMap.end()) {
// Calculate apropriate function info and populate map
FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
Info = &FunctionInfoMap[F];
} else {
Info = &info_item->second;
}
if (Info->getDecorationStyle() == None) return;
const FunctionType *FT = F->getFunctionType();
// "Pure" variadic functions do not receive @0 suffix.
if (!FT->isVarArg() || FT->getNumParams() == 0 ||
(FT->getNumParams() == 1 && F->hasStructRetAttr()))
raw_svector_ostream(Name) << '@' << Info->getBytesToPopOnReturn();
if (Info->getDecorationStyle() == FastCall) {
if (Name[0] == '_')
Name[0] = '@';
else
Name.insert(Name.begin(), '@');
}
}
/// DecorateCygMingName - Query FunctionInfoMap and use this information for
/// various name decorations for Cygwin and MingW.
void X86ATTAsmPrinter::DecorateCygMingName(std::string &Name,
const GlobalValue *GV) {
SmallString<128> NameStr(Name.begin(), Name.end());
DecorateCygMingName(NameStr, GV);
Name.assign(NameStr.begin(), NameStr.end());
}
void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) { void X86ATTAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
unsigned FnAlign = MF.getAlignment(); unsigned FnAlign = MF.getAlignment();
const Function *F = MF.getFunction(); const Function *F = MF.getFunction();
if (Subtarget->isTargetCygMing()) if (Subtarget->isTargetCygMing()) {
DecorateCygMingName(CurrentFnName, F); X86COFFMachineModuleInfo &COFFMMI =
MMI->getObjFileInfo<X86COFFMachineModuleInfo>();
COFFMMI.DecorateCygMingName(CurrentFnName, F, *TM.getTargetData());
}
OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM)); OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
EmitAlignment(FnAlign, F); EmitAlignment(FnAlign, F);
@ -220,17 +135,19 @@ bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
SetupMachineFunction(MF); SetupMachineFunction(MF);
O << "\n\n"; O << "\n\n";
// Populate function information map. Actually, We don't want to populate if (Subtarget->isTargetCOFF()) {
// non-stdcall or non-fastcall functions' information right now. X86COFFMachineModuleInfo &COFFMMI =
if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall) MMI->getObjFileInfo<X86COFFMachineModuleInfo>();
FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
// Populate function information map. Don't want to populate
// non-stdcall or non-fastcall functions' information right now.
if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
COFFMMI.AddFunctionInfo(F, *MF.getInfo<X86MachineFunctionInfo>());
}
// Print out constants referenced by the function // Print out constants referenced by the function
EmitConstantPool(MF.getConstantPool()); EmitConstantPool(MF.getConstantPool());
if (F->hasDLLExportLinkage())
DLLExportedFns.insert(Mang->getMangledName(F));
// Print the 'header' of function // Print the 'header' of function
emitFunctionHeader(MF); emitFunctionHeader(MF);
@ -308,8 +225,11 @@ void X86ATTAsmPrinter::printSymbolOperand(const MachineOperand &MO) {
Suffix = "$non_lazy_ptr"; Suffix = "$non_lazy_ptr";
std::string Name = Mang->getMangledName(GV, Suffix, Suffix[0] != '\0'); std::string Name = Mang->getMangledName(GV, Suffix, Suffix[0] != '\0');
if (Subtarget->isTargetCygMing()) if (Subtarget->isTargetCygMing()) {
DecorateCygMingName(Name, GV); X86COFFMachineModuleInfo &COFFMMI =
MMI->getObjFileInfo<X86COFFMachineModuleInfo>();
COFFMMI.DecorateCygMingName(Name, GV, *TM.getTargetData());
}
// Handle dllimport linkage. // Handle dllimport linkage.
if (MO.getTargetFlags() == X86II::MO_DLLIMPORT) if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
@ -954,16 +874,28 @@ void X86ATTAsmPrinter::EmitEndOfAsmFile(Module &M) {
} }
if (Subtarget->isTargetCOFF()) { if (Subtarget->isTargetCOFF()) {
// Necessary for dllexport support
std::vector<std::string> DLLExportedFns, DLLExportedGlobals;
X86COFFMachineModuleInfo &COFFMMI =
MMI->getObjFileInfo<X86COFFMachineModuleInfo>();
TargetLoweringObjectFileCOFF &TLOFCOFF =
static_cast<TargetLoweringObjectFileCOFF&>(getObjFileLowering());
for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
if (I->hasDLLExportLinkage())
DLLExportedFns.push_back(Mang->getMangledName(I));
for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
I != E; ++I) I != E; ++I)
if (I->hasDLLExportLinkage()) if (I->hasDLLExportLinkage())
DLLExportedGVs.insert(Mang->getMangledName(I)); DLLExportedGlobals.push_back(Mang->getMangledName(I));
if (Subtarget->isTargetCygMing()) { if (Subtarget->isTargetCygMing()) {
// Emit type information for external functions // Emit type information for external functions
for (StringSet<>::iterator i = CygMingStubs.begin(), e = CygMingStubs.end(); for (X86COFFMachineModuleInfo::stub_iterator I = COFFMMI.stub_begin(),
i != e; ++i) { E = COFFMMI.stub_end(); I != E; ++I) {
O << "\t.def\t " << i->getKeyData() O << "\t.def\t " << I->getKeyData()
<< ";\t.scl\t" << COFF::C_EXT << ";\t.scl\t" << COFF::C_EXT
<< ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT) << ";\t.type\t" << (COFF::DT_FCN << COFF::N_BTSHFT)
<< ";\t.endef\n"; << ";\t.endef\n";
@ -971,23 +903,16 @@ void X86ATTAsmPrinter::EmitEndOfAsmFile(Module &M) {
} }
// Output linker support code for dllexported globals on windows. // Output linker support code for dllexported globals on windows.
if (!DLLExportedGVs.empty() || !DLLExportedFns.empty()) { if (!DLLExportedGlobals.empty() || !DLLExportedFns.empty()) {
// dllexport symbols only exist on coff targets.
TargetLoweringObjectFileCOFF &TLOFCOFF =
static_cast<TargetLoweringObjectFileCOFF&>(getObjFileLowering());
OutStreamer.SwitchSection(TLOFCOFF.getCOFFSection(".section .drectve", OutStreamer.SwitchSection(TLOFCOFF.getCOFFSection(".section .drectve",
true, true,
SectionKind::getMetadata())); SectionKind::getMetadata()));
for (StringSet<>::iterator i = DLLExportedGVs.begin(), for (unsigned i = 0, e = DLLExportedGlobals.size(); i != e; ++i)
e = DLLExportedGVs.end(); i != e; ++i) O << "\t.ascii \" -export:" << DLLExportedGlobals[i] << ",data\"\n";
O << "\t.ascii \" -export:" << i->getKeyData() << ",data\"\n";
for (StringSet<>::iterator i = DLLExportedFns.begin(), for (unsigned i = 0, e = DLLExportedFns.size(); i != e; ++i)
e = DLLExportedFns.end(); O << "\t.ascii \" -export:" << DLLExportedFns[i] << "\"\n";
i != e; ++i)
O << "\t.ascii \" -export:" << i->getKeyData() << "\"\n";
} }
} }
} }

View File

@ -153,25 +153,6 @@ class VISIBILITY_HIDDEN X86ATTAsmPrinter : public AsmPrinter {
void emitFunctionHeader(const MachineFunction &MF); void emitFunctionHeader(const MachineFunction &MF);
// Necessary for dllexport support
StringSet<> CygMingStubs, DLLExportedFns, DLLExportedGVs;
// We have to propagate some information about MachineFunction to
// AsmPrinter. It's ok, when we're printing the function, since we have
// access to MachineFunction and can get the appropriate MachineFunctionInfo.
// Unfortunately, this is not possible when we're printing reference to
// Function (e.g. calling it and so on). Even more, there is no way to get the
// corresponding MachineFunctions: it can even be not created at all. That's
// why we should use additional structure, when we're collecting all necessary
// information.
//
// This structure is using e.g. for name decoration for stdcall & fastcall'ed
// function, since we have to use arguments' size for decoration.
typedef std::map<const Function*, X86MachineFunctionInfo> FMFInfoMap;
FMFInfoMap FunctionInfoMap;
void DecorateCygMingName(std::string &Name, const GlobalValue *GV);
void DecorateCygMingName(SmallVectorImpl<char> &Name, const GlobalValue *GV);
}; };
} // end namespace llvm } // end namespace llvm

View File

@ -15,6 +15,7 @@
#include "X86MCInstLower.h" #include "X86MCInstLower.h"
#include "X86ATTAsmPrinter.h" #include "X86ATTAsmPrinter.h"
#include "X86MCAsmInfo.h" #include "X86MCAsmInfo.h"
#include "X86COFFMachineModuleInfo.h"
#include "llvm/CodeGen/MachineModuleInfoImpls.h" #include "llvm/CodeGen/MachineModuleInfoImpls.h"
#include "llvm/MC/MCContext.h" #include "llvm/MC/MCContext.h"
#include "llvm/MC/MCExpr.h" #include "llvm/MC/MCExpr.h"
@ -60,8 +61,11 @@ GetGlobalAddressSymbol(const MachineOperand &MO) const {
SmallString<128> Name; SmallString<128> Name;
Mang->getNameWithPrefix(Name, GV, isImplicitlyPrivate); Mang->getNameWithPrefix(Name, GV, isImplicitlyPrivate);
if (getSubtarget().isTargetCygMing()) if (getSubtarget().isTargetCygMing()) {
AsmPrinter.DecorateCygMingName(Name, GV); X86COFFMachineModuleInfo &COFFMMI =
AsmPrinter.MMI->getObjFileInfo<X86COFFMachineModuleInfo>();
COFFMMI.DecorateCygMingName(Name, GV, *AsmPrinter.TM.getTargetData());
}
switch (MO.getTargetFlags()) { switch (MO.getTargetFlags()) {
default: llvm_unreachable("Unknown target flag on GV operand"); default: llvm_unreachable("Unknown target flag on GV operand");

View File

@ -15,6 +15,7 @@ tablegen(X86GenSubtarget.inc -gen-subtarget)
set(sources set(sources
X86CodeEmitter.cpp X86CodeEmitter.cpp
X86COFFMachineModuleInfo.cpp
X86ELFWriterInfo.cpp X86ELFWriterInfo.cpp
X86FloatingPoint.cpp X86FloatingPoint.cpp
X86FloatingPointRegKill.cpp X86FloatingPointRegKill.cpp

View File

@ -0,0 +1,123 @@
//===-- llvm/CodeGen/X86COFFMachineModuleInfo.cpp -------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This is an MMI implementation for X86 COFF (windows) targets.
//
//===----------------------------------------------------------------------===//
#include "X86COFFMachineModuleInfo.h"
#include "X86MachineFunctionInfo.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Function.h"
#include "llvm/Target/TargetData.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
X86COFFMachineModuleInfo::X86COFFMachineModuleInfo(const MachineModuleInfo &) {
}
X86COFFMachineModuleInfo::~X86COFFMachineModuleInfo() {
}
void X86COFFMachineModuleInfo::AddFunctionInfo(const Function *F,
const X86MachineFunctionInfo &Val) {
FunctionInfoMap[F] = Val;
}
static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
const TargetData &TD) {
X86MachineFunctionInfo Info;
uint64_t Size = 0;
switch (F->getCallingConv()) {
case CallingConv::X86_StdCall:
Info.setDecorationStyle(StdCall);
break;
case CallingConv::X86_FastCall:
Info.setDecorationStyle(FastCall);
break;
default:
return Info;
}
unsigned argNum = 1;
for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
AI != AE; ++AI, ++argNum) {
const Type* Ty = AI->getType();
// 'Dereference' type in case of byval parameter attribute
if (F->paramHasAttr(argNum, Attribute::ByVal))
Ty = cast<PointerType>(Ty)->getElementType();
// Size should be aligned to DWORD boundary
Size += ((TD.getTypeAllocSize(Ty) + 3)/4)*4;
}
// We're not supporting tooooo huge arguments :)
Info.setBytesToPopOnReturn((unsigned int)Size);
return Info;
}
/// DecorateCygMingName - Query FunctionInfoMap and use this information for
/// various name decorations for Cygwin and MingW.
void X86COFFMachineModuleInfo::DecorateCygMingName(SmallVectorImpl<char> &Name,
const GlobalValue *GV,
const TargetData &TD) {
const Function *F = dyn_cast<Function>(GV);
if (!F) return;
// Save function name for later type emission.
if (F->isDeclaration())
CygMingStubs.insert(StringRef(Name.data(), Name.size()));
// We don't want to decorate non-stdcall or non-fastcall functions right now
CallingConv::ID CC = F->getCallingConv();
if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
return;
const X86MachineFunctionInfo *Info;
FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
if (info_item == FunctionInfoMap.end()) {
// Calculate apropriate function info and populate map
FunctionInfoMap[F] = calculateFunctionInfo(F, TD);
Info = &FunctionInfoMap[F];
} else {
Info = &info_item->second;
}
if (Info->getDecorationStyle() == None) return;
const FunctionType *FT = F->getFunctionType();
// "Pure" variadic functions do not receive @0 suffix.
if (!FT->isVarArg() || FT->getNumParams() == 0 ||
(FT->getNumParams() == 1 && F->hasStructRetAttr()))
raw_svector_ostream(Name) << '@' << Info->getBytesToPopOnReturn();
if (Info->getDecorationStyle() == FastCall) {
if (Name[0] == '_')
Name[0] = '@';
else
Name.insert(Name.begin(), '@');
}
}
/// DecorateCygMingName - Query FunctionInfoMap and use this information for
/// various name decorations for Cygwin and MingW.
void X86COFFMachineModuleInfo::DecorateCygMingName(std::string &Name,
const GlobalValue *GV,
const TargetData &TD) {
SmallString<128> NameStr(Name.begin(), Name.end());
DecorateCygMingName(NameStr, GV, TD);
Name.assign(NameStr.begin(), NameStr.end());
}

View File

@ -0,0 +1,67 @@
//===-- llvm/CodeGen/X86COFFMachineModuleInfo.h -----------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This is an MMI implementation for X86 COFF (windows) targets.
//
//===----------------------------------------------------------------------===//
#ifndef X86COFF_MACHINEMODULEINFO_H
#define X86COFF_MACHINEMODULEINFO_H
#include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/ADT/StringSet.h"
namespace llvm {
class X86MachineFunctionInfo;
class TargetData;
/// X86COFFMachineModuleInfo - This is a MachineModuleInfoImpl implementation
/// for X86 COFF targets.
class X86COFFMachineModuleInfo : public MachineModuleInfoImpl {
StringSet<> CygMingStubs;
// We have to propagate some information about MachineFunction to
// AsmPrinter. It's ok, when we're printing the function, since we have
// access to MachineFunction and can get the appropriate MachineFunctionInfo.
// Unfortunately, this is not possible when we're printing reference to
// Function (e.g. calling it and so on). Even more, there is no way to get the
// corresponding MachineFunctions: it can even be not created at all. That's
// why we should use additional structure, when we're collecting all necessary
// information.
//
// This structure is using e.g. for name decoration for stdcall & fastcall'ed
// function, since we have to use arguments' size for decoration.
typedef std::map<const Function*, X86MachineFunctionInfo> FMFInfoMap;
FMFInfoMap FunctionInfoMap;
public:
X86COFFMachineModuleInfo(const MachineModuleInfo &);
~X86COFFMachineModuleInfo();
void DecorateCygMingName(std::string &Name, const GlobalValue *GV,
const TargetData &TD);
void DecorateCygMingName(SmallVectorImpl<char> &Name, const GlobalValue *GV,
const TargetData &TD);
void AddFunctionInfo(const Function *F, const X86MachineFunctionInfo &Val);
typedef StringSet<>::const_iterator stub_iterator;
stub_iterator stub_begin() const { return CygMingStubs.begin(); }
stub_iterator stub_end() const { return CygMingStubs.end(); }
};
} // end namespace llvm
#endif