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;
+ }
}
!cast<PointerType>(Ty)->getElementType()->isPrimitiveType() ||
!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)));
}
}
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);
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()) {
// 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())
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);
// 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()) ||
// 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;
}