+/// PrintLLVMName - Turn the specified name 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). Print it out.
+static void PrintLLVMName(raw_ostream &OS, const char *NameStr,
+ unsigned NameLen, PrefixType Prefix) {
+ assert(NameStr && "Cannot get empty name!");
+ switch (Prefix) {
+ default: assert(0 && "Bad prefix!");
+ case NoPrefix: break;
+ case GlobalPrefix: OS << '@'; break;
+ case LabelPrefix: break;
+ case LocalPrefix: OS << '%'; break;
+ }
+
+ // Scan the name to see if it needs quotes first.
+ bool NeedsQuotes = NameStr[0] >= '0' && NameStr[0] <= '9';
+ if (!NeedsQuotes) {
+ for (unsigned i = 0; i != NameLen; ++i) {
+ char C = NameStr[i];
+ if (!isalnum(C) && C != '-' && C != '.' && C != '_') {
+ NeedsQuotes = true;
+ break;
+ }
+ }
+ }
+
+ // If we didn't need any quotes, just write out the name in one blast.
+ if (!NeedsQuotes) {
+ OS.write(NameStr, NameLen);
+ return;
+ }
+
+ // Okay, we need quotes. Output the quotes and escape any scary characters as
+ // needed.
+ OS << '"';
+ for (unsigned i = 0; i != NameLen; ++i) {
+ char C = NameStr[i];
+ if (C == '\\') {
+ OS << "\\\\";
+ } else if (C != '"' && isprint(C)) {
+ OS << C;
+ } else {
+ OS << '\\';
+ OS << hexdigit((C >> 4) & 0x0F);
+ OS << hexdigit((C >> 0) & 0x0F);
+ }
+ }
+ OS << '"';
+}
+
+/// getLLVMName - Turn the specified string into an 'LLVM name', which is
+/// surrounded with ""'s and escaped if it has special chars in it.
+static std::string getLLVMName(const std::string &Name) {
+ assert(!Name.empty() && "Cannot get empty name!");
+ std::string result;
+ raw_string_ostream OS(result);
+ PrintLLVMName(OS, Name.c_str(), Name.length(), NoPrefix);
+ return OS.str();
+}
+
+/// PrintLLVMName - Turn the specified name 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). Print it out.
+static void PrintLLVMName(raw_ostream &OS, const Value *V) {
+ PrintLLVMName(OS, V->getNameStart(), V->getNameLen(),
+ isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
+}
+
+
+
+//===----------------------------------------------------------------------===//
+// SlotTracker Class: Enumerate slot numbers for unnamed values
+//===----------------------------------------------------------------------===//
+
+namespace {
+
+/// This class provides computation of slot numbers for LLVM Assembly writing.
+///
+class SlotTracker {