do not embed the raw_ostream into TypePrinting, pass it as an argument to print etc.

llvm-svn: 65723
This commit is contained in:
Chris Lattner 2009-02-28 21:26:53 +00:00
parent 68318b1535
commit e101c44d77
1 changed files with 43 additions and 52 deletions

View File

@ -141,12 +141,11 @@ namespace {
/// TypePrinting - Type printing machinery. /// TypePrinting - Type printing machinery.
class TypePrinting { class TypePrinting {
std::map<const Type *, std::string> TypeNames; std::map<const Type *, std::string> TypeNames;
raw_ostream &OS;
public: public:
TypePrinting(const Module *M, raw_ostream &os); TypePrinting(const Module *M);
void print(const Type *Ty); void print(const Type *Ty, raw_ostream &OS);
void printAtLeastOneLevel(const Type *Ty); void printAtLeastOneLevel(const Type *Ty, raw_ostream &OS);
private: private:
void CalcTypeName(const Type *Ty, SmallVectorImpl<const Type *> &TypeStack, void CalcTypeName(const Type *Ty, SmallVectorImpl<const Type *> &TypeStack,
@ -154,7 +153,7 @@ namespace {
}; };
} // end anonymous namespace. } // end anonymous namespace.
TypePrinting::TypePrinting(const Module *M, raw_ostream &os) : OS(os) { TypePrinting::TypePrinting(const Module *M) {
if (M == 0) return; if (M == 0) return;
// If the module has a symbol table, take all global types and stuff their // If the module has a symbol table, take all global types and stuff their
@ -295,7 +294,7 @@ void TypePrinting::CalcTypeName(const Type *Ty,
/// printTypeInt - The internal guts of printing out a type that has a /// printTypeInt - The internal guts of printing out a type that has a
/// potentially named portion. /// potentially named portion.
/// ///
void TypePrinting::print(const Type *Ty) { void TypePrinting::print(const Type *Ty, raw_ostream &OS) {
// Check to see if the type is named. // Check to see if the type is named.
std::map<const Type*, std::string>::iterator I = TypeNames.find(Ty); std::map<const Type*, std::string>::iterator I = TypeNames.find(Ty);
if (I != TypeNames.end()) { if (I != TypeNames.end()) {
@ -319,12 +318,12 @@ void TypePrinting::print(const Type *Ty) {
/// printAtLeastOneLevel - Print out one level of the possibly complex type /// printAtLeastOneLevel - Print out one level of the possibly complex type
/// without considering any symbolic types that we may have equal to it. /// without considering any symbolic types that we may have equal to it.
void TypePrinting::printAtLeastOneLevel(const Type *Ty) { void TypePrinting::printAtLeastOneLevel(const Type *Ty, raw_ostream &OS) {
// If the type does not have a name, then it is already guaranteed to print at // If the type does not have a name, then it is already guaranteed to print at
// least one level. // least one level.
std::map<const Type*, std::string>::iterator I = TypeNames.find(Ty); std::map<const Type*, std::string>::iterator I = TypeNames.find(Ty);
if (I == TypeNames.end()) if (I == TypeNames.end())
return print(Ty); return print(Ty, OS);
// Otherwise, temporarily remove the name and print it. // Otherwise, temporarily remove the name and print it.
std::string OldName; std::string OldName;
@ -343,8 +342,8 @@ void TypePrinting::printAtLeastOneLevel(const Type *Ty) {
/// 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 &Out, const Type *Ty, const Module *M){ void llvm::WriteTypeSymbolic(raw_ostream &OS, const Type *Ty, const Module *M){
TypePrinting(M, Out).print(Ty); TypePrinting(M).print(Ty, OS);
} }
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
@ -740,13 +739,13 @@ static void WriteConstantInt(raw_ostream &Out, const Constant *CV,
} else { // Cannot output in string format... } else { // Cannot output in string format...
Out << '['; Out << '[';
if (CA->getNumOperands()) { if (CA->getNumOperands()) {
TypePrinter.print(ETy); TypePrinter.print(ETy, Out);
Out << ' '; Out << ' ';
WriteAsOperandInternal(Out, CA->getOperand(0), WriteAsOperandInternal(Out, CA->getOperand(0),
TypePrinter, Machine); TypePrinter, Machine);
for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) { for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
Out << ", "; Out << ", ";
TypePrinter.print(ETy); TypePrinter.print(ETy, Out);
Out << ' '; Out << ' ';
WriteAsOperandInternal(Out, CA->getOperand(i), TypePrinter, Machine); WriteAsOperandInternal(Out, CA->getOperand(i), TypePrinter, Machine);
} }
@ -763,14 +762,14 @@ static void WriteConstantInt(raw_ostream &Out, const Constant *CV,
unsigned N = CS->getNumOperands(); unsigned N = CS->getNumOperands();
if (N) { if (N) {
Out << ' '; Out << ' ';
TypePrinter.print(CS->getOperand(0)->getType()); TypePrinter.print(CS->getOperand(0)->getType(), Out);
Out << ' '; Out << ' ';
WriteAsOperandInternal(Out, CS->getOperand(0), TypePrinter, Machine); WriteAsOperandInternal(Out, CS->getOperand(0), TypePrinter, Machine);
for (unsigned i = 1; i < N; i++) { for (unsigned i = 1; i < N; i++) {
Out << ", "; Out << ", ";
TypePrinter.print(CS->getOperand(i)->getType()); TypePrinter.print(CS->getOperand(i)->getType(), Out);
Out << ' '; Out << ' ';
WriteAsOperandInternal(Out, CS->getOperand(i), TypePrinter, Machine); WriteAsOperandInternal(Out, CS->getOperand(i), TypePrinter, Machine);
@ -789,12 +788,12 @@ static void WriteConstantInt(raw_ostream &Out, const Constant *CV,
assert(CP->getNumOperands() > 0 && assert(CP->getNumOperands() > 0 &&
"Number of operands for a PackedConst must be > 0"); "Number of operands for a PackedConst must be > 0");
Out << '<'; Out << '<';
TypePrinter.print(ETy); TypePrinter.print(ETy, Out);
Out << ' '; Out << ' ';
WriteAsOperandInternal(Out, CP->getOperand(0), TypePrinter, Machine); WriteAsOperandInternal(Out, CP->getOperand(0), TypePrinter, Machine);
for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) { for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
Out << ", "; Out << ", ";
TypePrinter.print(ETy); TypePrinter.print(ETy, Out);
Out << ' '; Out << ' ';
WriteAsOperandInternal(Out, CP->getOperand(i), TypePrinter, Machine); WriteAsOperandInternal(Out, CP->getOperand(i), TypePrinter, Machine);
} }
@ -819,7 +818,7 @@ static void WriteConstantInt(raw_ostream &Out, const Constant *CV,
Out << " ("; Out << " (";
for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) { for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
TypePrinter.print((*OI)->getType()); TypePrinter.print((*OI)->getType(), Out);
Out << ' '; Out << ' ';
WriteAsOperandInternal(Out, *OI, TypePrinter, Machine); WriteAsOperandInternal(Out, *OI, TypePrinter, Machine);
if (OI+1 != CE->op_end()) if (OI+1 != CE->op_end())
@ -834,7 +833,7 @@ static void WriteConstantInt(raw_ostream &Out, const Constant *CV,
if (CE->isCast()) { if (CE->isCast()) {
Out << " to "; Out << " to ";
TypePrinter.print(CE->getType()); TypePrinter.print(CE->getType(), Out);
} }
Out << ')'; Out << ')';
@ -919,9 +918,9 @@ 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, Out); TypePrinting TypePrinter(Context);
if (PrintType) { if (PrintType) {
TypePrinter.print(V->getType()); TypePrinter.print(V->getType(), Out);
Out << ' '; Out << ' ';
} }
@ -940,7 +939,7 @@ 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), : Out(o), Machine(Mac), TheModule(M), TypePrinter(M),
AnnotationWriter(AAW) { AnnotationWriter(AAW) {
} }
@ -959,7 +958,6 @@ public:
void write(const BasicBlock *BB) { printBasicBlock(BB); } void write(const BasicBlock *BB) { printBasicBlock(BB); }
void write(const Instruction *I) { printInstruction(*I); } void write(const Instruction *I) { printInstruction(*I); }
// void write(const Type *Ty) { printType(Ty); }
void writeOperand(const Value *Op, bool PrintType); void writeOperand(const Value *Op, bool PrintType);
void writeParamOperand(const Value *Operand, Attributes Attrs); void writeParamOperand(const Value *Operand, Attributes Attrs);
@ -976,13 +974,6 @@ private:
void printBasicBlock(const BasicBlock *BB); void printBasicBlock(const BasicBlock *BB);
void printInstruction(const Instruction &I); void printInstruction(const Instruction &I);
// printType - Go to extreme measures to attempt to print out a short,
// symbolic version of a type name.
//
void printType(const Type *Ty) {
TypePrinter.print(Ty);
}
// printInfoComment - Print a little comment after the instruction indicating // printInfoComment - Print a little comment after the instruction indicating
// which slot it occupies. // which slot it occupies.
void printInfoComment(const Value &V); void printInfoComment(const Value &V);
@ -995,7 +986,7 @@ void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
Out << "<null operand!>"; Out << "<null operand!>";
} else { } else {
if (PrintType) { if (PrintType) {
printType(Operand->getType()); TypePrinter.print(Operand->getType(), Out);
Out << ' '; Out << ' ';
} }
WriteAsOperandInternal(Out, Operand, TypePrinter, &Machine); WriteAsOperandInternal(Out, Operand, TypePrinter, &Machine);
@ -1008,7 +999,7 @@ void AssemblyWriter::writeParamOperand(const Value *Operand,
Out << "<null operand!>"; Out << "<null operand!>";
} else { } else {
// Print the type // Print the type
printType(Operand->getType()); TypePrinter.print(Operand->getType(), Out);
// Print parameter attributes list // Print parameter attributes list
if (Attrs != Attribute::None) if (Attrs != Attribute::None)
Out << ' ' << Attribute::getAsString(Attrs); Out << ' ' << Attribute::getAsString(Attrs);
@ -1127,7 +1118,7 @@ void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
if (unsigned AddressSpace = GV->getType()->getAddressSpace()) if (unsigned AddressSpace = GV->getType()->getAddressSpace())
Out << "addrspace(" << AddressSpace << ") "; Out << "addrspace(" << AddressSpace << ") ";
Out << (GV->isConstant() ? "constant " : "global "); Out << (GV->isConstant() ? "constant " : "global ");
printType(GV->getType()->getElementType()); TypePrinter.print(GV->getType()->getElementType(), Out);
if (GV->hasInitializer()) { if (GV->hasInitializer()) {
Out << ' '; Out << ' ';
@ -1160,17 +1151,17 @@ void AssemblyWriter::printAlias(const GlobalAlias *GA) {
const Constant *Aliasee = GA->getAliasee(); const Constant *Aliasee = GA->getAliasee();
if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Aliasee)) { if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Aliasee)) {
printType(GV->getType()); TypePrinter.print(GV->getType(), Out);
Out << ' '; Out << ' ';
PrintLLVMName(Out, GV); PrintLLVMName(Out, GV);
} else if (const Function *F = dyn_cast<Function>(Aliasee)) { } else if (const Function *F = dyn_cast<Function>(Aliasee)) {
printType(F->getFunctionType()); TypePrinter.print(F->getFunctionType(), Out);
Out << "* "; Out << "* ";
WriteAsOperandInternal(Out, F, TypePrinter, &Machine); WriteAsOperandInternal(Out, F, TypePrinter, &Machine);
} else if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(Aliasee)) { } else if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(Aliasee)) {
printType(GA->getType()); TypePrinter.print(GA->getType(), Out);
Out << " "; Out << ' ';
PrintLLVMName(Out, GA); PrintLLVMName(Out, GA);
} else { } else {
const ConstantExpr *CE = 0; const ConstantExpr *CE = 0;
@ -1195,7 +1186,7 @@ void AssemblyWriter::printTypeSymbolTable(const TypeSymbolTable &ST) {
// Make sure we print out at least one level of the type structure, so // Make sure we print out at least one level of the type structure, so
// that we do not get %FILE = type %FILE // that we do not get %FILE = type %FILE
TypePrinter.printAtLeastOneLevel(TI->second); TypePrinter.printAtLeastOneLevel(TI->second, Out);
Out << '\n'; Out << '\n';
} }
} }
@ -1231,7 +1222,7 @@ void AssemblyWriter::printFunction(const Function *F) {
Attributes RetAttrs = Attrs.getRetAttributes(); Attributes RetAttrs = Attrs.getRetAttributes();
if (RetAttrs != Attribute::None) if (RetAttrs != Attribute::None)
Out << Attribute::getAsString(Attrs.getRetAttributes()) << ' '; Out << Attribute::getAsString(Attrs.getRetAttributes()) << ' ';
printType(F->getReturnType()); TypePrinter.print(F->getReturnType(), Out);
Out << ' '; Out << ' ';
WriteAsOperandInternal(Out, F, TypePrinter, &Machine); WriteAsOperandInternal(Out, F, TypePrinter, &Machine);
Out << '('; Out << '(';
@ -1256,7 +1247,7 @@ void AssemblyWriter::printFunction(const Function *F) {
if (i) Out << ", "; if (i) Out << ", ";
// Output type... // Output type...
printType(FT->getParamType(i)); TypePrinter.print(FT->getParamType(i), Out);
Attributes ArgAttrs = Attrs.getParamAttributes(i+1); Attributes ArgAttrs = Attrs.getParamAttributes(i+1);
if (ArgAttrs != Attribute::None) if (ArgAttrs != Attribute::None)
@ -1300,7 +1291,7 @@ void AssemblyWriter::printFunction(const Function *F) {
void AssemblyWriter::printArgument(const Argument *Arg, void AssemblyWriter::printArgument(const Argument *Arg,
Attributes Attrs) { Attributes Attrs) {
// Output type... // Output type...
printType(Arg->getType()); TypePrinter.print(Arg->getType(), Out);
// Output parameter attributes list // Output parameter attributes list
if (Attrs != Attribute::None) if (Attrs != Attribute::None)
@ -1366,7 +1357,7 @@ void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
void AssemblyWriter::printInfoComment(const Value &V) { void AssemblyWriter::printInfoComment(const Value &V) {
if (V.getType() != Type::VoidTy) { if (V.getType() != Type::VoidTy) {
Out << "\t\t; <"; Out << "\t\t; <";
printType(V.getType()); TypePrinter.print(V.getType(), Out);
Out << '>'; Out << '>';
if (!V.hasName() && !isa<Instruction>(V)) { if (!V.hasName() && !isa<Instruction>(V)) {
@ -1449,7 +1440,7 @@ void AssemblyWriter::printInstruction(const Instruction &I) {
Out << "\n\t]"; Out << "\n\t]";
} else if (isa<PHINode>(I)) { } else if (isa<PHINode>(I)) {
Out << ' '; Out << ' ';
printType(I.getType()); TypePrinter.print(I.getType(), Out);
Out << ' '; Out << ' ';
for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) { for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
@ -1498,7 +1489,7 @@ void AssemblyWriter::printInstruction(const Instruction &I) {
if (!FTy->isVarArg() && if (!FTy->isVarArg() &&
(!isa<PointerType>(RetTy) || (!isa<PointerType>(RetTy) ||
!isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) { !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
printType(RetTy); TypePrinter.print(RetTy, Out);
Out << ' '; Out << ' ';
writeOperand(Operand, false); writeOperand(Operand, false);
} else { } else {
@ -1540,7 +1531,7 @@ void AssemblyWriter::printInstruction(const Instruction &I) {
if (!FTy->isVarArg() && if (!FTy->isVarArg() &&
(!isa<PointerType>(RetTy) || (!isa<PointerType>(RetTy) ||
!isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) { !isa<FunctionType>(cast<PointerType>(RetTy)->getElementType()))) {
printType(RetTy); TypePrinter.print(RetTy, Out);
Out << ' '; Out << ' ';
writeOperand(Operand, false); writeOperand(Operand, false);
} else { } else {
@ -1564,7 +1555,7 @@ void AssemblyWriter::printInstruction(const Instruction &I) {
} else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) { } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
Out << ' '; Out << ' ';
printType(AI->getType()->getElementType()); TypePrinter.print(AI->getType()->getElementType(), Out);
if (AI->isArrayAllocation()) { if (AI->isArrayAllocation()) {
Out << ", "; Out << ", ";
writeOperand(AI->getArraySize(), true); writeOperand(AI->getArraySize(), true);
@ -1578,15 +1569,15 @@ void AssemblyWriter::printInstruction(const Instruction &I) {
writeOperand(Operand, true); // Work with broken code writeOperand(Operand, true); // Work with broken code
} }
Out << " to "; Out << " to ";
printType(I.getType()); TypePrinter.print(I.getType(), Out);
} else if (isa<VAArgInst>(I)) { } else if (isa<VAArgInst>(I)) {
if (Operand) { if (Operand) {
Out << ' '; Out << ' ';
writeOperand(Operand, true); // Work with broken code writeOperand(Operand, true); // Work with broken code
} }
Out << ", "; Out << ", ";
printType(I.getType()); TypePrinter.print(I.getType(), Out);
} else if (Operand) { // Print the normal way... } else if (Operand) { // Print the normal way.
// PrintAllTypes - Instructions who have operands of all the same type // PrintAllTypes - Instructions who have operands of all the same type
// omit the type from all but the first operand. If the instruction has // omit the type from all but the first operand. If the instruction has
@ -1612,7 +1603,7 @@ void AssemblyWriter::printInstruction(const Instruction &I) {
if (!PrintAllTypes) { if (!PrintAllTypes) {
Out << ' '; Out << ' ';
printType(TheType); TypePrinter.print(TheType, Out);
} }
Out << ' '; Out << ' ';
@ -1658,7 +1649,7 @@ void Type::print(raw_ostream &OS) const {
OS << "<null Type>"; OS << "<null Type>";
return; return;
} }
TypePrinting(0, OS).print(this); TypePrinting(0).print(this, OS);
} }
void Value::print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const { void Value::print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const {
@ -1682,8 +1673,8 @@ 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, OS); TypePrinting TypePrinter(0);
TypePrinter.print(C->getType()); TypePrinter.print(C->getType(), OS);
OS << ' '; OS << ' ';
WriteConstantInt(OS, C, TypePrinter, 0); WriteConstantInt(OS, C, TypePrinter, 0);
} else if (const Argument *A = dyn_cast<Argument>(this)) { } else if (const Argument *A = dyn_cast<Argument>(this)) {