1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2025-01-31 12:41:49 +01:00

move type name population out of TypePrinting class into a static

AsmWriter.cpp method.

llvm-svn: 65736
This commit is contained in:
Chris Lattner 2009-02-28 23:20:19 +00:00
parent 666bf65fa3
commit 1a7eac8434
2 changed files with 62 additions and 38 deletions

View File

@ -29,11 +29,11 @@ template <typename T> class SmallVectorImpl;
/// TypePrinting - Type printing machinery. /// TypePrinting - Type printing machinery.
class TypePrinting { class TypePrinting {
void *TypeNames; void *TypeNames; // A map to remember type names.
TypePrinting(const TypePrinting &); // DO NOT IMPLEMENT TypePrinting(const TypePrinting &); // DO NOT IMPLEMENT
void operator=(const TypePrinting&); // DO NOT IMPLEMENT void operator=(const TypePrinting&); // DO NOT IMPLEMENT
public: public:
TypePrinting(const Module *M = 0); TypePrinting();
~TypePrinting(); ~TypePrinting();
void clear(); void clear();
@ -41,6 +41,15 @@ public:
void print(const Type *Ty, raw_ostream &OS); void print(const Type *Ty, raw_ostream &OS);
void printAtLeastOneLevel(const Type *Ty, raw_ostream &OS); void printAtLeastOneLevel(const Type *Ty, raw_ostream &OS);
/// hasTypeName - Return true if the type has a name in TypeNames, false
/// otherwise.
bool hasTypeName(const Type *Ty) const;
/// addTypeName - Add a name for the specified type if it doesn't already have
/// one. This name will be printed instead of the structural version of the
/// type in order to make the output more concise.
void addTypeName(const Type *Ty, const std::string &N);
private: private:
void CalcTypeName(const Type *Ty, SmallVectorImpl<const Type *> &TypeStack, void CalcTypeName(const Type *Ty, SmallVectorImpl<const Type *> &TypeStack,
raw_ostream &OS); raw_ostream &OS);

View File

@ -145,36 +145,17 @@ void TypePrinting::clear() {
getTypeNamesMap(TypeNames).clear(); getTypeNamesMap(TypeNames).clear();
} }
TypePrinting::TypePrinting(const Module *M) { bool TypePrinting::hasTypeName(const Type *Ty) const {
return getTypeNamesMap(TypeNames).count(Ty);
}
void TypePrinting::addTypeName(const Type *Ty, const std::string &N) {
getTypeNamesMap(TypeNames).insert(std::make_pair(Ty, N));
}
TypePrinting::TypePrinting() {
TypeNames = new DenseMap<const Type *, std::string>(); TypeNames = new DenseMap<const Type *, std::string>();
if (M == 0) return;
// If the module has a symbol table, take all global types and stuff their
// names into the TypeNames map.
const TypeSymbolTable &ST = M->getTypeSymbolTable();
for (TypeSymbolTable::const_iterator TI = ST.begin(), E = ST.end();
TI != E; ++TI) {
const Type *Ty = cast<Type>(TI->second);
// As a heuristic, don't insert pointer to primitive types, because
// they are used too often to have a single useful name.
if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
const Type *PETy = PTy->getElementType();
if ((PETy->isPrimitiveType() || PETy->isInteger()) &&
!isa<OpaqueType>(PETy))
continue;
}
// Likewise don't insert primitives either.
if (Ty->isInteger() || Ty->isPrimitiveType())
continue;
// Get the name as a string and insert it into TypeNames.
std::string NameStr;
raw_string_ostream NameOS(NameStr);
PrintLLVMName(NameOS, TI->first.c_str(), TI->first.length(), LocalPrefix);
getTypeNamesMap(TypeNames).insert(std::make_pair(Ty, NameOS.str()));
}
} }
TypePrinting::~TypePrinting() { TypePrinting::~TypePrinting() {
@ -337,13 +318,46 @@ void TypePrinting::printAtLeastOneLevel(const Type *Ty, raw_ostream &OS) {
std::swap(OldName, I->second); std::swap(OldName, I->second);
} }
static void AddModuleTypesToPrinter(TypePrinting &TP, const Module *M) {
if (M == 0) return;
// If the module has a symbol table, take all global types and stuff their
// names into the TypeNames map.
const TypeSymbolTable &ST = M->getTypeSymbolTable();
for (TypeSymbolTable::const_iterator TI = ST.begin(), E = ST.end();
TI != E; ++TI) {
const Type *Ty = cast<Type>(TI->second);
// As a heuristic, don't insert pointer to primitive types, because
// they are used too often to have a single useful name.
if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
const Type *PETy = PTy->getElementType();
if ((PETy->isPrimitiveType() || PETy->isInteger()) &&
!isa<OpaqueType>(PETy))
continue;
}
// Likewise don't insert primitives either.
if (Ty->isInteger() || Ty->isPrimitiveType())
continue;
// Get the name as a string and insert it into TypeNames.
std::string NameStr;
raw_string_ostream NameOS(NameStr);
PrintLLVMName(NameOS, TI->first.c_str(), TI->first.length(), LocalPrefix);
TP.addTypeName(Ty, NameOS.str());
}
}
/// WriteTypeSymbolic - This attempts to write the specified type as a symbolic /// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
/// type, iff there is an entry in the modules symbol table for the specified /// type, iff there is an entry in the modules symbol table for the specified
/// type or one of it's component types. /// type or one of it's component types.
/// ///
void llvm::WriteTypeSymbolic(raw_ostream &OS, const Type *Ty, const Module *M){ void llvm::WriteTypeSymbolic(raw_ostream &OS, const Type *Ty, const Module *M) {
TypePrinting(M).print(Ty, OS); TypePrinting Printer;
AddModuleTypesToPrinter(Printer, M);
Printer.print(Ty, OS);
} }
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
@ -918,7 +932,8 @@ void llvm::WriteAsOperand(raw_ostream &Out, const Value *V, bool PrintType,
const Module *Context) { const Module *Context) {
if (Context == 0) Context = getModuleFromVal(V); if (Context == 0) Context = getModuleFromVal(V);
TypePrinting TypePrinter(Context); TypePrinting TypePrinter;
AddModuleTypesToPrinter(TypePrinter, Context);
if (PrintType) { if (PrintType) {
TypePrinter.print(V->getType(), Out); TypePrinter.print(V->getType(), Out);
Out << ' '; Out << ' ';
@ -939,8 +954,8 @@ class AssemblyWriter {
public: public:
inline AssemblyWriter(raw_ostream &o, SlotTracker &Mac, const Module *M, inline AssemblyWriter(raw_ostream &o, SlotTracker &Mac, const Module *M,
AssemblyAnnotationWriter *AAW) AssemblyAnnotationWriter *AAW)
: Out(o), Machine(Mac), TheModule(M), TypePrinter(M), : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
AnnotationWriter(AAW) { AddModuleTypesToPrinter(TypePrinter, M);
} }
void write(const Module *M) { printModule(M); } void write(const Module *M) { printModule(M); }
@ -1649,7 +1664,7 @@ void Type::print(raw_ostream &OS) const {
OS << "<null Type>"; OS << "<null Type>";
return; return;
} }
TypePrinting(0).print(this, OS); TypePrinting().print(this, OS);
} }
void Value::print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const { void Value::print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const {
@ -1673,7 +1688,7 @@ void Value::print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const {
AssemblyWriter W(OS, SlotTable, GV->getParent(), 0); AssemblyWriter W(OS, SlotTable, GV->getParent(), 0);
W.write(GV); W.write(GV);
} else if (const Constant *C = dyn_cast<Constant>(this)) { } else if (const Constant *C = dyn_cast<Constant>(this)) {
TypePrinting TypePrinter(0); TypePrinting TypePrinter;
TypePrinter.print(C->getType(), OS); TypePrinter.print(C->getType(), OS);
OS << ' '; OS << ' ';
WriteConstantInt(OS, C, TypePrinter, 0); WriteConstantInt(OS, C, TypePrinter, 0);