Use SequenceToOffsetTable in emitRegisterNameString.
authorJakob Stoklund Olesen <stoklund@2pi.dk>
Fri, 30 Mar 2012 21:12:52 +0000 (21:12 +0000)
committerJakob Stoklund Olesen <stoklund@2pi.dk>
Fri, 30 Mar 2012 21:12:52 +0000 (21:12 +0000)
This allows suffix sharing in register names. (AX is a suffix of EAX).

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@153777 91177308-0d34-0410-b5e6-96231b3b80d8

utils/TableGen/AsmWriterEmitter.cpp
utils/TableGen/SequenceToOffsetTable.h

index c4812dcae6d5cf4ca55d9a3b27ab8d1c8fcc988e..f840709c99ac2993f55e7a132fe1f7534e5f1e20 100644 (file)
@@ -16,6 +16,7 @@
 #include "AsmWriterInst.h"
 #include "CodeGenTarget.h"
 #include "StringToOffsetTable.h"
+#include "SequenceToOffsetTable.h"
 #include "llvm/ADT/Twine.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/MathExtras.h"
@@ -462,12 +463,12 @@ void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
 static void
 emitRegisterNameString(raw_ostream &O, StringRef AltName,
   const std::vector<CodeGenRegister*> &Registers) {
-  StringToOffsetTable StringTable;
-  O << "  static const unsigned RegAsmOffset" << AltName << "[] = {\n    ";
+  SequenceToOffsetTable<std::string> StringTable;
+  SmallVector<std::string, 4> AsmNames(Registers.size());
   for (unsigned i = 0, e = Registers.size(); i != e; ++i) {
     const CodeGenRegister &Reg = *Registers[i];
+    std::string &AsmName = AsmNames[i];
 
-    std::string AsmName;
     // "NoRegAltName" is special. We don't need to do a lookup for that,
     // as it's just a reference to the default register name.
     if (AltName == "" || AltName == "NoRegAltName") {
@@ -495,8 +496,17 @@ emitRegisterNameString(raw_ostream &O, StringRef AltName,
         AsmName = AltNames[Idx];
       }
     }
+    StringTable.add(AsmName);
+  }
+
+  StringTable.layout();
+  O << "  static const char AsmStrs" << AltName << "[] = {\n";
+  StringTable.emit(O, printChar);
+  O << "  };\n\n";
 
-    O << StringTable.GetOrAddStringOffset(AsmName);
+  O << "  static const unsigned RegAsmOffset" << AltName << "[] = {\n    ";
+  for (unsigned i = 0, e = Registers.size(); i != e; ++i) {
+    O << StringTable.get(AsmNames[i]);
     if (((i + 1) % 14) == 0)
       O << ",\n    ";
     else
@@ -506,10 +516,6 @@ emitRegisterNameString(raw_ostream &O, StringRef AltName,
   O << "0\n"
     << "  };\n"
     << "\n";
-
-  O << "  const char *AsmStrs" << AltName << " =\n";
-  StringTable.EmitString(O);
-  O << ";\n";
 }
 
 void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
index 09dccbb1084affb3da75847eecdde3c8d9086de2..97c764e61d564c3a05b7ec8632fae5c16ac65d1e 100644 (file)
@@ -21,6 +21,7 @@
 #include <algorithm>
 #include <vector>
 #include <cassert>
+#include <cctype>
 
 namespace llvm {
 
@@ -120,6 +121,19 @@ public:
   }
 };
 
+// Helper function for SequenceToOffsetTable<string>.
+static inline void printChar(raw_ostream &OS, char C) {
+  unsigned char UC(C);
+  if (isalnum(UC) || ispunct(UC)) {
+    OS << '\'';
+    if (C == '\\' || C == '\'')
+      OS << '\\';
+    OS << C << '\'';
+  } else {
+    OS << unsigned(UC);
+  }
+}
+
 } // end namespace llvm
 
 #endif