#include "llvm/InlineAsm.h"
#include "llvm/Instruction.h"
#include "llvm/Instructions.h"
+#include "llvm/MDNode.h"
#include "llvm/Module.h"
#include "llvm/ValueSymbolTable.h"
#include "llvm/TypeSymbolTable.h"
-#include "llvm/ADT/DenseMap.h"
+#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/CFG.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cctype>
+#include <map>
using namespace llvm;
// Make virtual table appear in this compilation unit.
raw_ostream &Out) {
for (unsigned i = 0; i != Length; ++i) {
unsigned char C = Str[i];
- if (isprint(C) && C != '\\' && C != '"' && isprint(C))
+ if (isprint(C) && C != '\\' && C != '"')
Out << C;
else
Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
case GlobalPrefix: OS << '@'; break;
case LabelPrefix: break;
case LocalPrefix: OS << '%'; break;
- }
+ }
// Scan the name to see if it needs quotes first.
bool NeedsQuotes = isdigit(NameStr[0]);
/// use of type names or up references to shorten the type name where possible.
void TypePrinting::CalcTypeName(const Type *Ty,
SmallVectorImpl<const Type *> &TypeStack,
- raw_ostream &OS) {
+ raw_ostream &OS, bool IgnoreTopLevelName) {
// Check to see if the type is named.
- DenseMap<const Type*, std::string> &TM = getTypeNamesMap(TypeNames);
- DenseMap<const Type *, std::string>::iterator I = TM.find(Ty);
- if (I != TM.end() &&
- // If the name wasn't temporarily removed use it.
- !I->second.empty()) {
- OS << I->second;
- return;
+ if (!IgnoreTopLevelName) {
+ DenseMap<const Type *, std::string> &TM = getTypeNamesMap(TypeNames);
+ DenseMap<const Type *, std::string>::iterator I = TM.find(Ty);
+ if (I != TM.end()) {
+ OS << I->second;
+ return;
+ }
}
// Check to see if the Type is already on the stack...
case Type::FP128TyID: OS << "fp128"; break;
case Type::PPC_FP128TyID: OS << "ppc_fp128"; break;
case Type::LabelTyID: OS << "label"; break;
+ case Type::MetadataTyID: OS << "metadata"; break;
case Type::IntegerTyID:
OS << 'i' << cast<IntegerType>(Ty)->getBitWidth();
break;
/// printTypeInt - The internal guts of printing out a type that has a
/// potentially named portion.
///
-void TypePrinting::print(const Type *Ty, raw_ostream &OS) {
+void TypePrinting::print(const Type *Ty, raw_ostream &OS,
+ bool IgnoreTopLevelName) {
// Check to see if the type is named.
DenseMap<const Type*, std::string> &TM = getTypeNamesMap(TypeNames);
- DenseMap<const Type*, std::string>::iterator I = TM.find(Ty);
- if (I != TM.end()) {
- OS << I->second;
- return;
+ if (!IgnoreTopLevelName) {
+ DenseMap<const Type*, std::string>::iterator I = TM.find(Ty);
+ if (I != TM.end()) {
+ OS << I->second;
+ return;
+ }
}
// Otherwise we have a type that has not been named but is a derived type.
std::string TypeName;
raw_string_ostream TypeOS(TypeName);
- CalcTypeName(Ty, TypeStack, TypeOS);
+ CalcTypeName(Ty, TypeStack, TypeOS, IgnoreTopLevelName);
OS << TypeOS.str();
// Cache type name for later use.
- TM.insert(std::make_pair(Ty, TypeOS.str()));
+ if (!IgnoreTopLevelName)
+ TM.insert(std::make_pair(Ty, TypeOS.str()));
}
-/// printAtLeastOneLevel - Print out one level of the possibly complex type
-/// without considering any symbolic types that we may have equal to it.
-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
- // least one level.
- DenseMap<const Type*, std::string> &TM = getTypeNamesMap(TypeNames);
- DenseMap<const Type*, std::string>::iterator I = TM.find(Ty);
- if (I == TM.end())
- return print(Ty, OS);
-
- // Otherwise, temporarily remove the name and print it.
- std::string OldName;
- std::swap(OldName, I->second);
-
- // Print the type without the name.
- SmallVector<const Type *, 16> TypeStack;
- CalcTypeName(Ty, TypeStack, OS);
+namespace {
+ class TypeFinder {
+ // To avoid walking constant expressions multiple times and other IR
+ // objects, we keep several helper maps.
+ DenseSet<const Value*> VisitedConstants;
+ DenseSet<const Type*> VisitedTypes;
+
+ TypePrinting &TP;
+ std::vector<const Type*> &NumberedTypes;
+ public:
+ TypeFinder(TypePrinting &tp, std::vector<const Type*> &numberedTypes)
+ : TP(tp), NumberedTypes(numberedTypes) {}
+
+ void Run(const Module &M) {
+ // Get types from the type symbol table. This gets opaque types referened
+ // only through derived named types.
+ const TypeSymbolTable &ST = M.getTypeSymbolTable();
+ for (TypeSymbolTable::const_iterator TI = ST.begin(), E = ST.end();
+ TI != E; ++TI)
+ IncorporateType(TI->second);
+
+ // Get types from global variables.
+ for (Module::const_global_iterator I = M.global_begin(),
+ E = M.global_end(); I != E; ++I) {
+ IncorporateType(I->getType());
+ if (I->hasInitializer())
+ IncorporateValue(I->getInitializer());
+ }
+
+ // Get types from aliases.
+ for (Module::const_alias_iterator I = M.alias_begin(),
+ E = M.alias_end(); I != E; ++I) {
+ IncorporateType(I->getType());
+ IncorporateValue(I->getAliasee());
+ }
+
+ // Get types from functions.
+ for (Module::const_iterator FI = M.begin(), E = M.end(); FI != E; ++FI) {
+ IncorporateType(FI->getType());
+
+ for (Function::const_iterator BB = FI->begin(), E = FI->end();
+ BB != E;++BB)
+ for (BasicBlock::const_iterator II = BB->begin(),
+ E = BB->end(); II != E; ++II) {
+ const Instruction &I = *II;
+ // Incorporate the type of the instruction and all its operands.
+ IncorporateType(I.getType());
+ for (User::const_op_iterator OI = I.op_begin(), OE = I.op_end();
+ OI != OE; ++OI)
+ IncorporateValue(*OI);
+ }
+ }
+ }
+
+ private:
+ void IncorporateType(const Type *Ty) {
+ // Check to see if we're already visited this type.
+ if (!VisitedTypes.insert(Ty).second)
+ return;
+
+ // If this is a structure or opaque type, add a name for the type.
+ if (((isa<StructType>(Ty) && cast<StructType>(Ty)->getNumElements())
+ || isa<OpaqueType>(Ty)) && !TP.hasTypeName(Ty)) {
+ TP.addTypeName(Ty, "%"+utostr(unsigned(NumberedTypes.size())));
+ NumberedTypes.push_back(Ty);
+ }
+
+ // Recursively walk all contained types.
+ for (Type::subtype_iterator I = Ty->subtype_begin(),
+ E = Ty->subtype_end(); I != E; ++I)
+ IncorporateType(*I);
+ }
+
+ /// IncorporateValue - This method is used to walk operand lists finding
+ /// types hiding in constant expressions and other operands that won't be
+ /// walked in other ways. GlobalValues, basic blocks, instructions, and
+ /// inst operands are all explicitly enumerated.
+ void IncorporateValue(const Value *V) {
+ if (V == 0 || !isa<Constant>(V) || isa<GlobalValue>(V)) return;
+
+ // Already visited?
+ if (!VisitedConstants.insert(V).second)
+ return;
+
+ // Check this type.
+ IncorporateType(V->getType());
+
+ // Look in operands for types.
+ const Constant *C = cast<Constant>(V);
+ for (Constant::const_op_iterator I = C->op_begin(),
+ E = C->op_end(); I != E;++I)
+ IncorporateValue(*I);
+ }
+ };
+} // end anonymous namespace
- // Restore the name.
- std::swap(OldName, I->second);
-}
-static void AddModuleTypesToPrinter(TypePrinting &TP, const Module *M) {
+/// AddModuleTypesToPrinter - Add all of the symbolic type names for types in
+/// the specified module to the TypePrinter and all numbered types to it and the
+/// NumberedTypes table.
+static void AddModuleTypesToPrinter(TypePrinting &TP,
+ std::vector<const Type*> &NumberedTypes,
+ const Module *M) {
if (M == 0) return;
// If the module has a symbol table, take all global types and stuff their
PrintLLVMName(NameOS, TI->first.c_str(), TI->first.length(), LocalPrefix);
TP.addTypeName(Ty, NameOS.str());
}
+
+ // Walk the entire module to find references to unnamed structure and opaque
+ // types. This is required for correctness by opaque types (because multiple
+ // uses of an unnamed opaque type needs to be referred to by the same ID) and
+ // it shrinks complex recursive structure types substantially in some cases.
+ TypeFinder(TP, NumberedTypes).Run(*M);
}
///
void llvm::WriteTypeSymbolic(raw_ostream &OS, const Type *Ty, const Module *M) {
TypePrinting Printer;
- AddModuleTypesToPrinter(Printer, M);
+ std::vector<const Type*> NumberedTypes;
+ AddModuleTypesToPrinter(Printer, NumberedTypes, M);
Printer.print(Ty, OS);
}
// Some form of long double. These appear as a magic letter identifying
// the type, then a fixed number of hex digits.
Out << "0x";
- if (&CFP->getValueAPF().getSemantics() == &APFloat::x87DoubleExtended)
+ if (&CFP->getValueAPF().getSemantics() == &APFloat::x87DoubleExtended) {
Out << 'K';
- else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEquad)
+ // api needed to prevent premature destruction
+ APInt api = CFP->getValueAPF().bitcastToAPInt();
+ const uint64_t* p = api.getRawData();
+ uint64_t word = p[1];
+ int shiftcount=12;
+ int width = api.getBitWidth();
+ for (int j=0; j<width; j+=4, shiftcount-=4) {
+ unsigned int nibble = (word>>shiftcount) & 15;
+ if (nibble < 10)
+ Out << (unsigned char)(nibble + '0');
+ else
+ Out << (unsigned char)(nibble - 10 + 'A');
+ if (shiftcount == 0 && j+4 < width) {
+ word = *p;
+ shiftcount = 64;
+ if (width-j-4 < 64)
+ shiftcount = width-j-4;
+ }
+ }
+ return;
+ } else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEquad)
Out << 'L';
else if (&CFP->getValueAPF().getSemantics() == &APFloat::PPCDoubleDouble)
Out << 'M';
Out << "undef";
return;
}
+
+ if (const MDString *S = dyn_cast<MDString>(CV)) {
+ Out << "!\"";
+ PrintEscapedString(S->begin(), S->size(), Out);
+ Out << '"';
+ return;
+ }
if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Out << CE->getOpcodeName();
if (Context == 0) Context = getModuleFromVal(V);
TypePrinting TypePrinter;
- AddModuleTypesToPrinter(TypePrinter, Context);
+ std::vector<const Type*> NumberedTypes;
+ AddModuleTypesToPrinter(TypePrinter, NumberedTypes, Context);
if (PrintType) {
TypePrinter.print(V->getType(), Out);
Out << ' ';
const Module *TheModule;
TypePrinting TypePrinter;
AssemblyAnnotationWriter *AnnotationWriter;
+ std::vector<const Type*> NumberedTypes;
+
+ // Each MDNode is assigned unique MetadataIDNo.
+ std::map<const MDNode *, unsigned> MDNodes;
+ unsigned MetadataIDNo;
public:
inline AssemblyWriter(raw_ostream &o, SlotTracker &Mac, const Module *M,
AssemblyAnnotationWriter *AAW)
- : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW) {
- AddModuleTypesToPrinter(TypePrinter, M);
+ : Out(o), Machine(Mac), TheModule(M), AnnotationWriter(AAW), MetadataIDNo(0) {
+ AddModuleTypesToPrinter(TypePrinter, NumberedTypes, M);
}
- void write(const Module *M) { printModule(M); }
+ void write(const Module *M) { printModule(M); }
void write(const GlobalValue *G) {
if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(G))
void writeOperand(const Value *Op, bool PrintType);
void writeParamOperand(const Value *Operand, Attributes Attrs);
+ void printMDNode(const MDNode *Node, bool StandAlone);
const Module* getModule() { return TheModule; }
// which slot it occupies.
void printInfoComment(const Value &V);
};
-} // end of llvm namespace
+} // end of anonymous namespace
void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
Out << " ]\n";
}
- // Loop over the symbol table, emitting all named constants.
+ // Loop over the symbol table, emitting all id'd types.
printTypeSymbolTable(M->getTypeSymbolTable());
for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
static void PrintLinkage(GlobalValue::LinkageTypes LT, raw_ostream &Out) {
switch (LT) {
- case GlobalValue::PrivateLinkage: Out << "private "; break;
- case GlobalValue::InternalLinkage: Out << "internal "; break;
- case GlobalValue::LinkOnceLinkage: Out << "linkonce "; break;
- case GlobalValue::WeakLinkage: Out << "weak "; break;
- case GlobalValue::CommonLinkage: Out << "common "; break;
- case GlobalValue::AppendingLinkage: Out << "appending "; break;
- case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
- case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
- case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
+ case GlobalValue::PrivateLinkage: Out << "private "; break;
+ case GlobalValue::InternalLinkage: Out << "internal "; break;
+ case GlobalValue::AvailableExternallyLinkage:
+ Out << "available_externally ";
+ break;
+ case GlobalValue::LinkOnceAnyLinkage: Out << "linkonce "; break;
+ case GlobalValue::LinkOnceODRLinkage: Out << "linkonce_odr "; break;
+ case GlobalValue::WeakAnyLinkage: Out << "weak "; break;
+ case GlobalValue::WeakODRLinkage: Out << "weak_odr "; break;
+ case GlobalValue::CommonLinkage: Out << "common "; break;
+ case GlobalValue::AppendingLinkage: Out << "appending "; break;
+ case GlobalValue::DLLImportLinkage: Out << "dllimport "; break;
+ case GlobalValue::DLLExportLinkage: Out << "dllexport "; break;
+ case GlobalValue::ExternalWeakLinkage: Out << "extern_weak "; break;
case GlobalValue::ExternalLinkage: break;
case GlobalValue::GhostLinkage:
Out << "GhostLinkage not allowed in AsmWriter!\n";
abort();
}
}
-
+
static void PrintVisibility(GlobalValue::VisibilityTypes Vis,
raw_ostream &Out) {
}
void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
+ if (GV->hasInitializer())
+ // If GV is initialized using Metadata then separate out metadata
+ // operands used by the initializer. Note, MDNodes are not cyclic.
+ if (MDNode *N = dyn_cast<MDNode>(GV->getInitializer())) {
+ SmallVector<const MDNode *, 4> WorkList;
+ // Collect MDNodes used by the initializer.
+ for (MDNode::const_elem_iterator I = N->elem_begin(), E = N->elem_end();
+ I != E; ++I) {
+ const Value *TV = *I;
+ if (TV)
+ if (const MDNode *NN = dyn_cast<MDNode>(TV))
+ WorkList.push_back(NN);
+ }
+
+ // Print MDNodes used by the initializer.
+ while (!WorkList.empty()) {
+ const MDNode *N = WorkList.back(); WorkList.pop_back();
+ printMDNode(N, true);
+ Out << '\n';
+ }
+ }
+
if (GV->hasName()) {
PrintLLVMName(Out, GV);
Out << " = ";
if (GV->hasInitializer()) {
Out << ' ';
- writeOperand(GV->getInitializer(), false);
+ if (MDNode *N = dyn_cast<MDNode>(GV->getInitializer()))
+ printMDNode(N, false);
+ else
+ writeOperand(GV->getInitializer(), false);
}
if (GV->hasSection())
Out << '\n';
}
+void AssemblyWriter::printMDNode(const MDNode *Node,
+ bool StandAlone) {
+ std::map<const MDNode *, unsigned>::iterator MI = MDNodes.find(Node);
+ // If this node is already printed then just refer it using its Metadata
+ // id number.
+ if (MI != MDNodes.end()) {
+ if (!StandAlone)
+ Out << "!" << MI->second;
+ return;
+ }
+
+ if (StandAlone) {
+ // Print standalone MDNode.
+ // !42 = !{ ... }
+ Out << "!" << MetadataIDNo << " = ";
+ Out << "constant metadata ";
+ }
+
+ Out << "!{";
+ for (MDNode::const_elem_iterator I = Node->elem_begin(), E = Node->elem_end();
+ I != E;) {
+ const Value *TV = *I;
+ if (!TV)
+ Out << "null";
+ else if (const MDNode *N = dyn_cast<MDNode>(TV)) {
+ TypePrinter.print(N->getType(), Out);
+ Out << ' ';
+ printMDNode(N, StandAlone);
+ }
+ else if (!*I)
+ Out << "null";
+ else
+ writeOperand(*I, true);
+ if (++I != E)
+ Out << ", ";
+ }
+ Out << "}";
+
+ MDNodes[Node] = MetadataIDNo++;
+}
+
void AssemblyWriter::printAlias(const GlobalAlias *GA) {
// Don't crash when dumping partially built GA
if (!GA->hasName())
Out << ' ';
PrintLLVMName(Out, GA);
} else {
- const ConstantExpr *CE = 0;
- if ((CE = dyn_cast<ConstantExpr>(Aliasee)) &&
- (CE->getOpcode() == Instruction::BitCast)) {
- writeOperand(CE, false);
- } else
- assert(0 && "Unsupported aliasee");
+ const ConstantExpr *CE = cast<ConstantExpr>(Aliasee);
+ // The only valid GEP is an all zero GEP.
+ assert((CE->getOpcode() == Instruction::BitCast ||
+ CE->getOpcode() == Instruction::GetElementPtr) &&
+ "Unsupported aliasee");
+ writeOperand(CE, false);
}
printInfoComment(*GA);
}
void AssemblyWriter::printTypeSymbolTable(const TypeSymbolTable &ST) {
- // Print the types.
+ // Emit all numbered types.
+ for (unsigned i = 0, e = NumberedTypes.size(); i != e; ++i) {
+ Out << "\ttype ";
+
+ // Make sure we print out at least one level of the type structure, so
+ // that we do not get %2 = type %2
+ TypePrinter.printAtLeastOneLevel(NumberedTypes[i], Out);
+ Out << "\t\t; type %" << i << '\n';
+ }
+
+ // Print the named types.
for (TypeSymbolTable::const_iterator TI = ST.begin(), TE = ST.end();
TI != TE; ++TI) {
Out << '\t';
case CallingConv::Fast: Out << "fastcc "; break;
case CallingConv::Cold: Out << "coldcc "; break;
case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
- case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
+ case CallingConv::X86_FastCall: Out << "x86_fastcallcc "; break;
+ case CallingConv::ARM_APCS: Out << "arm_apcscc "; break;
+ case CallingConv::ARM_AAPCS: Out << "arm_aapcscc "; break;
+ case CallingConv::ARM_AAPCS_VFP:Out << "arm_aapcs_vfpcc "; break;
default: Out << "cc" << F->getCallingConv() << " "; break;
}
case CallingConv::Fast: Out << " fastcc"; break;
case CallingConv::Cold: Out << " coldcc"; break;
case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break;
- case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break;
+ case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break;
+ case CallingConv::ARM_APCS: Out << " arm_apcscc "; break;
+ case CallingConv::ARM_AAPCS: Out << " arm_aapcscc "; break;
+ case CallingConv::ARM_AAPCS_VFP:Out << " arm_aapcs_vfpcc "; break;
default: Out << " cc" << CI->getCallingConv(); break;
}
case CallingConv::Cold: Out << " coldcc"; break;
case CallingConv::X86_StdCall: Out << " x86_stdcallcc"; break;
case CallingConv::X86_FastCall: Out << " x86_fastcallcc"; break;
+ case CallingConv::ARM_APCS: Out << " arm_apcscc "; break;
+ case CallingConv::ARM_AAPCS: Out << " arm_aapcscc "; break;
+ case CallingConv::ARM_AAPCS_VFP:Out << " arm_aapcs_vfpcc "; break;
default: Out << " cc" << II->getCallingConv(); break;
}
W.write(BB);
} else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
SlotTracker SlotTable(GV->getParent());
- AssemblyWriter W(OS, SlotTable, GV->getParent(), 0);
+ AssemblyWriter W(OS, SlotTable, GV->getParent(), AAW);
W.write(GV);
+ } else if (const MDNode *N = dyn_cast<MDNode>(this)) {
+ TypePrinting TypePrinter;
+ TypePrinter.print(N->getType(), OS);
+ OS << ' ';
+ // FIXME: Do we need a slot tracker for metadata ?
+ SlotTracker SlotTable((const Function *)NULL);
+ AssemblyWriter W(OS, SlotTable, NULL, AAW);
+ W.printMDNode(N, false);
} else if (const Constant *C = dyn_cast<Constant>(this)) {
TypePrinting TypePrinter;
TypePrinter.print(C->getType(), OS);
}
// Value::dump - allow easy printing of Values from the debugger.
-void Value::dump() const { print(errs()); errs() << '\n'; errs().flush(); }
+void Value::dump() const { print(errs()); errs() << '\n'; }
// Type::dump - allow easy printing of Types from the debugger.
// This one uses type names from the given context module
void Type::dump(const Module *Context) const {
WriteTypeSymbolic(errs(), this, Context);
errs() << '\n';
- errs().flush();
}
// Type::dump - allow easy printing of Types from the debugger.
void Type::dump() const { dump(0); }
-
// Module::dump() - Allow printing of Modules from the debugger.
-void Module::dump() const { print(errs(), 0); errs().flush(); }
-
-
+void Module::dump() const { print(errs(), 0); }