#include "llvm/Instruction.h"
#include "llvm/Instructions.h"
#include "llvm/Module.h"
-#include "llvm/SymbolTable.h"
+#include "llvm/ValueSymbolTable.h"
#include "llvm/TypeSymbolTable.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/STLExtras.h"
return 0;
}
-// getLLVMName - Turn the specified string into an 'LLVM name', which is either
-// prefixed with % (if the string only contains simple characters) or is
-// surrounded with ""'s (if it has special chars in it).
-static std::string getLLVMName(const std::string &Name,
- bool prefixName = true) {
- assert(!Name.empty() && "Cannot get empty name!");
-
- // First character cannot start with a number...
- if (Name[0] >= '0' && Name[0] <= '9')
- return "\"" + Name + "\"";
-
+/// NameNeedsQuotes - Return true if the specified llvm name should be wrapped
+/// with ""'s.
+static bool NameNeedsQuotes(const std::string &Name) {
+ if (Name[0] >= '0' && Name[0] <= '9') return true;
// Scan to see if we have any characters that are not on the "white list"
for (unsigned i = 0, e = Name.size(); i != e; ++i) {
char C = Name[i];
assert(C != '"' && "Illegal character in LLVM value name!");
if ((C < 'a' || C > 'z') && (C < 'A' || C > 'Z') && (C < '0' || C > '9') &&
C != '-' && C != '.' && C != '_')
- return "\"" + Name + "\"";
+ return true;
+ }
+ return false;
+}
+
+enum PrefixType {
+ GlobalPrefix,
+ LabelPrefix,
+ LocalPrefix
+};
+
+/// getLLVMName - Turn the specified string into an 'LLVM name', which is either
+/// prefixed with % (if the string only contains simple characters) or is
+/// surrounded with ""'s (if it has special chars in it).
+static std::string getLLVMName(const std::string &Name, PrefixType Prefix) {
+ assert(!Name.empty() && "Cannot get empty name!");
+
+ // First character cannot start with a number...
+ if (NameNeedsQuotes(Name)) {
+ if (Prefix == GlobalPrefix)
+ return "@\"" + Name + "\"";
+ return "\"" + Name + "\"";
}
// If we get here, then the identifier is legal to use as a "VarID".
- if (prefixName)
- return "%"+Name;
- else
- return Name;
+ switch (Prefix) {
+ default: assert(0 && "Bad prefix!");
+ case GlobalPrefix: return '@' + Name;
+ case LabelPrefix: return Name;
+ case LocalPrefix: return '%' + Name;
+ }
}
const Type *Ty = cast<Type>(TI->second);
if (!isa<PointerType>(Ty) ||
!cast<PointerType>(Ty)->getElementType()->isPrimitiveType() ||
- !cast<PointerType>(Ty)->getElementType()->isIntegral() ||
+ !cast<PointerType>(Ty)->getElementType()->isInteger() ||
isa<OpaqueType>(cast<PointerType>(Ty)->getElementType()))
- TypeNames.insert(std::make_pair(Ty, getLLVMName(TI->first)));
+ TypeNames.insert(std::make_pair(Ty, getLLVMName(TI->first, LocalPrefix)));
}
}
std::vector<const Type *> &TypeStack,
std::map<const Type *, std::string> &TypeNames,
std::string & Result){
- if (Ty->isIntegral() || (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))) {
+ if (Ty->isInteger() || (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))) {
Result += Ty->getDescription(); // Base case
return;
}
Result += "]";
break;
}
- case Type::PackedTyID: {
- const PackedType *PTy = cast<PackedType>(Ty);
+ case Type::VectorTyID: {
+ const VectorType *PTy = cast<VectorType>(Ty);
Result += "<" + utostr(PTy->getNumElements()) + " x ";
calcTypeName(PTy->getElementType(), TypeStack, TypeNames, Result);
Result += ">";
// Primitive types always print out their description, regardless of whether
// they have been named or not.
//
- if (Ty->isIntegral() || (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty)))
+ if (Ty->isInteger() || (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty)))
return Out << Ty->getDescription();
// Check to see if the type is named.
if (CI->getType() == Type::Int1Ty)
Out << (CI->getZExtValue() ? "true" : "false");
else
- Out << CI->getSExtValue();
+ Out << CI->getValue().toStringSigned(10);
} else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
// We would like to output the FP constant value in exponential notation,
// but we cannot do this if doing so will lose precision. Check here to
Out << " }";
if (CS->getType()->isPacked())
Out << '>';
- } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
+ } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
const Type *ETy = CP->getType()->getElementType();
assert(CP->getNumOperands() > 0 &&
"Number of operands for a PackedConst must be > 0");
SlotMachine *Machine) {
Out << ' ';
if (V->hasName())
- Out << getLLVMName(V->getName());
+ Out << getLLVMName(V->getName(),
+ isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
else {
const Constant *CV = dyn_cast<Constant>(V);
if (CV && !isa<GlobalValue>(CV)) {
PrintEscapedString(IA->getConstraintString(), Out);
Out << '"';
} else {
+ char Prefix = '%';
int Slot;
if (Machine) {
- if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
+ if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Slot = Machine->getGlobalSlot(GV);
- else
+ Prefix = '@';
+ } else {
Slot = Machine->getLocalSlot(V);
+ }
} else {
Machine = createSlotMachine(V);
if (Machine) {
- if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
+ if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Slot = Machine->getGlobalSlot(GV);
- else
+ Prefix = '@';
+ } else {
Slot = Machine->getLocalSlot(V);
+ }
} else {
Slot = -1;
}
delete Machine;
}
if (Slot != -1)
- Out << '%' << Slot;
+ Out << Prefix << Slot;
else
Out << "<badref>";
}
inline void write(const Function *F) { printFunction(F); }
inline void write(const BasicBlock *BB) { printBasicBlock(BB); }
inline void write(const Instruction *I) { printInstruction(*I); }
- inline void write(const Constant *CPV) { printConstant(CPV); }
inline void write(const Type *Ty) { printType(Ty); }
void writeOperand(const Value *Op, bool PrintType);
private:
void printModule(const Module *M);
void printTypeSymbolTable(const TypeSymbolTable &ST);
- void printValueSymbolTable(const SymbolTable &ST);
- void printConstant(const Constant *CPV);
void printGlobal(const GlobalVariable *GV);
void printFunction(const Function *F);
void printArgument(const Argument *FA, FunctionType::ParameterAttributes A);
} else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Out << '[' << ATy->getNumElements() << " x ";
printType(ATy->getElementType()) << ']';
- } else if (const PackedType *PTy = dyn_cast<PackedType>(Ty)) {
+ } else if (const VectorType *PTy = dyn_cast<VectorType>(Ty)) {
Out << '<' << PTy->getNumElements() << " x ";
printType(PTy->getElementType()) << '>';
}
if (!M->getDataLayout().empty())
Out << "target datalayout = \"" << M->getDataLayout() << "\"\n";
-
- switch (M->getEndianness()) {
- case Module::LittleEndian: Out << "target endian = little\n"; break;
- case Module::BigEndian: Out << "target endian = big\n"; break;
- case Module::AnyEndianness: break;
- }
- switch (M->getPointerSize()) {
- case Module::Pointer32: Out << "target pointersize = 32\n"; break;
- case Module::Pointer64: Out << "target pointersize = 64\n"; break;
- case Module::AnyPointerSize: break;
- }
if (!M->getTargetTriple().empty())
Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
// Loop over the symbol table, emitting all named constants.
printTypeSymbolTable(M->getTypeSymbolTable());
- printValueSymbolTable(M->getValueSymbolTable());
for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
I != E; ++I)
}
void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
- if (GV->hasName()) Out << getLLVMName(GV->getName()) << " = ";
+ if (GV->hasName()) Out << getLLVMName(GV->getName(), GlobalPrefix) << " = ";
if (!GV->hasInitializer())
switch (GV->getLinkage()) {
abort();
}
switch (GV->getVisibility()) {
+ default: assert(0 && "Invalid visibility style!");
case GlobalValue::DefaultVisibility: break;
case GlobalValue::HiddenVisibility: Out << "hidden "; break;
- default:
- cerr << "Invalid visibility style!\n";
- abort();
}
}
// Print the types.
for (TypeSymbolTable::const_iterator TI = ST.begin(), TE = ST.end();
TI != TE; ++TI) {
- Out << "\t" << getLLVMName(TI->first) << " = type ";
+ Out << "\t" << getLLVMName(TI->first, LocalPrefix) << " = type ";
// Make sure we print out at least one level of the type structure, so
// that we do not get %FILE = type %FILE
}
}
-// printSymbolTable - Run through symbol table looking for constants
-// and types. Emit their declarations.
-void AssemblyWriter::printValueSymbolTable(const SymbolTable &ST) {
-
- // Print the constants, in type plane order.
- for (SymbolTable::plane_const_iterator PI = ST.plane_begin();
- PI != ST.plane_end(); ++PI) {
- SymbolTable::value_const_iterator VI = ST.value_begin(PI->first);
- SymbolTable::value_const_iterator VE = ST.value_end(PI->first);
-
- for (; VI != VE; ++VI) {
- const Value* V = VI->second;
- const Constant *CPV = dyn_cast<Constant>(V) ;
- if (CPV && !isa<GlobalValue>(V)) {
- printConstant(CPV);
- }
- }
- }
-}
-
-
-/// printConstant - Print out a constant pool entry...
-///
-void AssemblyWriter::printConstant(const Constant *CPV) {
- // Don't print out unnamed constants, they will be inlined
- if (!CPV->hasName()) return;
-
- // Print out name...
- Out << "\t" << getLLVMName(CPV->getName()) << " =";
-
- // Write the value out now.
- writeOperand(CPV, true);
-
- printInfoComment(*CPV);
- Out << "\n";
-}
-
/// printFunction - Print all aspects of a function.
///
void AssemblyWriter::printFunction(const Function *F) {
// Print out the return type and name...
Out << "\n";
- // Ensure that no local symbols conflict with global symbols.
- const_cast<Function*>(F)->renameLocalSymbols();
-
if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
- if (F->isExternal())
+ if (F->isDeclaration())
switch (F->getLinkage()) {
case GlobalValue::DLLImportLinkage: Out << "declare dllimport "; break;
case GlobalValue::ExternalWeakLinkage: Out << "declare extern_weak "; break;
abort();
}
switch (F->getVisibility()) {
+ default: assert(0 && "Invalid visibility style!");
case GlobalValue::DefaultVisibility: break;
case GlobalValue::HiddenVisibility: Out << "hidden "; break;
- default:
- cerr << "Invalid visibility style!\n";
- abort();
}
}
// Print the calling convention.
switch (F->getCallingConv()) {
case CallingConv::C: break; // default
- case CallingConv::CSRet: Out << "csretcc "; break;
case CallingConv::Fast: Out << "fastcc "; break;
case CallingConv::Cold: Out << "coldcc "; break;
case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
const FunctionType *FT = F->getFunctionType();
printType(F->getReturnType()) << ' ';
if (!F->getName().empty())
- Out << getLLVMName(F->getName());
+ Out << getLLVMName(F->getName(), GlobalPrefix);
else
- Out << "\"\"";
+ Out << "@\"\"";
Out << '(';
Machine.incorporateFunction(F);
if (F->getAlignment())
Out << " align " << F->getAlignment();
- if (F->isExternal()) {
+ if (F->isDeclaration()) {
Out << "\n";
} else {
Out << " {";
// Output name, if available...
if (Arg->hasName())
- Out << ' ' << getLLVMName(Arg->getName());
+ Out << ' ' << getLLVMName(Arg->getName(), LocalPrefix);
}
/// printBasicBlock - This member is called for each basic block in a method.
///
void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
if (BB->hasName()) { // Print out the label if it exists...
- Out << "\n" << getLLVMName(BB->getName(), false) << ':';
+ Out << "\n" << getLLVMName(BB->getName(), LabelPrefix) << ':';
} else if (!BB->use_empty()) { // Don't print block # of no uses...
Out << "\n; <label>:";
int Slot = Machine.getLocalSlot(BB);
// Print out name if it exists...
if (I.hasName())
- Out << getLLVMName(I.getName()) << " = ";
+ Out << getLLVMName(I.getName(), LocalPrefix) << " = ";
// If this is a volatile load or store, print out the volatile marker.
if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
// Print the calling convention being used.
switch (CI->getCallingConv()) {
case CallingConv::C: break; // default
- case CallingConv::CSRet: Out << " csretcc"; break;
case CallingConv::Fast: Out << " fastcc"; break;
case CallingConv::Cold: Out << " coldcc"; break;
case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
// Print the calling convention being used.
switch (II->getCallingConv()) {
case CallingConv::C: break; // default
- case CallingConv::CSRet: Out << " csretcc"; break;
case CallingConv::Fast: Out << " fastcc"; break;
case CallingConv::Cold: Out << " coldcc"; break;
case CallingConv::X86_StdCall: Out << "x86_stdcallcc "; break;
bool PrintAllTypes = false;
const Type *TheType = Operand->getType();
- // Shift Left & Right print both types even for Ubyte LHS, and select prints
- // types even if all operands are bools.
- if (isa<ShiftInst>(I) || isa<SelectInst>(I) || isa<StoreInst>(I) ||
- isa<ShuffleVectorInst>(I)) {
+ // Select, Store and ShuffleVector always print all types.
+ if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)) {
PrintAllTypes = true;
} else {
for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
// Lookup the Value in the function and module maps.
ValueMap::const_iterator FVI = FI->second.map.find(V);
- TypedPlanes::const_iterator MI = mMap.find(VTy);
// If the value doesn't exist in the function map, it is a <badref>
if (FVI == FI->second.map.end()) return -1;
- // Return the slot number as the module's contribution to
- // the type plane plus the index in the function's contribution
- // to the type plane.
- if (MI != mMap.end())
- return MI->second.next_slot + FVI->second;
- else
- return FVI->second;
+ return FVI->second;
}