1From b42f9f2014ec49a22077b6610863d9341a74e142 Mon Sep 17 00:00:00 2001
2From: mephi42 <mephi42@gmail.com>
3Date: Fri, 17 Aug 2018 11:07:39 +0200
4Subject: [PATCH 7/7] capstone: generate *GenInsnNameMaps.inc
5
6---
7 lib/Target/SystemZ/CMakeLists.txt   |  1 +
8 utils/TableGen/InstrInfoEmitter.cpp | 29 +++++++++++++++++++++++++++++
9 utils/TableGen/TableGen.cpp         |  6 ++++++
10 utils/TableGen/TableGenBackends.h   |  1 +
11 4 files changed, 37 insertions(+)
12
13diff --git a/lib/Target/SystemZ/CMakeLists.txt b/lib/Target/SystemZ/CMakeLists.txt
14index 4b5d9c4a3b2..2c64e0a94b8 100644
15--- a/lib/Target/SystemZ/CMakeLists.txt
16+++ b/lib/Target/SystemZ/CMakeLists.txt
17@@ -7,6 +7,7 @@ tablegen(LLVM SystemZGenDAGISel.inc -gen-dag-isel)
18 tablegen(LLVM SystemZGenDisassemblerTables.inc -gen-disassembler)
19 tablegen(LLVM SystemZGenInstrInfo.inc -gen-instr-info)
20 tablegen(LLVM SystemZMappingInsn.inc -mapping-insn)
21+tablegen(LLVM SystemZGenInsnNameMaps.inc -gen-insn-name-maps)
22 tablegen(LLVM SystemZGenMCCodeEmitter.inc -gen-emitter)
23 tablegen(LLVM SystemZGenRegisterInfo.inc -gen-register-info)
24 tablegen(LLVM SystemZGenSubtargetInfo.inc -gen-subtarget)
25diff --git a/utils/TableGen/InstrInfoEmitter.cpp b/utils/TableGen/InstrInfoEmitter.cpp
26index 14ab1ea8a72..ccf8170ca62 100644
27--- a/utils/TableGen/InstrInfoEmitter.cpp
28+++ b/utils/TableGen/InstrInfoEmitter.cpp
29@@ -837,6 +837,35 @@ void EmitMappingInsn(RecordKeeper &RK, raw_ostream &OS) {
30        << "},\n";
31   }
32 }
33+
34+std::string GetMnemonic(const CodeGenInstruction *Inst) {
35+    std::string Mnemonic = Inst->AsmString;
36+
37+    for (size_t i = 0; i < Mnemonic.length(); i++) {
38+      if (Mnemonic[i] == '\t') {
39+        return Mnemonic.substr(0, i);
40+      }
41+    }
42+    return Mnemonic;
43+}
44+
45+void EmitInsnNameMaps(RecordKeeper &RK, raw_ostream &OS) {
46+  OS << "// This is auto-gen data for Capstone engine (www.capstone-engine.org)\n"
47+        "// By Nguyen Anh Quynh <aquynh@gmail.com>\n"
48+        "\n";
49+  CodeGenTarget Target(RK);
50+  std::map<std::string, std::string> M;
51+  for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
52+    if (Inst->TheDef->getValueAsBit("isPseudo") ||
53+        Inst->TheDef->getValueAsBit("isCodeGenOnly")) {
54+      continue;
55+    }
56+    M[GetPublicName(Inst)] = GetMnemonic(Inst);
57+  }
58+  for (auto &P : M) {
59+    OS << "\t{ " << P.first << ", \"" << P.second << "\" },\n";
60+  }
61+}
62 #endif
63
64 } // end llvm namespace
65diff --git a/utils/TableGen/TableGen.cpp b/utils/TableGen/TableGen.cpp
66index bbb4e860536..27c6603de5a 100644
67--- a/utils/TableGen/TableGen.cpp
68+++ b/utils/TableGen/TableGen.cpp
69@@ -28,6 +28,7 @@ enum ActionType {
70   GenRegisterInfo,
71   GenInstrInfo,
72   MappingInsn,
73+  GenInsnNameMaps,
74   GenInstrDocs,
75   GenAsmWriter,
76   GenAsmMatcher,
77@@ -68,6 +69,8 @@ namespace {
78                                "Generate instruction descriptions"),
79                     clEnumValN(MappingInsn, "mapping-insn",
80                                ""),
81+                    clEnumValN(GenInsnNameMaps, "gen-insn-name-maps",
82+                               ""),
83                     clEnumValN(GenInstrDocs, "gen-instr-docs",
84                                "Generate instruction documentation"),
85                     clEnumValN(GenCallingConv, "gen-callingconv",
86@@ -141,6 +144,9 @@ bool LLVMTableGenMain(raw_ostream &OS, RecordKeeper &Records) {
87   case MappingInsn:
88     EmitMappingInsn(Records, OS);
89     break;
90+  case GenInsnNameMaps:
91+    EmitInsnNameMaps(Records, OS);
92+    break;
93   case GenInstrDocs:
94     EmitInstrDocs(Records, OS);
95     break;
96diff --git a/utils/TableGen/TableGenBackends.h b/utils/TableGen/TableGenBackends.h
97index a41e46b1db0..5656e5be849 100644
98--- a/utils/TableGen/TableGenBackends.h
99+++ b/utils/TableGen/TableGenBackends.h
100@@ -76,6 +76,7 @@ void EmitDisassembler(RecordKeeper &RK, raw_ostream &OS);
101 void EmitFastISel(RecordKeeper &RK, raw_ostream &OS);
102 void EmitInstrInfo(RecordKeeper &RK, raw_ostream &OS);
103 void EmitMappingInsn(RecordKeeper &RK, raw_ostream &OS);
104+void EmitInsnNameMaps(RecordKeeper &RK, raw_ostream &OS);
105 void EmitInstrDocs(RecordKeeper &RK, raw_ostream &OS);
106 void EmitPseudoLowering(RecordKeeper &RK, raw_ostream &OS);
107 void EmitCompressInst(RecordKeeper &RK, raw_ostream &OS);
108--
1092.19.1
110
111