Debian Codegraph @

contrib/riscv_update/0004-fix-riscv-registerclass-array-of-genregisterinfo.patch

raw
1 From 66794c07a825d5149f72b5072d2de8ba95d5b284 Mon Sep 17 00:00:00 2001
2 From: fanfuqiang <[email protected]>
3 Date: Thu, 28 Feb 2019 21:17:27 +0800
4 Subject: [PATCH] fix riscv registerclass array of genregisterinfo
5
6 ---
7 llvm/0001-capstone-riscv-patchs.patch | 3161 +++++++++++++++++
8 ...for-generate-RISCV-port-inc-for-CAPS.patch | 905 +++++
9 llvm/0003-clear-old-patchs.patch | 1602 +++++++++
10 .../utils/TableGen/FixedLenDecoderEmitter.cpp | 1 -
11 llvm/utils/TableGen/RegisterInfoEmitter.cpp | 8 +-
12 5 files changed, 5672 insertions(+), 5 deletions(-)
13 create mode 100644 llvm/0001-capstone-riscv-patchs.patch
14 create mode 100644 llvm/0002-update-TableGen-for-generate-RISCV-port-inc-for-CAPS.patch
15 create mode 100644 llvm/0003-clear-old-patchs.patch
16
17 diff --git a/llvm/0001-capstone-riscv-patchs.patch b/llvm/0001-capstone-riscv-patchs.patch
18 new file mode 100644
19 index 000000000..eb3246814
20 --- /dev/null
21 +++ b/llvm/0001-capstone-riscv-patchs.patch
22 @@ -0,0 +1,3161 @@
23 +From 3373228170bbc2324d223bdeca761de3b4565508 Mon Sep 17 00:00:00 2001
24 +From: fanfuqiang <[email protected]>
25 +Date: Sun, 17 Feb 2019 06:08:44 +0800
26 +Subject: [PATCH] capstone riscv patchs
27 +
28 +---
29 + ...apstone-generate-GenRegisterInfo.inc.patch | 338 +++++++++++++
30 + ...pstone-generate-GenSubtargetInfo.inc.patch | 86 ++++
31 + ...3-capstone-generate-GenInstrInfo.inc.patch | 130 +++++
32 + ...e-generate-GenDisassemblerTables.inc.patch | 472 ++++++++++++++++++
33 + ...5-capstone-generate-GenAsmWriter.inc.patch | 225 +++++++++
34 + ...06-capstone-generate-MappingInsn.inc.patch | 174 +++++++
35 + ...apstone-generate-GenInsnNameMaps.inc.patch | 110 ++++
36 + llvm/lib/Target/RISCV/CMakeLists.txt | 2 +
37 + llvm/utils/TableGen/AsmWriterEmitter.cpp | 103 +++-
38 + llvm/utils/TableGen/AsmWriterInst.cpp | 4 +
39 + llvm/utils/TableGen/DisassemblerEmitter.cpp | 12 +-
40 + .../utils/TableGen/FixedLenDecoderEmitter.cpp | 398 ++++++++++++++-
41 + llvm/utils/TableGen/InstrInfoEmitter.cpp | 173 ++++++-
42 + llvm/utils/TableGen/RegisterInfoEmitter.cpp | 130 ++++-
43 + llvm/utils/TableGen/SubtargetEmitter.cpp | 28 +-
44 + llvm/utils/TableGen/TableGen.cpp | 12 +
45 + llvm/utils/TableGen/TableGenBackends.h | 2 +
46 + 17 files changed, 2349 insertions(+), 50 deletions(-)
47 + create mode 100644 llvm/0001-capstone-generate-GenRegisterInfo.inc.patch
48 + create mode 100644 llvm/0002-capstone-generate-GenSubtargetInfo.inc.patch
49 + create mode 100644 llvm/0003-capstone-generate-GenInstrInfo.inc.patch
50 + create mode 100644 llvm/0004-capstone-generate-GenDisassemblerTables.inc.patch
51 + create mode 100644 llvm/0005-capstone-generate-GenAsmWriter.inc.patch
52 + create mode 100644 llvm/0006-capstone-generate-MappingInsn.inc.patch
53 + create mode 100644 llvm/0007-capstone-generate-GenInsnNameMaps.inc.patch
54 +
55 +diff --git a/llvm/0001-capstone-generate-GenRegisterInfo.inc.patch b/llvm/0001-capstone-generate-GenRegisterInfo.inc.patch
56 +new file mode 100644
57 +index 000000000..b51aa515a
58 +--- /dev/null
59 ++++ b/llvm/0001-capstone-generate-GenRegisterInfo.inc.patch
60 +@@ -0,0 +1,338 @@
61 ++From 5d631cb16e7ba5dd0380ff1ee9dda192b1cdad18 Mon Sep 17 00:00:00 2001
62 ++From: mephi42 <[email protected]>
63 ++Date: Tue, 7 Aug 2018 17:02:40 +0200
64 ++Subject: [PATCH 1/7] capstone: generate *GenRegisterInfo.inc
65 ++
66 ++---
67 ++ utils/TableGen/RegisterInfoEmitter.cpp | 130 ++++++++++++++++++++++---
68 ++ 1 file changed, 115 insertions(+), 15 deletions(-)
69 ++
70 ++diff --git a/utils/TableGen/RegisterInfoEmitter.cpp b/utils/TableGen/RegisterInfoEmitter.cpp
71 ++index 49016cca799..6ebb7148b1b 100644
72 ++--- a/utils/TableGen/RegisterInfoEmitter.cpp
73 +++++ b/utils/TableGen/RegisterInfoEmitter.cpp
74 ++@@ -99,6 +99,12 @@ private:
75 ++
76 ++ } // end anonymous namespace
77 ++
78 +++#ifdef CAPSTONE
79 +++#define NAME_PREFIX Target.getName() << "_" <<
80 +++#else
81 +++#define NAME_PREFIX
82 +++#endif
83 +++
84 ++ // runEnums - Print out enum values for all of the registers.
85 ++ void RegisterInfoEmitter::runEnums(raw_ostream &OS,
86 ++ CodeGenTarget &Target, CodeGenRegBank &Bank) {
87 ++@@ -107,13 +113,22 @@ void RegisterInfoEmitter::runEnums(raw_ostream &OS,
88 ++ // Register enums are stored as uint16_t in the tables. Make sure we'll fit.
89 ++ assert(Registers.size() <= 0xffff && "Too many regs to fit in tables");
90 ++
91 +++#ifndef CAPSTONE
92 ++ StringRef Namespace = Registers.front().TheDef->getValueAsString("Namespace");
93 +++#endif
94 ++
95 ++ emitSourceFileHeader("Target Register Enum Values", OS);
96 ++
97 +++#ifdef CAPSTONE
98 +++ OS << "/* Capstone Disassembly Engine */\n"
99 +++ "/* By Nguyen Anh Quynh <[email protected]>, 2013-2015 */\n"
100 +++ "\n";
101 +++#endif
102 +++
103 ++ OS << "\n#ifdef GET_REGINFO_ENUM\n";
104 ++ OS << "#undef GET_REGINFO_ENUM\n\n";
105 ++
106 +++#ifndef CAPSTONE
107 ++ OS << "namespace llvm {\n\n";
108 ++
109 ++ OS << "class MCRegisterClass;\n"
110 ++@@ -122,16 +137,20 @@ void RegisterInfoEmitter::runEnums(raw_ostream &OS,
111 ++
112 ++ if (!Namespace.empty())
113 ++ OS << "namespace " << Namespace << " {\n";
114 ++- OS << "enum {\n NoRegister,\n";
115 +++#endif
116 +++
117 +++ OS << "enum {\n " << NAME_PREFIX "NoRegister,\n";
118 ++
119 ++ for (const auto &Reg : Registers)
120 ++- OS << " " << Reg.getName() << " = " << Reg.EnumValue << ",\n";
121 +++ OS << " " << NAME_PREFIX Reg.getName() << " = " << Reg.EnumValue << ",\n";
122 ++ assert(Registers.size() == Registers.back().EnumValue &&
123 ++ "Register enum value mismatch!");
124 ++- OS << " NUM_TARGET_REGS \t// " << Registers.size()+1 << "\n";
125 +++ OS << " " << NAME_PREFIX "NUM_TARGET_REGS \t// " << Registers.size()+1 << "\n";
126 ++ OS << "};\n";
127 +++#ifndef CAPSTONE
128 ++ if (!Namespace.empty())
129 ++ OS << "} // end namespace " << Namespace << "\n";
130 +++#endif
131 ++
132 ++ const auto &RegisterClasses = Bank.getRegClasses();
133 ++ if (!RegisterClasses.empty()) {
134 ++@@ -140,18 +159,29 @@ void RegisterInfoEmitter::runEnums(raw_ostream &OS,
135 ++ assert(RegisterClasses.size() <= 0xffff &&
136 ++ "Too many register classes to fit in tables");
137 ++
138 ++- OS << "\n// Register classes\n\n";
139 +++ OS << "\n// Register classes\n";
140 +++#ifndef CAPSTONE
141 +++ OS << "\n";
142 ++ if (!Namespace.empty())
143 ++ OS << "namespace " << Namespace << " {\n";
144 +++#endif
145 ++ OS << "enum {\n";
146 ++ for (const auto &RC : RegisterClasses)
147 ++- OS << " " << RC.getName() << "RegClassID"
148 +++ OS << " " << NAME_PREFIX RC.getName() << "RegClassID"
149 ++ << " = " << RC.EnumValue << ",\n";
150 ++- OS << "\n };\n";
151 +++#ifdef CAPSTONE
152 +++ OS
153 +++#else
154 +++ OS << "\n "
155 +++#endif
156 +++ << "};\n";
157 +++#ifndef CAPSTONE
158 ++ if (!Namespace.empty())
159 ++ OS << "} // end namespace " << Namespace << "\n\n";
160 +++#endif
161 ++ }
162 ++
163 +++#ifndef CAPSTONE
164 ++ const std::vector<Record*> &RegAltNameIndices = Target.getRegAltNameIndices();
165 ++ // If the only definition is the default NoRegAltName, we don't need to
166 ++ // emit anything.
167 ++@@ -182,8 +212,11 @@ void RegisterInfoEmitter::runEnums(raw_ostream &OS,
168 ++ if (!Namespace.empty())
169 ++ OS << "} // end namespace " << Namespace << "\n\n";
170 ++ }
171 +++#endif
172 ++
173 +++#ifndef CAPSTONE
174 ++ OS << "} // end namespace llvm\n\n";
175 +++#endif
176 ++ OS << "#endif // GET_REGINFO_ENUM\n\n";
177 ++ }
178 ++
179 ++@@ -830,7 +863,9 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
180 ++
181 ++ const auto &Regs = RegBank.getRegisters();
182 ++
183 +++#ifndef CAPSTONE
184 ++ auto &SubRegIndices = RegBank.getSubRegIndices();
185 +++#endif
186 ++ // The lists of sub-registers and super-registers go in the same array. That
187 ++ // allows us to share suffixes.
188 ++ typedef std::vector<const CodeGenRegister*> RegVec;
189 ++@@ -922,25 +957,40 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
190 ++ LaneMaskSeqs.layout();
191 ++ SubRegIdxSeqs.layout();
192 ++
193 +++#ifndef CAPSTONE
194 ++ OS << "namespace llvm {\n\n";
195 +++#endif
196 ++
197 ++ const std::string &TargetName = Target.getName();
198 ++
199 ++ // Emit the shared table of differential lists.
200 ++- OS << "extern const MCPhysReg " << TargetName << "RegDiffLists[] = {\n";
201 +++#ifdef CAPSTONE
202 +++ OS << "static"
203 +++#else
204 +++ OS << "extern"
205 +++#endif
206 +++ << " const MCPhysReg " << TargetName << "RegDiffLists[] = {\n";
207 ++ DiffSeqs.emit(OS, printDiff16);
208 ++ OS << "};\n\n";
209 ++
210 +++#ifndef CAPSTONE
211 ++ // Emit the shared table of regunit lane mask sequences.
212 ++ OS << "extern const LaneBitmask " << TargetName << "LaneMaskLists[] = {\n";
213 ++ LaneMaskSeqs.emit(OS, printMask, "LaneBitmask::getAll()");
214 ++ OS << "};\n\n";
215 +++#endif
216 ++
217 ++ // Emit the table of sub-register indexes.
218 ++- OS << "extern const uint16_t " << TargetName << "SubRegIdxLists[] = {\n";
219 +++#ifdef CAPSTONE
220 +++ OS << "static"
221 +++#else
222 +++ OS << "extern"
223 +++#endif
224 +++ << " const uint16_t " << TargetName << "SubRegIdxLists[] = {\n";
225 ++ SubRegIdxSeqs.emit(OS, printSubRegIndex);
226 ++ OS << "};\n\n";
227 ++
228 +++#ifndef CAPSTONE
229 ++ // Emit the table of sub-register index sizes.
230 ++ OS << "extern const MCRegisterInfo::SubRegCoveredBits "
231 ++ << TargetName << "SubRegIdxRanges[] = {\n";
232 ++@@ -950,14 +1000,22 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
233 ++ << Idx.getName() << "\n";
234 ++ }
235 ++ OS << "};\n\n";
236 +++#endif
237 ++
238 ++ // Emit the string table.
239 ++ RegStrings.layout();
240 +++#ifndef CAPSTONE
241 ++ OS << "extern const char " << TargetName << "RegStrings[] = {\n";
242 ++ RegStrings.emit(OS, printChar);
243 ++ OS << "};\n\n";
244 +++#endif
245 ++
246 ++- OS << "extern const MCRegisterDesc " << TargetName
247 +++#ifdef CAPSTONE
248 +++ OS << "static"
249 +++#else
250 +++ OS << "extern"
251 +++#endif
252 +++ << " const MCRegisterDesc " << TargetName
253 ++ << "RegDesc[] = { // Descriptors\n";
254 ++ OS << " { " << RegStrings.get("") << ", 0, 0, 0, 0, 0 },\n";
255 ++
256 ++@@ -973,6 +1031,7 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
257 ++ }
258 ++ OS << "};\n\n"; // End of register descriptors...
259 ++
260 +++#ifndef CAPSTONE
261 ++ // Emit the table of register unit roots. Each regunit has one or two root
262 ++ // registers.
263 ++ OS << "extern const MCPhysReg " << TargetName << "RegUnitRoots[][2] = {\n";
264 ++@@ -986,11 +1045,14 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
265 ++ OS << " },\n";
266 ++ }
267 ++ OS << "};\n\n";
268 +++#endif
269 ++
270 ++ const auto &RegisterClasses = RegBank.getRegClasses();
271 ++
272 ++ // Loop over all of the register classes... emitting each one.
273 +++#ifndef CAPSTONE
274 ++ OS << "namespace { // Register classes...\n";
275 +++#endif
276 ++
277 ++ SequenceToOffsetTable<std::string> RegClassStrings;
278 ++
279 ++@@ -1005,15 +1067,28 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
280 ++
281 ++ // Emit the register list now.
282 ++ OS << " // " << Name << " Register Class...\n"
283 ++- << " const MCPhysReg " << Name
284 +++ << " "
285 +++#ifdef CAPSTONE
286 +++ << "static "
287 +++#endif
288 +++ << "const MCPhysReg " << Name
289 ++ << "[] = {\n ";
290 ++ for (Record *Reg : Order) {
291 ++- OS << getQualifiedName(Reg) << ", ";
292 +++#ifdef CAPSTONE
293 +++ OS << NAME_PREFIX Reg->getName()
294 +++#else
295 +++ OS << getQualifiedName(Reg)
296 +++#endif
297 +++ << ", ";
298 ++ }
299 ++ OS << "\n };\n\n";
300 ++
301 ++ OS << " // " << Name << " Bit set.\n"
302 ++- << " const uint8_t " << Name
303 +++ << " "
304 +++#ifdef CAPSTONE
305 +++ << "static "
306 +++#endif
307 +++ << "const uint8_t " << Name
308 ++ << "Bits[] = {\n ";
309 ++ BitVectorEmitter BVE;
310 ++ for (Record *Reg : Order) {
311 ++@@ -1023,14 +1098,23 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
312 ++ OS << "\n };\n\n";
313 ++
314 ++ }
315 +++#ifndef CAPSTONE
316 ++ OS << "} // end anonymous namespace\n\n";
317 +++#endif
318 ++
319 ++ RegClassStrings.layout();
320 +++#ifndef CAPSTONE
321 ++ OS << "extern const char " << TargetName << "RegClassStrings[] = {\n";
322 ++ RegClassStrings.emit(OS, printChar);
323 ++ OS << "};\n\n";
324 +++#endif
325 ++
326 ++- OS << "extern const MCRegisterClass " << TargetName
327 +++#ifdef CAPSTONE
328 +++ OS << "static"
329 +++#else
330 +++ OS << "extern"
331 +++#endif
332 +++ << " const MCRegisterClass " << TargetName
333 ++ << "MCRegisterClasses[] = {\n";
334 ++
335 ++ for (const auto &RC : RegisterClasses) {
336 ++@@ -1041,7 +1125,12 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
337 ++ OS << " { " << RC.getName() << ", " << RC.getName() << "Bits, "
338 ++ << RegClassStrings.get(RC.getName()) << ", "
339 ++ << RC.getOrder().size() << ", sizeof(" << RC.getName() << "Bits), "
340 ++- << RC.getQualifiedName() + "RegClassID" << ", "
341 +++#ifdef CAPSTONE
342 +++ << NAME_PREFIX RC.getName()
343 +++#else
344 +++ << RC.getQualifiedName()
345 +++#endif
346 +++ << "RegClassID" << ", "
347 ++ << RegSize/8 << ", "
348 ++ << RC.CopyCost << ", "
349 ++ << ( RC.Allocatable ? "true" : "false" ) << " },\n";
350 ++@@ -1049,6 +1138,7 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
351 ++
352 ++ OS << "};\n\n";
353 ++
354 +++#ifndef CAPSTONE
355 ++ EmitRegMappingTables(OS, Regs, false);
356 ++
357 ++ // Emit Reg encoding table
358 ++@@ -1067,7 +1157,9 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
359 ++ OS << " " << Value << ",\n";
360 ++ }
361 ++ OS << "};\n"; // End of HW encoding table
362 +++#endif
363 ++
364 +++#ifndef CAPSTONE
365 ++ // MCRegisterInfo initialization routine.
366 ++ OS << "static inline void Init" << TargetName
367 ++ << "MCRegisterInfo(MCRegisterInfo *RI, unsigned RA, "
368 ++@@ -1088,7 +1180,12 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
369 ++ OS << "}\n\n";
370 ++
371 ++ OS << "} // end namespace llvm\n\n";
372 ++- OS << "#endif // GET_REGINFO_MC_DESC\n\n";
373 +++#endif
374 +++ OS << "#endif // GET_REGINFO_MC_DESC\n"
375 +++#ifndef CAPSTONE
376 +++ << "\n"
377 +++#endif
378 +++ ;
379 ++ }
380 ++
381 ++ void
382 ++@@ -1568,10 +1665,13 @@ RegisterInfoEmitter::runTargetDesc(raw_ostream &OS, CodeGenTarget &Target,
383 ++
384 ++ void RegisterInfoEmitter::run(raw_ostream &OS) {
385 ++ CodeGenRegBank &RegBank = Target.getRegBank();
386 +++
387 ++ runEnums(OS, Target, RegBank);
388 ++ runMCDesc(OS, Target, RegBank);
389 +++#ifndef CAPSTONE
390 ++ runTargetHeader(OS, Target, RegBank);
391 ++ runTargetDesc(OS, Target, RegBank);
392 +++#endif
393 ++
394 ++ if (RegisterInfoDebug)
395 ++ debugDump(errs());
396 ++--
397 ++2.19.1
398 ++
399 +diff --git a/llvm/0002-capstone-generate-GenSubtargetInfo.inc.patch b/llvm/0002-capstone-generate-GenSubtargetInfo.inc.patch
400 +new file mode 100644
401 +index 000000000..56ad28256
402 +--- /dev/null
403 ++++ b/llvm/0002-capstone-generate-GenSubtargetInfo.inc.patch
404 +@@ -0,0 +1,86 @@
405 ++From 46ca491e1bbbc9ace2a91fe6a7b112c83b9b88cc Mon Sep 17 00:00:00 2001
406 ++From: mephi42 <[email protected]>
407 ++Date: Tue, 7 Aug 2018 17:42:59 +0200
408 ++Subject: [PATCH 2/7] capstone: generate *GenSubtargetInfo.inc
409 ++
410 ++---
411 ++ utils/TableGen/SubtargetEmitter.cpp | 28 +++++++++++++++++++++++++++-
412 ++ 1 file changed, 27 insertions(+), 1 deletion(-)
413 ++
414 ++diff --git a/utils/TableGen/SubtargetEmitter.cpp b/utils/TableGen/SubtargetEmitter.cpp
415 ++index c5da8d8142f..98ab3240472 100644
416 ++--- a/utils/TableGen/SubtargetEmitter.cpp
417 +++++ b/utils/TableGen/SubtargetEmitter.cpp
418 ++@@ -147,7 +147,9 @@ void SubtargetEmitter::Enumeration(raw_ostream &OS) {
419 ++ if (N > MAX_SUBTARGET_FEATURES)
420 ++ PrintFatalError("Too many subtarget features! Bump MAX_SUBTARGET_FEATURES.");
421 ++
422 +++#ifndef CAPSTONE
423 ++ OS << "namespace " << Target << " {\n";
424 +++#endif
425 ++
426 ++ // Open enumeration.
427 ++ OS << "enum {\n";
428 ++@@ -158,12 +160,22 @@ void SubtargetEmitter::Enumeration(raw_ostream &OS) {
429 ++ Record *Def = DefList[i];
430 ++
431 ++ // Get and emit name
432 ++- OS << " " << Def->getName() << " = " << i << ",\n";
433 +++ OS << " "
434 +++#ifdef CAPSTONE
435 +++ << Target << "_"
436 +++#endif
437 +++ << Def->getName() << " = "
438 +++#ifdef CAPSTONE
439 +++ << "1ULL << "
440 +++#endif
441 +++ << i << ",\n";
442 ++ }
443 ++
444 ++ // Close enumeration and namespace
445 ++ OS << "};\n";
446 +++#ifndef CAPSTONE
447 ++ OS << "} // end namespace " << Target << "\n";
448 +++#endif
449 ++ }
450 ++
451 ++ //
452 ++@@ -1709,14 +1721,27 @@ void SubtargetEmitter::emitGenMCSubtargetInfo(raw_ostream &OS) {
453 ++ void SubtargetEmitter::run(raw_ostream &OS) {
454 ++ emitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
455 ++
456 +++#ifdef CAPSTONE
457 +++ OS << "/* Capstone Disassembly Engine, http://www.capstone-engine.org */\n"
458 +++ "/* By Nguyen Anh Quynh <[email protected]>, 2013-2015 */\n"
459 +++ "\n";
460 +++#endif
461 +++
462 ++ OS << "\n#ifdef GET_SUBTARGETINFO_ENUM\n";
463 ++ OS << "#undef GET_SUBTARGETINFO_ENUM\n\n";
464 ++
465 +++#ifndef CAPSTONE
466 ++ OS << "namespace llvm {\n";
467 +++#endif
468 ++ Enumeration(OS);
469 +++#ifdef CAPSTONE
470 +++ OS << "\n";
471 +++#else
472 ++ OS << "} // end namespace llvm\n\n";
473 +++#endif
474 ++ OS << "#endif // GET_SUBTARGETINFO_ENUM\n\n";
475 ++
476 +++#ifndef CAPSTONE
477 ++ OS << "\n#ifdef GET_SUBTARGETINFO_MC_DESC\n";
478 ++ OS << "#undef GET_SUBTARGETINFO_MC_DESC\n\n";
479 ++
480 ++@@ -1857,6 +1882,7 @@ void SubtargetEmitter::run(raw_ostream &OS) {
481 ++ OS << "} // end namespace llvm\n\n";
482 ++
483 ++ OS << "#endif // GET_SUBTARGETINFO_CTOR\n\n";
484 +++#endif
485 ++ }
486 ++
487 ++ namespace llvm {
488 ++--
489 ++2.19.1
490 ++
491 +diff --git a/llvm/0003-capstone-generate-GenInstrInfo.inc.patch b/llvm/0003-capstone-generate-GenInstrInfo.inc.patch
492 +new file mode 100644
493 +index 000000000..2baa59fc9
494 +--- /dev/null
495 ++++ b/llvm/0003-capstone-generate-GenInstrInfo.inc.patch
496 +@@ -0,0 +1,130 @@
497 ++From a73fe8ac18d3ca81fa7a8d8c404cd7e0faf92ddc Mon Sep 17 00:00:00 2001
498 ++From: mephi42 <[email protected]>
499 ++Date: Tue, 7 Aug 2018 17:59:43 +0200
500 ++Subject: [PATCH 3/7] capstone: generate *GenInstrInfo.inc
501 ++
502 ++---
503 ++ utils/TableGen/InstrInfoEmitter.cpp | 49 ++++++++++++++++++++++++++---
504 ++ 1 file changed, 44 insertions(+), 5 deletions(-)
505 ++
506 ++diff --git a/utils/TableGen/InstrInfoEmitter.cpp b/utils/TableGen/InstrInfoEmitter.cpp
507 ++index 0aff1aa6f94..2f3a2729262 100644
508 ++--- a/utils/TableGen/InstrInfoEmitter.cpp
509 +++++ b/utils/TableGen/InstrInfoEmitter.cpp
510 ++@@ -92,6 +92,7 @@ private:
511 ++
512 ++ } // end anonymous namespace
513 ++
514 +++#ifndef CAPSTONE
515 ++ static void PrintDefList(const std::vector<Record*> &Uses,
516 ++ unsigned Num, raw_ostream &OS) {
517 ++ OS << "static const MCPhysReg ImplicitList" << Num << "[] = { ";
518 ++@@ -99,6 +100,7 @@ static void PrintDefList(const std::vector<Record*> &Uses,
519 ++ OS << getQualifiedName(U) << ", ";
520 ++ OS << "0 };\n";
521 ++ }
522 +++#endif
523 ++
524 ++ //===----------------------------------------------------------------------===//
525 ++ // Operand Info Emission.
526 ++@@ -426,8 +428,17 @@ void InstrInfoEmitter::emitTIIHelperMethods(raw_ostream &OS) {
527 ++ // run - Emit the main instruction description records for the target...
528 ++ void InstrInfoEmitter::run(raw_ostream &OS) {
529 ++ emitSourceFileHeader("Target Instruction Enum Values and Descriptors", OS);
530 +++
531 +++#ifdef CAPSTONE
532 +++ OS << "/* Capstone Disassembly Engine */\n"
533 +++ "/* By Nguyen Anh Quynh <[email protected]>, 2013-2015 */\n"
534 +++ "\n"
535 +++ "\n";
536 +++#endif
537 +++
538 ++ emitEnums(OS);
539 ++
540 +++#ifndef CAPSTONE
541 ++ OS << "#ifdef GET_INSTRINFO_MC_DESC\n";
542 ++ OS << "#undef GET_INSTRINFO_MC_DESC\n";
543 ++
544 ++@@ -545,6 +556,7 @@ void InstrInfoEmitter::run(raw_ostream &OS) {
545 ++ emitOperandTypesEnum(OS, Target);
546 ++
547 ++ emitMCIIHelperMethods(OS);
548 +++#endif
549 ++ }
550 ++
551 ++ void InstrInfoEmitter::emitRecord(const CodeGenInstruction &Inst, unsigned Num,
552 ++@@ -659,7 +671,9 @@ void InstrInfoEmitter::emitEnums(raw_ostream &OS) {
553 ++ OS << "#ifdef GET_INSTRINFO_ENUM\n";
554 ++ OS << "#undef GET_INSTRINFO_ENUM\n";
555 ++
556 +++#ifndef CAPSTONE
557 ++ OS << "namespace llvm {\n\n";
558 +++#endif
559 ++
560 ++ CodeGenTarget Target(Records);
561 ++
562 ++@@ -669,17 +683,39 @@ void InstrInfoEmitter::emitEnums(raw_ostream &OS) {
563 ++ if (Namespace.empty())
564 ++ PrintFatalError("No instructions defined!");
565 ++
566 +++#ifndef CAPSTONE
567 ++ OS << "namespace " << Namespace << " {\n";
568 ++- OS << " enum {\n";
569 +++#endif
570 +++#ifdef CAPSTONE
571 +++ OS << "\n"
572 +++#else
573 +++ OS << " "
574 +++#endif
575 +++ << "enum {\n";
576 ++ unsigned Num = 0;
577 ++ for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue())
578 ++- OS << " " << Inst->TheDef->getName() << "\t= " << Num++ << ",\n";
579 ++- OS << " INSTRUCTION_LIST_END = " << Num << "\n";
580 +++ OS << " "
581 +++#ifdef CAPSTONE
582 +++ << Target.getName() << "_"
583 +++#endif
584 +++ << Inst->TheDef->getName() << "\t= " << Num++ << ",\n";
585 +++ OS << " "
586 +++#ifdef CAPSTONE
587 +++ << Target.getName() << "_"
588 +++#endif
589 +++ << "INSTRUCTION_LIST_END = " << Num << "\n";
590 ++ OS << " };\n\n";
591 +++#ifndef CAPSTONE
592 ++ OS << "} // end " << Namespace << " namespace\n";
593 ++ OS << "} // end llvm namespace\n";
594 ++- OS << "#endif // GET_INSTRINFO_ENUM\n\n";
595 ++-
596 +++#endif
597 +++ OS << "#endif // GET_INSTRINFO_ENUM\n"
598 +++#ifndef CAPSTONE
599 +++ << "\n"
600 +++#endif
601 +++ ;
602 +++
603 +++#ifndef CAPSTONE
604 ++ OS << "#ifdef GET_INSTRINFO_SCHED_ENUM\n";
605 ++ OS << "#undef GET_INSTRINFO_SCHED_ENUM\n";
606 ++ OS << "namespace llvm {\n\n";
607 ++@@ -696,13 +732,16 @@ void InstrInfoEmitter::emitEnums(raw_ostream &OS) {
608 ++ OS << "} // end llvm namespace\n";
609 ++
610 ++ OS << "#endif // GET_INSTRINFO_SCHED_ENUM\n\n";
611 +++#endif
612 ++ }
613 ++
614 ++ namespace llvm {
615 ++
616 ++ void EmitInstrInfo(RecordKeeper &RK, raw_ostream &OS) {
617 ++ InstrInfoEmitter(RK).run(OS);
618 +++#ifndef CAPSTONE
619 ++ EmitMapTable(RK, OS);
620 +++#endif
621 ++ }
622 ++
623 ++ } // end llvm namespace
624 ++--
625 ++2.19.1
626 ++
627 +diff --git a/llvm/0004-capstone-generate-GenDisassemblerTables.inc.patch b/llvm/0004-capstone-generate-GenDisassemblerTables.inc.patch
628 +new file mode 100644
629 +index 000000000..0002b81b4
630 +--- /dev/null
631 ++++ b/llvm/0004-capstone-generate-GenDisassemblerTables.inc.patch
632 +@@ -0,0 +1,472 @@
633 ++From 29da4c6929679b8ac4019767ab4ebcd83c9894b4 Mon Sep 17 00:00:00 2001
634 ++From: mephi42 <[email protected]>
635 ++Date: Tue, 7 Aug 2018 18:20:17 +0200
636 ++Subject: [PATCH 4/7] capstone: generate *GenDisassemblerTables.inc
637 ++
638 ++---
639 ++ utils/TableGen/DisassemblerEmitter.cpp | 12 +-
640 ++ utils/TableGen/FixedLenDecoderEmitter.cpp | 248 ++++++++++++++++++++--
641 ++ 2 files changed, 239 insertions(+), 21 deletions(-)
642 ++
643 ++diff --git a/utils/TableGen/DisassemblerEmitter.cpp b/utils/TableGen/DisassemblerEmitter.cpp
644 ++index b99a0a973a2..2ac6d89645c 100644
645 ++--- a/utils/TableGen/DisassemblerEmitter.cpp
646 +++++ b/utils/TableGen/DisassemblerEmitter.cpp
647 ++@@ -106,6 +106,11 @@ extern void EmitFixedLenDecoder(RecordKeeper &RK, raw_ostream &OS,
648 ++ void EmitDisassembler(RecordKeeper &Records, raw_ostream &OS) {
649 ++ CodeGenTarget Target(Records);
650 ++ emitSourceFileHeader(" * " + Target.getName().str() + " Disassembler", OS);
651 +++#ifdef CAPSTONE
652 +++ OS << "/* Capstone Disassembly Engine */\n"
653 +++ "/* By Nguyen Anh Quynh <[email protected]>, 2013-2015 */\n"
654 +++ "\n";
655 +++#endif
656 ++
657 ++ // X86 uses a custom disassembler.
658 ++ if (Target.getName() == "X86") {
659 ++@@ -150,7 +155,12 @@ void EmitDisassembler(RecordKeeper &Records, raw_ostream &OS) {
660 ++ }
661 ++
662 ++ EmitFixedLenDecoder(Records, OS, Target.getName(),
663 ++- "if (", " == MCDisassembler::Fail)",
664 +++ "if (",
665 +++#ifdef CAPSTONE
666 +++ " == MCDisassembler_Fail)",
667 +++#else
668 +++ " == MCDisassembler::Fail)",
669 +++#endif
670 ++ "MCDisassembler::Success", "MCDisassembler::Fail", "");
671 ++ }
672 ++
673 ++diff --git a/utils/TableGen/FixedLenDecoderEmitter.cpp b/utils/TableGen/FixedLenDecoderEmitter.cpp
674 ++index fcecc764d44..36845d960d8 100644
675 ++--- a/utils/TableGen/FixedLenDecoderEmitter.cpp
676 +++++ b/utils/TableGen/FixedLenDecoderEmitter.cpp
677 ++@@ -730,7 +730,13 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
678 ++ ++I;
679 ++ unsigned Start = *I++;
680 ++ unsigned Len = *I++;
681 ++- OS.indent(Indentation) << "MCD::OPC_ExtractField, " << Start << ", "
682 +++ OS.indent(Indentation)
683 +++#ifdef CAPSTONE
684 +++ << "MCD_OPC_ExtractField"
685 +++#else
686 +++ << "MCD::OPC_ExtractField"
687 +++#endif
688 +++ << ", " << Start << ", "
689 ++ << Len << ", // Inst{";
690 ++ if (Len > 1)
691 ++ OS << (Start + Len - 1) << "-";
692 ++@@ -739,7 +745,13 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
693 ++ }
694 ++ case MCD::OPC_FilterValue: {
695 ++ ++I;
696 ++- OS.indent(Indentation) << "MCD::OPC_FilterValue, ";
697 +++ OS.indent(Indentation)
698 +++#ifdef CAPSTONE
699 +++ << "MCD_OPC_FilterValue"
700 +++#else
701 +++ << "MCD::OPC_FilterValue"
702 +++#endif
703 +++ << ", ";
704 ++ // The filter value is ULEB128 encoded.
705 ++ while (*I >= 128)
706 ++ OS << (unsigned)*I++ << ", ";
707 ++@@ -759,7 +771,13 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
708 ++ ++I;
709 ++ unsigned Start = *I++;
710 ++ unsigned Len = *I++;
711 ++- OS.indent(Indentation) << "MCD::OPC_CheckField, " << Start << ", "
712 +++ OS.indent(Indentation)
713 +++#ifdef CAPSTONE
714 +++ << "MCD_OPC_CheckField"
715 +++#else
716 +++ << "MCD::OPC_CheckField"
717 +++#endif
718 +++ << ", " << Start << ", "
719 ++ << Len << ", ";// << Val << ", " << NumToSkip << ",\n";
720 ++ // ULEB128 encoded field value.
721 ++ for (; *I >= 128; ++I)
722 ++@@ -777,7 +795,13 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
723 ++ }
724 ++ case MCD::OPC_CheckPredicate: {
725 ++ ++I;
726 ++- OS.indent(Indentation) << "MCD::OPC_CheckPredicate, ";
727 +++ OS.indent(Indentation)
728 +++#ifdef CAPSTONE
729 +++ << "MCD_OPC_CheckPredicate"
730 +++#else
731 +++ << "MCD::OPC_CheckPredicate"
732 +++#endif
733 +++ << ", ";
734 ++ for (; *I >= 128; ++I)
735 ++ OS << (unsigned)*I << ", ";
736 ++ OS << (unsigned)*I++ << ", ";
737 ++@@ -803,7 +827,13 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
738 ++ && "ULEB128 value too large!");
739 ++ // Decode the Opcode value.
740 ++ unsigned Opc = decodeULEB128(Buffer);
741 ++- OS.indent(Indentation) << "MCD::OPC_" << (IsTry ? "Try" : "")
742 +++ OS.indent(Indentation)
743 +++#ifdef CAPSTONE
744 +++ << "MCD_OPC_"
745 +++#else
746 +++ << "MCD::OPC_"
747 +++#endif
748 +++ << (IsTry ? "Try" : "")
749 ++ << "Decode, ";
750 ++ for (p = Buffer; *p >= 128; ++p)
751 ++ OS << (unsigned)*p << ", ";
752 ++@@ -837,7 +867,12 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
753 ++ }
754 ++ case MCD::OPC_SoftFail: {
755 ++ ++I;
756 ++- OS.indent(Indentation) << "MCD::OPC_SoftFail";
757 +++ OS.indent(Indentation)
758 +++#ifdef CAPSTONE
759 +++ << "MCD_OPC_SoftFail";
760 +++#else
761 +++ << "MCD::OPC_SoftFail";
762 +++#endif
763 ++ // Positive mask
764 ++ uint64_t Value = 0;
765 ++ unsigned Shift = 0;
766 ++@@ -869,7 +904,13 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
767 ++ }
768 ++ case MCD::OPC_Fail: {
769 ++ ++I;
770 ++- OS.indent(Indentation) << "MCD::OPC_Fail,\n";
771 +++ OS.indent(Indentation)
772 +++#ifdef CAPSTONE
773 +++ << "MCD_OPC_Fail"
774 +++#else
775 +++ << "MCD::OPC_Fail"
776 +++#endif
777 +++ << ",\n";
778 ++ break;
779 ++ }
780 ++ }
781 ++@@ -884,23 +925,46 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
782 ++ void FixedLenDecoderEmitter::
783 ++ emitPredicateFunction(formatted_raw_ostream &OS, PredicateSet &Predicates,
784 ++ unsigned Indentation) const {
785 +++#ifdef CAPSTONE
786 +++ OS.indent(Indentation) << "static bool getbool(uint64_t b)\n";
787 +++ OS.indent(Indentation) << "{\n";
788 +++ OS.indent(Indentation) << "\treturn b != 0;\n";
789 +++ OS.indent(Indentation) << "}\n\n";
790 +++#endif
791 +++
792 ++ // The predicate function is just a big switch statement based on the
793 ++ // input predicate index.
794 ++ OS.indent(Indentation) << "static bool checkDecoderPredicate(unsigned Idx, "
795 +++#ifdef CAPSTONE
796 +++ << "uint64_t Bits)\n{\n";
797 +++#else
798 ++ << "const FeatureBitset& Bits) {\n";
799 +++#endif
800 ++ Indentation += 2;
801 ++ if (!Predicates.empty()) {
802 ++ OS.indent(Indentation) << "switch (Idx) {\n";
803 ++- OS.indent(Indentation) << "default: llvm_unreachable(\"Invalid index!\");\n";
804 +++ OS.indent(Indentation) << "default: "
805 +++#ifdef CAPSTONE
806 +++ << "// "
807 +++#endif
808 +++ << "llvm_unreachable(\"Invalid index!\");\n";
809 ++ unsigned Index = 0;
810 ++ for (const auto &Predicate : Predicates) {
811 ++ OS.indent(Indentation) << "case " << Index++ << ":\n";
812 ++- OS.indent(Indentation+2) << "return (" << Predicate << ");\n";
813 +++ OS.indent(Indentation+2) << "return "
814 +++#ifdef CAPSTONE
815 +++ << "getbool"
816 +++#endif
817 +++ << "(" << Predicate << ");\n";
818 ++ }
819 ++ OS.indent(Indentation) << "}\n";
820 ++ } else {
821 ++ // No case statement to emit
822 ++- OS.indent(Indentation) << "llvm_unreachable(\"Invalid index!\");\n";
823 +++ OS.indent(Indentation)
824 +++#ifdef CAPSTONE
825 +++ << "// "
826 +++#endif
827 +++ << "llvm_unreachable(\"Invalid index!\");\n";
828 ++ }
829 ++ Indentation -= 2;
830 ++ OS.indent(Indentation) << "}\n\n";
831 ++@@ -911,23 +975,39 @@ emitDecoderFunction(formatted_raw_ostream &OS, DecoderSet &Decoders,
832 ++ unsigned Indentation) const {
833 ++ // The decoder function is just a big switch statement based on the
834 ++ // input decoder index.
835 +++#ifdef CAPSTONE
836 +++#define EDF_EOL " \\\n"
837 +++ OS.indent(Indentation) << "#define DecodeToMCInst(fname,fieldname, InsnType) \\\n";
838 +++ OS.indent(Indentation) << "static DecodeStatus fname(DecodeStatus S, unsigned Idx, InsnType insn, MCInst *MI, \\\n";
839 +++ OS.indent(Indentation) << " uint64_t Address, const void *Decoder) \\\n";
840 +++ OS.indent(Indentation) << "{ \\\n";
841 +++#else
842 +++#define EDF_EOL "\n"
843 ++ OS.indent(Indentation) << "template<typename InsnType>\n";
844 ++ OS.indent(Indentation) << "static DecodeStatus decodeToMCInst(DecodeStatus S,"
845 ++ << " unsigned Idx, InsnType insn, MCInst &MI,\n";
846 ++ OS.indent(Indentation) << " uint64_t "
847 ++ << "Address, const void *Decoder, bool &DecodeComplete) {\n";
848 +++#endif
849 ++ Indentation += 2;
850 +++#ifndef CAPSTONE
851 ++ OS.indent(Indentation) << "DecodeComplete = true;\n";
852 ++- OS.indent(Indentation) << "InsnType tmp;\n";
853 ++- OS.indent(Indentation) << "switch (Idx) {\n";
854 ++- OS.indent(Indentation) << "default: llvm_unreachable(\"Invalid index!\");\n";
855 +++#endif
856 +++ OS.indent(Indentation) << "InsnType tmp;" EDF_EOL;
857 +++ OS.indent(Indentation) << "switch (Idx) {" EDF_EOL;
858 +++ OS.indent(Indentation) << "default:"
859 +++#ifndef CAPSTONE
860 +++ << " llvm_unreachable(\"Invalid index!\");\n";
861 +++#else
862 +++ << " \\\n";
863 +++#endif
864 ++ unsigned Index = 0;
865 ++ for (const auto &Decoder : Decoders) {
866 ++- OS.indent(Indentation) << "case " << Index++ << ":\n";
867 +++ OS.indent(Indentation) << "case " << Index++ << ":" EDF_EOL;
868 ++ OS << Decoder;
869 ++- OS.indent(Indentation+2) << "return S;\n";
870 +++ OS.indent(Indentation+2) << "return S;" EDF_EOL;
871 ++ }
872 ++- OS.indent(Indentation) << "}\n";
873 +++ OS.indent(Indentation) << "}" EDF_EOL;
874 ++ Indentation -= 2;
875 ++ OS.indent(Indentation) << "}\n\n";
876 ++ }
877 ++@@ -1054,16 +1134,21 @@ void FilterChooser::emitBinaryParser(raw_ostream &o, unsigned &Indentation,
878 ++ const std::string &Decoder = OpInfo.Decoder;
879 ++
880 ++ if (OpInfo.numFields() != 1)
881 ++- o.indent(Indentation) << "tmp = 0;\n";
882 +++ o.indent(Indentation) << "tmp = 0;" EDF_EOL;
883 ++
884 ++ for (const EncodingField &EF : OpInfo) {
885 ++ o.indent(Indentation) << "tmp ";
886 ++ if (OpInfo.numFields() != 1) o << '|';
887 ++- o << "= fieldFromInstruction"
888 +++ o << "= "
889 +++#ifdef CAPSTONE
890 +++ << "fieldname"
891 +++#else
892 +++ << "fieldFromInstruction"
893 +++#endif
894 ++ << "(insn, " << EF.Base << ", " << EF.Width << ')';
895 ++ if (OpInfo.numFields() != 1 || EF.Offset != 0)
896 ++ o << " << " << EF.Offset;
897 ++- o << ";\n";
898 +++ o << ";" EDF_EOL;
899 ++ }
900 ++
901 ++ if (Decoder != "") {
902 ++@@ -1071,8 +1156,12 @@ void FilterChooser::emitBinaryParser(raw_ostream &o, unsigned &Indentation,
903 ++ o.indent(Indentation) << Emitter->GuardPrefix << Decoder
904 ++ << "(MI, tmp, Address, Decoder)"
905 ++ << Emitter->GuardPostfix
906 +++#ifdef CAPSTONE
907 +++ << " return MCDisassembler_Fail; \\\n";
908 +++#else
909 ++ << " { " << (OpHasCompleteDecoder ? "" : "DecodeComplete = false; ")
910 ++ << "return MCDisassembler::Fail; }\n";
911 +++#endif
912 ++ } else {
913 ++ OpHasCompleteDecoder = true;
914 ++ o.indent(Indentation) << "MI.addOperand(MCOperand::createImm(tmp));\n";
915 ++@@ -1091,7 +1180,13 @@ void FilterChooser::emitDecoder(raw_ostream &OS, unsigned Indentation,
916 ++ << "(MI, insn, Address, Decoder)"
917 ++ << Emitter->GuardPostfix
918 ++ << " { " << (HasCompleteDecoder ? "" : "DecodeComplete = false; ")
919 ++- << "return MCDisassembler::Fail; }\n";
920 +++ << "return "
921 +++#ifdef CAPSTONE
922 +++ << "MCDisassembler_Fail"
923 +++#else
924 +++ << "MCDisassembler::Fail"
925 +++#endif
926 +++ << "; }\n";
927 ++ break;
928 ++ }
929 ++
930 ++@@ -1129,10 +1224,19 @@ unsigned FilterChooser::getDecoderIndex(DecoderSet &Decoders,
931 ++ static void emitSinglePredicateMatch(raw_ostream &o, StringRef str,
932 ++ const std::string &PredicateNamespace) {
933 ++ if (str[0] == '!')
934 +++#ifdef CAPSTONE
935 +++ o << "~(Bits & " << PredicateNamespace << "_"
936 +++ << str.slice(1,str.size()) << ")";
937 +++#else
938 ++ o << "!Bits[" << PredicateNamespace << "::"
939 ++ << str.slice(1,str.size()) << "]";
940 +++#endif
941 ++ else
942 +++#ifdef CAPSTONE
943 +++ o << "(Bits & " << PredicateNamespace << "_" << str << ")";
944 +++#else
945 ++ o << "Bits[" << PredicateNamespace << "::" << str << "]";
946 +++#endif
947 ++ }
948 ++
949 ++ bool FilterChooser::emitPredicateMatch(raw_ostream &o, unsigned &Indentation,
950 ++@@ -2047,6 +2151,17 @@ static bool populateInstruction(CodeGenTarget &Target,
951 ++ // fieldFromInstruction().
952 ++ static void emitFieldFromInstruction(formatted_raw_ostream &OS) {
953 ++ OS << "// Helper function for extracting fields from encoded instructions.\n"
954 +++#ifdef CAPSTONE
955 +++ << "#define FieldFromInstruction(fname, InsnType) \\\n"
956 +++ << "static InsnType fname(InsnType insn, unsigned startBit, unsigned numBits) \\\n"
957 +++ << "{ \\\n"
958 +++ << " InsnType fieldMask; \\\n"
959 +++ << " if (numBits == sizeof(InsnType)*8) \\\n"
960 +++ << " fieldMask = (InsnType)(-1LL); \\\n"
961 +++ << " else \\\n"
962 +++ << " fieldMask = (((InsnType)1 << numBits) - 1) << startBit; \\\n"
963 +++ << " return (insn & fieldMask) >> startBit; \\\n"
964 +++#else
965 ++ << "template<typename InsnType>\n"
966 ++ << "static InsnType fieldFromInstruction(InsnType insn, unsigned startBit,\n"
967 ++ << " unsigned numBits) {\n"
968 ++@@ -2058,12 +2173,92 @@ static void emitFieldFromInstruction(formatted_raw_ostream &OS) {
969 ++ << " else\n"
970 ++ << " fieldMask = (((InsnType)1 << numBits) - 1) << startBit;\n"
971 ++ << " return (insn & fieldMask) >> startBit;\n"
972 +++#endif
973 ++ << "}\n\n";
974 ++ }
975 ++
976 ++ // emitDecodeInstruction - Emit the templated helper function
977 ++ // decodeInstruction().
978 ++ static void emitDecodeInstruction(formatted_raw_ostream &OS) {
979 +++#ifdef CAPSTONE
980 +++ OS << "#define DecodeInstruction(fname, fieldname, decoder, InsnType) \\\n"
981 +++ << "static DecodeStatus fname(const uint8_t DecodeTable[], MCInst *MI, \\\n"
982 +++ << " InsnType insn, uint64_t Address, const MCRegisterInfo *MRI, int feature) \\\n"
983 +++ << "{ \\\n"
984 +++ << " uint64_t Bits = getFeatureBits(feature); \\\n"
985 +++ << " const uint8_t *Ptr = DecodeTable; \\\n"
986 +++ << " uint32_t CurFieldValue = 0, ExpectedValue; \\\n"
987 +++ << " DecodeStatus S = MCDisassembler_Success; \\\n"
988 +++ << " unsigned Start, Len, NumToSkip, PIdx, Opc, DecodeIdx; \\\n"
989 +++ << " InsnType Val, FieldValue, PositiveMask, NegativeMask; \\\n"
990 +++ << " bool Pred, Fail; \\\n"
991 +++ << " for (;;) { \\\n"
992 +++ << " switch (*Ptr) { \\\n"
993 +++ << " default: \\\n"
994 +++ << " return MCDisassembler_Fail; \\\n"
995 +++ << " case MCD_OPC_ExtractField: { \\\n"
996 +++ << " Start = *++Ptr; \\\n"
997 +++ << " Len = *++Ptr; \\\n"
998 +++ << " ++Ptr; \\\n"
999 +++ << " CurFieldValue = (uint32_t)fieldname(insn, Start, Len); \\\n"
1000 +++ << " break; \\\n"
1001 +++ << " } \\\n"
1002 +++ << " case MCD_OPC_FilterValue: { \\\n"
1003 +++ << " Val = (InsnType)decodeULEB128(++Ptr, &Len); \\\n"
1004 +++ << " Ptr += Len; \\\n"
1005 +++ << " NumToSkip = *Ptr++; \\\n"
1006 +++ << " NumToSkip |= (*Ptr++) << 8; \\\n"
1007 +++ << " if (Val != CurFieldValue) \\\n"
1008 +++ << " Ptr += NumToSkip; \\\n"
1009 +++ << " break; \\\n"
1010 +++ << " } \\\n"
1011 +++ << " case MCD_OPC_CheckField: { \\\n"
1012 +++ << " Start = *++Ptr; \\\n"
1013 +++ << " Len = *++Ptr; \\\n"
1014 +++ << " FieldValue = fieldname(insn, Start, Len); \\\n"
1015 +++ << " ExpectedValue = (uint32_t)decodeULEB128(++Ptr, &Len); \\\n"
1016 +++ << " Ptr += Len; \\\n"
1017 +++ << " NumToSkip = *Ptr++; \\\n"
1018 +++ << " NumToSkip |= (*Ptr++) << 8; \\\n"
1019 +++ << " if (ExpectedValue != FieldValue) \\\n"
1020 +++ << " Ptr += NumToSkip; \\\n"
1021 +++ << " break; \\\n"
1022 +++ << " } \\\n"
1023 +++ << " case MCD_OPC_CheckPredicate: { \\\n"
1024 +++ << " PIdx = (uint32_t)decodeULEB128(++Ptr, &Len); \\\n"
1025 +++ << " Ptr += Len; \\\n"
1026 +++ << " NumToSkip = *Ptr++; \\\n"
1027 +++ << " NumToSkip |= (*Ptr++) << 8; \\\n"
1028 +++ << " Pred = checkDecoderPredicate(PIdx, Bits); \\\n"
1029 +++ << " if (!Pred) \\\n"
1030 +++ << " Ptr += NumToSkip; \\\n"
1031 +++ << " (void)Pred; \\\n"
1032 +++ << " break; \\\n"
1033 +++ << " } \\\n"
1034 +++ << " case MCD_OPC_Decode: { \\\n"
1035 +++ << " Opc = (unsigned)decodeULEB128(++Ptr, &Len); \\\n"
1036 +++ << " Ptr += Len; \\\n"
1037 +++ << " DecodeIdx = (unsigned)decodeULEB128(Ptr, &Len); \\\n"
1038 +++ << " Ptr += Len; \\\n"
1039 +++ << " MCInst_setOpcode(MI, Opc); \\\n"
1040 +++ << " return decoder(S, DecodeIdx, insn, MI, Address, MRI); \\\n"
1041 +++ << " } \\\n"
1042 +++ << " case MCD_OPC_SoftFail: { \\\n"
1043 +++ << " PositiveMask = (InsnType)decodeULEB128(++Ptr, &Len); \\\n"
1044 +++ << " Ptr += Len; \\\n"
1045 +++ << " NegativeMask = (InsnType)decodeULEB128(Ptr, &Len); \\\n"
1046 +++ << " Ptr += Len; \\\n"
1047 +++ << " Fail = (insn & PositiveMask) || (~insn & NegativeMask); \\\n"
1048 +++ << " if (Fail) \\\n"
1049 +++ << " S = MCDisassembler_SoftFail; \\\n"
1050 +++ << " break; \\\n"
1051 +++ << " } \\\n"
1052 +++ << " case MCD_OPC_Fail: { \\\n"
1053 +++ << " return MCDisassembler_Fail; \\\n"
1054 +++ << " } \\\n"
1055 +++ << " } \\\n"
1056 +++ << " } \\\n"
1057 +++#else
1058 ++ OS << "template<typename InsnType>\n"
1059 ++ << "static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], "
1060 ++ "MCInst &MI,\n"
1061 ++@@ -2240,12 +2435,18 @@ static void emitDecodeInstruction(formatted_raw_ostream &OS) {
1062 ++ << " }\n"
1063 ++ << " llvm_unreachable(\"bogosity detected in disassembler state "
1064 ++ "machine!\");\n"
1065 +++#endif
1066 ++ << "}\n\n";
1067 ++ }
1068 ++
1069 ++ // Emits disassembler code for instruction decoding.
1070 ++ void FixedLenDecoderEmitter::run(raw_ostream &o) {
1071 ++ formatted_raw_ostream OS(o);
1072 +++#ifdef CAPSTONE
1073 +++ OS << "#include \"../../MCInst.h\"\n";
1074 +++ OS << "#include \"../../LEB128.h\"\n";
1075 +++ OS << "\n";
1076 +++#else
1077 ++ OS << "#include \"llvm/MC/MCInst.h\"\n";
1078 ++ OS << "#include \"llvm/Support/Debug.h\"\n";
1079 ++ OS << "#include \"llvm/Support/DataTypes.h\"\n";
1080 ++@@ -2254,6 +2455,7 @@ void FixedLenDecoderEmitter::run(raw_ostream &o) {
1081 ++ OS << "#include <assert.h>\n";
1082 ++ OS << '\n';
1083 ++ OS << "namespace llvm {\n\n";
1084 +++#endif
1085 ++
1086 ++ emitFieldFromInstruction(OS);
1087 ++
1088 ++@@ -2322,7 +2524,13 @@ void FixedLenDecoderEmitter::run(raw_ostream &o) {
1089 ++ // Emit the main entry point for the decoder, decodeInstruction().
1090 ++ emitDecodeInstruction(OS);
1091 ++
1092 +++#ifdef CAPSTONE
1093 +++ OS << "FieldFromInstruction(fieldFromInstruction, uint64_t)\n";
1094 +++ OS << "DecodeToMCInst(decodeToMCInst, fieldFromInstruction, uint64_t)\n";
1095 +++ OS << "DecodeInstruction(decodeInstruction, fieldFromInstruction, decodeToMCInst, uint64_t)\n";
1096 +++#else
1097 ++ OS << "\n} // End llvm namespace\n";
1098 +++#endif
1099 ++ }
1100 ++
1101 ++ namespace llvm {
1102 ++--
1103 ++2.19.1
1104 ++
1105 +diff --git a/llvm/0005-capstone-generate-GenAsmWriter.inc.patch b/llvm/0005-capstone-generate-GenAsmWriter.inc.patch
1106 +new file mode 100644
1107 +index 000000000..cd1353eb7
1108 +--- /dev/null
1109 ++++ b/llvm/0005-capstone-generate-GenAsmWriter.inc.patch
1110 +@@ -0,0 +1,225 @@
1111 ++From 5569e48b9cb34a33910e1e850fbfabc999f016a2 Mon Sep 17 00:00:00 2001
1112 ++From: mephi42 <[email protected]>
1113 ++Date: Tue, 7 Aug 2018 20:00:08 +0200
1114 ++Subject: [PATCH 5/7] capstone: generate *GenAsmWriter.inc
1115 ++
1116 ++---
1117 ++ utils/TableGen/AsmWriterEmitter.cpp | 89 +++++++++++++++++++++++++++--
1118 ++ utils/TableGen/AsmWriterInst.cpp | 4 ++
1119 ++ 2 files changed, 87 insertions(+), 6 deletions(-)
1120 ++
1121 ++diff --git a/utils/TableGen/AsmWriterEmitter.cpp b/utils/TableGen/AsmWriterEmitter.cpp
1122 ++index 3c4c9c8e5c6..133800d217c 100644
1123 ++--- a/utils/TableGen/AsmWriterEmitter.cpp
1124 +++++ b/utils/TableGen/AsmWriterEmitter.cpp
1125 ++@@ -272,16 +272,22 @@ static void UnescapeString(std::string &Str) {
1126 ++ /// clearing the Instructions vector.
1127 ++ void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
1128 ++ Record *AsmWriter = Target.getAsmWriter();
1129 +++#ifndef CAPSTONE
1130 ++ StringRef ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
1131 +++#endif
1132 ++ bool PassSubtarget = AsmWriter->getValueAsInt("PassSubtarget");
1133 ++
1134 ++ O <<
1135 ++ "/// printInstruction - This method is automatically generated by tablegen\n"
1136 ++ "/// from the instruction set description.\n"
1137 +++#ifdef CAPSTONE
1138 +++ "static void printInstruction(MCInst *MI, SStream *O, MCRegisterInfo *MRI)\n{\n";
1139 +++#else
1140 ++ "void " << Target.getName() << ClassName
1141 ++ << "::printInstruction(const MCInst *MI, "
1142 ++ << (PassSubtarget ? "const MCSubtargetInfo &STI, " : "")
1143 ++ << "raw_ostream &O) {\n";
1144 +++#endif
1145 ++
1146 ++ // Build an aggregate string, and build a table of offsets into it.
1147 ++ SequenceToOffsetTable<std::string> StringTable;
1148 ++@@ -379,9 +385,16 @@ void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
1149 ++ }
1150 ++
1151 ++ // Emit the string table itself.
1152 +++#ifdef CAPSTONE
1153 +++ O << "#ifndef CAPSTONE_DIET\n";
1154 +++#endif
1155 ++ O << " static const char AsmStrs[] = {\n";
1156 ++ StringTable.emit(O, printChar);
1157 ++- O << " };\n\n";
1158 +++ O << " };\n"
1159 +++#ifdef CAPSTONE
1160 +++ << "#endif\n"
1161 +++#endif
1162 +++ << "\n";
1163 ++
1164 ++ // Emit the lookup tables in pieces to minimize wasted bytes.
1165 ++ unsigned BytesNeeded = ((OpcodeInfoBits - BitsLeft) + 7) / 8;
1166 ++@@ -409,21 +422,45 @@ void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
1167 ++ // If the total bits is more than 32-bits we need to use a 64-bit type.
1168 ++ if (BitsLeft < (OpcodeInfoBits - 32))
1169 ++ BitsOS << "(uint64_t)";
1170 ++- BitsOS << "OpInfo" << Table << "[MI->getOpcode()] << " << Shift << ";\n";
1171 +++ BitsOS << "OpInfo" << Table << "["
1172 +++#ifdef CAPSTONE
1173 +++ << "MCInst_getOpcode(MI)"
1174 +++#else
1175 +++ << "MI->getOpcode()"
1176 +++#endif
1177 +++ << "] << " << Shift << ";\n";
1178 ++ // Prepare the shift for the next iteration and increment the table count.
1179 ++ Shift += TableSize;
1180 ++ ++Table;
1181 ++ }
1182 ++
1183 ++ // Emit the initial tab character.
1184 +++#ifndef CAPSTONE
1185 ++ O << " O << \"\\t\";\n\n";
1186 +++#endif
1187 ++
1188 ++ O << " // Emit the opcode for the instruction.\n";
1189 ++ O << BitsString;
1190 ++
1191 ++ // Emit the starting string.
1192 ++- O << " assert(Bits != 0 && \"Cannot print this instruction.\");\n"
1193 ++- << " O << AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << ")-1;\n\n";
1194 +++ O << " "
1195 +++#ifdef CAPSTONE
1196 +++ << "// "
1197 +++#endif
1198 +++ << "assert(Bits != 0 && \"Cannot print this instruction.\");\n"
1199 +++#ifdef CAPSTONE
1200 +++ << "#ifndef CAPSTONE_DIET\n"
1201 +++ << " SStream_concat0(O, "
1202 +++#else
1203 +++ << " O << "
1204 +++#endif
1205 +++ << "AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << ")-1"
1206 +++#ifdef CAPSTONE
1207 +++ << ");\n"
1208 +++ << "#endif\n\n";
1209 +++#else
1210 +++ << ");\n\n";
1211 +++#endif
1212 ++
1213 ++ // Output the table driven operand information.
1214 ++ BitsLeft = OpcodeInfoBits-AsmStrBits;
1215 ++@@ -455,7 +492,11 @@ void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
1216 ++ O << " switch ((Bits >> "
1217 ++ << (OpcodeInfoBits-BitsLeft) << ") & "
1218 ++ << ((1 << NumBits)-1) << ") {\n"
1219 ++- << " default: llvm_unreachable(\"Invalid command number.\");\n";
1220 +++ << " default: "
1221 +++#ifdef CAPSTONE
1222 +++ << "// "
1223 +++#endif
1224 +++ << "llvm_unreachable(\"Invalid command number.\");\n";
1225 ++
1226 ++ // Print out all the cases.
1227 ++ for (unsigned j = 0, e = Commands.size(); j != e; ++j) {
1228 ++@@ -536,6 +577,9 @@ emitRegisterNameString(raw_ostream &O, StringRef AltName,
1229 ++ }
1230 ++
1231 ++ StringTable.layout();
1232 +++#ifdef CAPSTONE
1233 +++ O << "#ifndef CAPSTONE_DIET\n";
1234 +++#endif
1235 ++ O << " static const char AsmStrs" << AltName << "[] = {\n";
1236 ++ StringTable.emit(O, printChar);
1237 ++ O << " };\n\n";
1238 ++@@ -552,8 +596,10 @@ emitRegisterNameString(raw_ostream &O, StringRef AltName,
1239 ++ }
1240 ++
1241 ++ void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
1242 +++#ifndef CAPSTONE
1243 ++ Record *AsmWriter = Target.getAsmWriter();
1244 ++ StringRef ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
1245 +++#endif
1246 ++ const auto &Registers = Target.getRegBank().getRegisters();
1247 ++ const std::vector<Record*> &AltNameIndices = Target.getRegAltNameIndices();
1248 ++ bool hasAltNames = AltNameIndices.size() > 1;
1249 ++@@ -563,12 +609,20 @@ void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
1250 ++ "\n\n/// getRegisterName - This method is automatically generated by tblgen\n"
1251 ++ "/// from the register set description. This returns the assembler name\n"
1252 ++ "/// for the specified register.\n"
1253 +++#ifdef CAPSTONE
1254 +++ "static const char *getRegisterName(unsigned RegNo)\n{\n";
1255 +++#else
1256 ++ "const char *" << Target.getName() << ClassName << "::";
1257 ++ if (hasAltNames)
1258 ++ O << "\ngetRegisterName(unsigned RegNo, unsigned AltIdx) {\n";
1259 ++ else
1260 ++ O << "getRegisterName(unsigned RegNo) {\n";
1261 ++- O << " assert(RegNo && RegNo < " << (Registers.size()+1)
1262 +++#endif
1263 +++ O << " "
1264 +++#ifdef CAPSTONE
1265 +++ << "// "
1266 +++#endif
1267 +++ << "assert(RegNo && RegNo < " << (Registers.size()+1)
1268 ++ << " && \"Invalid register number!\");\n"
1269 ++ << "\n";
1270 ++
1271 ++@@ -595,10 +649,22 @@ void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
1272 ++ }
1273 ++ O << " }\n";
1274 ++ } else {
1275 +++#ifdef CAPSTONE
1276 +++ O << " //int i;\n"
1277 +++ << " //for (i = 0; i < sizeof(RegAsmOffset); i++)\n"
1278 +++ << " // printf(\"%s = %u\\n\", AsmStrs+RegAsmOffset[i], i + 1);\n"
1279 +++ << " //printf(\"*************************\\n\");\n"
1280 +++#else
1281 ++ O << " assert (*(AsmStrs+RegAsmOffset[RegNo-1]) &&\n"
1282 ++ << " \"Invalid alt name index for register!\");\n"
1283 +++#endif
1284 ++ << " return AsmStrs+RegAsmOffset[RegNo-1];\n";
1285 ++ }
1286 +++#ifdef CAPSTONE
1287 +++ O << "#else\n"
1288 +++ << " return NULL;\n"
1289 +++ << "#endif\n";
1290 +++#endif
1291 ++ O << "}\n";
1292 ++ }
1293 ++
1294 ++@@ -1135,9 +1201,20 @@ AsmWriterEmitter::AsmWriterEmitter(RecordKeeper &R) : Records(R), Target(R) {
1295 ++ }
1296 ++
1297 ++ void AsmWriterEmitter::run(raw_ostream &O) {
1298 +++#ifdef CAPSTONE
1299 +++ O << "/* Capstone Disassembly Engine */\n"
1300 +++ "/* By Nguyen Anh Quynh <[email protected]>, 2013-2015 */\n"
1301 +++ "\n"
1302 +++ "#include <stdio.h>\t// debug\n"
1303 +++ "#include <capstone/platform.h>\n"
1304 +++ "\n"
1305 +++ "\n";
1306 +++#endif
1307 ++ EmitPrintInstruction(O);
1308 ++ EmitGetRegisterName(O);
1309 +++#ifndef CAPSTONE
1310 ++ EmitPrintAliasInstruction(O);
1311 +++#endif
1312 ++ }
1313 ++
1314 ++ namespace llvm {
1315 ++diff --git a/utils/TableGen/AsmWriterInst.cpp b/utils/TableGen/AsmWriterInst.cpp
1316 ++index 2c19e5d663d..6fa751e50df 100644
1317 ++--- a/utils/TableGen/AsmWriterInst.cpp
1318 +++++ b/utils/TableGen/AsmWriterInst.cpp
1319 ++@@ -28,9 +28,13 @@ static bool isIdentChar(char C) {
1320 ++
1321 ++ std::string AsmWriterOperand::getCode(bool PassSubtarget) const {
1322 ++ if (OperandType == isLiteralTextOperand) {
1323 +++#ifdef CAPSTONE
1324 +++ return "SStream_concat0(O, \"" + Str + "\");";
1325 +++#else
1326 ++ if (Str.size() == 1)
1327 ++ return "O << '" + Str + "';";
1328 ++ return "O << \"" + Str + "\";";
1329 +++#endif
1330 ++ }
1331 ++
1332 ++ if (OperandType == isLiteralStatementOperand)
1333 ++--
1334 ++2.19.1
1335 ++
1336 +diff --git a/llvm/0006-capstone-generate-MappingInsn.inc.patch b/llvm/0006-capstone-generate-MappingInsn.inc.patch
1337 +new file mode 100644
1338 +index 000000000..7ee22d787
1339 +--- /dev/null
1340 ++++ b/llvm/0006-capstone-generate-MappingInsn.inc.patch
1341 +@@ -0,0 +1,174 @@
1342 ++From 7a436110ef15c803dc8524af2fb5612bcacbb126 Mon Sep 17 00:00:00 2001
1343 ++From: mephi42 <[email protected]>
1344 ++Date: Tue, 7 Aug 2018 20:55:32 +0200
1345 ++Subject: [PATCH 6/7] capstone: generate *MappingInsn.inc
1346 ++
1347 ++---
1348 ++ lib/Target/SystemZ/CMakeLists.txt | 1 +
1349 ++ utils/TableGen/InstrInfoEmitter.cpp | 95 +++++++++++++++++++++++++++++
1350 ++ utils/TableGen/TableGen.cpp | 6 ++
1351 ++ utils/TableGen/TableGenBackends.h | 1 +
1352 ++ 4 files changed, 103 insertions(+)
1353 ++
1354 ++diff --git a/lib/Target/SystemZ/CMakeLists.txt b/lib/Target/SystemZ/CMakeLists.txt
1355 ++index f83b4242fb4..4b5d9c4a3b2 100644
1356 ++--- a/lib/Target/SystemZ/CMakeLists.txt
1357 +++++ b/lib/Target/SystemZ/CMakeLists.txt
1358 ++@@ -6,6 +6,7 @@ tablegen(LLVM SystemZGenCallingConv.inc -gen-callingconv)
1359 ++ tablegen(LLVM SystemZGenDAGISel.inc -gen-dag-isel)
1360 ++ tablegen(LLVM SystemZGenDisassemblerTables.inc -gen-disassembler)
1361 ++ tablegen(LLVM SystemZGenInstrInfo.inc -gen-instr-info)
1362 +++tablegen(LLVM SystemZMappingInsn.inc -mapping-insn)
1363 ++ tablegen(LLVM SystemZGenMCCodeEmitter.inc -gen-emitter)
1364 ++ tablegen(LLVM SystemZGenRegisterInfo.inc -gen-register-info)
1365 ++ tablegen(LLVM SystemZGenSubtargetInfo.inc -gen-subtarget)
1366 ++diff --git a/utils/TableGen/InstrInfoEmitter.cpp b/utils/TableGen/InstrInfoEmitter.cpp
1367 ++index 2f3a2729262..14ab1ea8a72 100644
1368 ++--- a/utils/TableGen/InstrInfoEmitter.cpp
1369 +++++ b/utils/TableGen/InstrInfoEmitter.cpp
1370 ++@@ -744,4 +744,99 @@ void EmitInstrInfo(RecordKeeper &RK, raw_ostream &OS) {
1371 ++ #endif
1372 ++ }
1373 ++
1374 +++#ifdef CAPSTONE
1375 +++std::string GetPublicName(const CodeGenInstruction *Inst) {
1376 +++ std::string Name = Inst->TheDef->getName();
1377 +++ // Apply backward compatibility fixups.
1378 +++ // BRNLE -> BNLER.
1379 +++ if (Name.length() >= 5 && Name.substr(0, 5) == "BRAsm") {
1380 +++ Name = "B" + Name.substr(5, Name.length() - 5) + "R";
1381 +++ }
1382 +++ // SSKEOpt -> SSKE.
1383 +++ while (Name.length() >= 3 && Name.substr(Name.length() - 3, 3) == "Opt") {
1384 +++ Name = Name.substr(0, Name.length() - 3);
1385 +++ }
1386 +++ // BRCLAsm -> BRCL.
1387 +++ while (true) {
1388 +++ size_t pos = Name.find("Asm");
1389 +++ if (pos == std::string::npos) {
1390 +++ break;
1391 +++ }
1392 +++ Name = Name.substr(0, pos) + Name.substr(pos + 3);
1393 +++ }
1394 +++ // CPSDRxx -> CPSDR.
1395 +++ if (Name.length() >= 2) {
1396 +++ std::string Suffix2 = Name.substr(Name.length() - 2, 2);
1397 +++ if (Suffix2 == "dd" || Suffix2 == "ds" ||
1398 +++ Suffix2 == "sd" || Suffix2 == "ss") {
1399 +++ Name = Name.substr(0, Name.length() - 2);
1400 +++ }
1401 +++ }
1402 +++ return "SYSZ_INS_" + Name;
1403 +++}
1404 +++
1405 +++std::string GetRegisterName(Record *Reg) {
1406 +++ std::string Name = Reg->getName();
1407 +++ for (char& c : Name) {
1408 +++ c = toupper(c);
1409 +++ }
1410 +++ // R0L, R0D -> R0.
1411 +++ if (Name.length() >= 3 &&
1412 +++ Name[Name.length() - 3] == 'R' &&
1413 +++ (Name[Name.length() - 1] == 'L' ||
1414 +++ Name[Name.length() - 1] == 'D')) {
1415 +++ Name = Name.substr(0, Name.length() - 3) + Name[Name.length() - 2];
1416 +++ }
1417 +++ return "SYSZ_REG_" + Name;
1418 +++}
1419 +++
1420 +++std::string GetGroupName(Record *Pred) {
1421 +++ std::string Name = Pred->getName();
1422 +++ for (char& c : Name) {
1423 +++ c = toupper(c);
1424 +++ }
1425 +++ if (Name.length() >= 7 && Name.substr(0, 7) == "FEATURE") {
1426 +++ Name = Name.substr(7);
1427 +++ }
1428 +++ return "SYSZ_GRP_" + Name;
1429 +++}
1430 +++
1431 +++void EmitMappingInsn(RecordKeeper &RK, raw_ostream &OS) {
1432 +++ OS << "// This is auto-gen data for Capstone engine (www.capstone-engine.org)\n"
1433 +++ "// By Nguyen Anh Quynh <[email protected]>\n"
1434 +++ "\n";
1435 +++ CodeGenTarget Target(RK);
1436 +++ for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
1437 +++ if (Inst->TheDef->getValueAsBit("isPseudo") ||
1438 +++ Inst->TheDef->getValueAsBit("isCodeGenOnly")) {
1439 +++ continue;
1440 +++ }
1441 +++ OS << "{\n"
1442 +++ << "\t" << Target.getName() << "_" << Inst->TheDef->getName() << ", "
1443 +++ << GetPublicName(Inst) << ",\n"
1444 +++ << "#ifndef CAPSTONE_DIET\n"
1445 +++ << "\t{ ";
1446 +++ for (Record *Use : Inst->TheDef->getValueAsListOfDefs("Uses")) {
1447 +++ OS << GetRegisterName(Use) << ", ";
1448 +++ }
1449 +++ OS << "0 }, { ";
1450 +++ for (Record *Def : Inst->TheDef->getValueAsListOfDefs("Defs")) {
1451 +++ OS << GetRegisterName(Def) << ", ";
1452 +++ }
1453 +++ OS << "0 }, { ";
1454 +++ ListInit *Predicates = Inst->TheDef->getValueAsListInit("Predicates");
1455 +++ for (unsigned i = 0; i < Predicates->size(); ++i) {
1456 +++ OS << GetGroupName(Predicates->getElementAsRecord(i)) << ", ";
1457 +++ }
1458 +++ OS << "0 }, "
1459 +++ << Inst->TheDef->getValueAsBit("isBranch")
1460 +++ << ", "
1461 +++ << Inst->TheDef->getValueAsBit("isIndirectBranch")
1462 +++ << "\n"
1463 +++ << "#endif\n"
1464 +++ << "},\n";
1465 +++ }
1466 +++}
1467 +++#endif
1468 +++
1469 ++ } // end llvm namespace
1470 ++diff --git a/utils/TableGen/TableGen.cpp b/utils/TableGen/TableGen.cpp
1471 ++index cf1404d8769..bbb4e860536 100644
1472 ++--- a/utils/TableGen/TableGen.cpp
1473 +++++ b/utils/TableGen/TableGen.cpp
1474 ++@@ -27,6 +27,7 @@ enum ActionType {
1475 ++ GenEmitter,
1476 ++ GenRegisterInfo,
1477 ++ GenInstrInfo,
1478 +++ MappingInsn,
1479 ++ GenInstrDocs,
1480 ++ GenAsmWriter,
1481 ++ GenAsmMatcher,
1482 ++@@ -65,6 +66,8 @@ namespace {
1483 ++ "Generate registers and register classes info"),
1484 ++ clEnumValN(GenInstrInfo, "gen-instr-info",
1485 ++ "Generate instruction descriptions"),
1486 +++ clEnumValN(MappingInsn, "mapping-insn",
1487 +++ ""),
1488 ++ clEnumValN(GenInstrDocs, "gen-instr-docs",
1489 ++ "Generate instruction documentation"),
1490 ++ clEnumValN(GenCallingConv, "gen-callingconv",
1491 ++@@ -135,6 +138,9 @@ bool LLVMTableGenMain(raw_ostream &OS, RecordKeeper &Records) {
1492 ++ case GenInstrInfo:
1493 ++ EmitInstrInfo(Records, OS);
1494 ++ break;
1495 +++ case MappingInsn:
1496 +++ EmitMappingInsn(Records, OS);
1497 +++ break;
1498 ++ case GenInstrDocs:
1499 ++ EmitInstrDocs(Records, OS);
1500 ++ break;
1501 ++diff --git a/utils/TableGen/TableGenBackends.h b/utils/TableGen/TableGenBackends.h
1502 ++index 1329a6d833f..a41e46b1db0 100644
1503 ++--- a/utils/TableGen/TableGenBackends.h
1504 +++++ b/utils/TableGen/TableGenBackends.h
1505 ++@@ -75,6 +75,7 @@ void EmitDFAPacketizer(RecordKeeper &RK, raw_ostream &OS);
1506 ++ void EmitDisassembler(RecordKeeper &RK, raw_ostream &OS);
1507 ++ void EmitFastISel(RecordKeeper &RK, raw_ostream &OS);
1508 ++ void EmitInstrInfo(RecordKeeper &RK, raw_ostream &OS);
1509 +++void EmitMappingInsn(RecordKeeper &RK, raw_ostream &OS);
1510 ++ void EmitInstrDocs(RecordKeeper &RK, raw_ostream &OS);
1511 ++ void EmitPseudoLowering(RecordKeeper &RK, raw_ostream &OS);
1512 ++ void EmitCompressInst(RecordKeeper &RK, raw_ostream &OS);
1513 ++--
1514 ++2.19.1
1515 ++
1516 +diff --git a/llvm/0007-capstone-generate-GenInsnNameMaps.inc.patch b/llvm/0007-capstone-generate-GenInsnNameMaps.inc.patch
1517 +new file mode 100644
1518 +index 000000000..019540d65
1519 +--- /dev/null
1520 ++++ b/llvm/0007-capstone-generate-GenInsnNameMaps.inc.patch
1521 +@@ -0,0 +1,110 @@
1522 ++From b42f9f2014ec49a22077b6610863d9341a74e142 Mon Sep 17 00:00:00 2001
1523 ++From: mephi42 <[email protected]>
1524 ++Date: Fri, 17 Aug 2018 11:07:39 +0200
1525 ++Subject: [PATCH 7/7] capstone: generate *GenInsnNameMaps.inc
1526 ++
1527 ++---
1528 ++ lib/Target/SystemZ/CMakeLists.txt | 1 +
1529 ++ utils/TableGen/InstrInfoEmitter.cpp | 29 +++++++++++++++++++++++++++++
1530 ++ utils/TableGen/TableGen.cpp | 6 ++++++
1531 ++ utils/TableGen/TableGenBackends.h | 1 +
1532 ++ 4 files changed, 37 insertions(+)
1533 ++
1534 ++diff --git a/lib/Target/SystemZ/CMakeLists.txt b/lib/Target/SystemZ/CMakeLists.txt
1535 ++index 4b5d9c4a3b2..2c64e0a94b8 100644
1536 ++--- a/lib/Target/SystemZ/CMakeLists.txt
1537 +++++ b/lib/Target/SystemZ/CMakeLists.txt
1538 ++@@ -7,6 +7,7 @@ tablegen(LLVM SystemZGenDAGISel.inc -gen-dag-isel)
1539 ++ tablegen(LLVM SystemZGenDisassemblerTables.inc -gen-disassembler)
1540 ++ tablegen(LLVM SystemZGenInstrInfo.inc -gen-instr-info)
1541 ++ tablegen(LLVM SystemZMappingInsn.inc -mapping-insn)
1542 +++tablegen(LLVM SystemZGenInsnNameMaps.inc -gen-insn-name-maps)
1543 ++ tablegen(LLVM SystemZGenMCCodeEmitter.inc -gen-emitter)
1544 ++ tablegen(LLVM SystemZGenRegisterInfo.inc -gen-register-info)
1545 ++ tablegen(LLVM SystemZGenSubtargetInfo.inc -gen-subtarget)
1546 ++diff --git a/utils/TableGen/InstrInfoEmitter.cpp b/utils/TableGen/InstrInfoEmitter.cpp
1547 ++index 14ab1ea8a72..ccf8170ca62 100644
1548 ++--- a/utils/TableGen/InstrInfoEmitter.cpp
1549 +++++ b/utils/TableGen/InstrInfoEmitter.cpp
1550 ++@@ -837,6 +837,35 @@ void EmitMappingInsn(RecordKeeper &RK, raw_ostream &OS) {
1551 ++ << "},\n";
1552 ++ }
1553 ++ }
1554 +++
1555 +++std::string GetMnemonic(const CodeGenInstruction *Inst) {
1556 +++ std::string Mnemonic = Inst->AsmString;
1557 +++
1558 +++ for (size_t i = 0; i < Mnemonic.length(); i++) {
1559 +++ if (Mnemonic[i] == '\t') {
1560 +++ return Mnemonic.substr(0, i);
1561 +++ }
1562 +++ }
1563 +++ return Mnemonic;
1564 +++}
1565 +++
1566 +++void EmitInsnNameMaps(RecordKeeper &RK, raw_ostream &OS) {
1567 +++ OS << "// This is auto-gen data for Capstone engine (www.capstone-engine.org)\n"
1568 +++ "// By Nguyen Anh Quynh <[email protected]>\n"
1569 +++ "\n";
1570 +++ CodeGenTarget Target(RK);
1571 +++ std::map<std::string, std::string> M;
1572 +++ for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
1573 +++ if (Inst->TheDef->getValueAsBit("isPseudo") ||
1574 +++ Inst->TheDef->getValueAsBit("isCodeGenOnly")) {
1575 +++ continue;
1576 +++ }
1577 +++ M[GetPublicName(Inst)] = GetMnemonic(Inst);
1578 +++ }
1579 +++ for (auto &P : M) {
1580 +++ OS << "\t{ " << P.first << ", \"" << P.second << "\" },\n";
1581 +++ }
1582 +++}
1583 ++ #endif
1584 ++
1585 ++ } // end llvm namespace
1586 ++diff --git a/utils/TableGen/TableGen.cpp b/utils/TableGen/TableGen.cpp
1587 ++index bbb4e860536..27c6603de5a 100644
1588 ++--- a/utils/TableGen/TableGen.cpp
1589 +++++ b/utils/TableGen/TableGen.cpp
1590 ++@@ -28,6 +28,7 @@ enum ActionType {
1591 ++ GenRegisterInfo,
1592 ++ GenInstrInfo,
1593 ++ MappingInsn,
1594 +++ GenInsnNameMaps,
1595 ++ GenInstrDocs,
1596 ++ GenAsmWriter,
1597 ++ GenAsmMatcher,
1598 ++@@ -68,6 +69,8 @@ namespace {
1599 ++ "Generate instruction descriptions"),
1600 ++ clEnumValN(MappingInsn, "mapping-insn",
1601 ++ ""),
1602 +++ clEnumValN(GenInsnNameMaps, "gen-insn-name-maps",
1603 +++ ""),
1604 ++ clEnumValN(GenInstrDocs, "gen-instr-docs",
1605 ++ "Generate instruction documentation"),
1606 ++ clEnumValN(GenCallingConv, "gen-callingconv",
1607 ++@@ -141,6 +144,9 @@ bool LLVMTableGenMain(raw_ostream &OS, RecordKeeper &Records) {
1608 ++ case MappingInsn:
1609 ++ EmitMappingInsn(Records, OS);
1610 ++ break;
1611 +++ case GenInsnNameMaps:
1612 +++ EmitInsnNameMaps(Records, OS);
1613 +++ break;
1614 ++ case GenInstrDocs:
1615 ++ EmitInstrDocs(Records, OS);
1616 ++ break;
1617 ++diff --git a/utils/TableGen/TableGenBackends.h b/utils/TableGen/TableGenBackends.h
1618 ++index a41e46b1db0..5656e5be849 100644
1619 ++--- a/utils/TableGen/TableGenBackends.h
1620 +++++ b/utils/TableGen/TableGenBackends.h
1621 ++@@ -76,6 +76,7 @@ void EmitDisassembler(RecordKeeper &RK, raw_ostream &OS);
1622 ++ void EmitFastISel(RecordKeeper &RK, raw_ostream &OS);
1623 ++ void EmitInstrInfo(RecordKeeper &RK, raw_ostream &OS);
1624 ++ void EmitMappingInsn(RecordKeeper &RK, raw_ostream &OS);
1625 +++void EmitInsnNameMaps(RecordKeeper &RK, raw_ostream &OS);
1626 ++ void EmitInstrDocs(RecordKeeper &RK, raw_ostream &OS);
1627 ++ void EmitPseudoLowering(RecordKeeper &RK, raw_ostream &OS);
1628 ++ void EmitCompressInst(RecordKeeper &RK, raw_ostream &OS);
1629 ++--
1630 ++2.19.1
1631 ++
1632 +diff --git a/llvm/lib/Target/RISCV/CMakeLists.txt b/llvm/lib/Target/RISCV/CMakeLists.txt
1633 +index 07c32cb31..1821f4b01 100644
1634 +--- a/llvm/lib/Target/RISCV/CMakeLists.txt
1635 ++++ b/llvm/lib/Target/RISCV/CMakeLists.txt
1636 +@@ -6,6 +6,8 @@ tablegen(LLVM RISCVGenCompressInstEmitter.inc -gen-compress-inst-emitter)
1637 + tablegen(LLVM RISCVGenDAGISel.inc -gen-dag-isel)
1638 + tablegen(LLVM RISCVGenDisassemblerTables.inc -gen-disassembler)
1639 + tablegen(LLVM RISCVGenInstrInfo.inc -gen-instr-info)
1640 ++tablegen(LLVM RISCVGenMappingInsn.inc -gen-mapping-insn)
1641 ++tablegen(LLVM RISCVGenInsnNameMaps.inc -gen-insn-name-maps)
1642 + tablegen(LLVM RISCVGenMCCodeEmitter.inc -gen-emitter)
1643 + tablegen(LLVM RISCVGenMCPseudoLowering.inc -gen-pseudo-lowering)
1644 + tablegen(LLVM RISCVGenRegisterInfo.inc -gen-register-info)
1645 +diff --git a/llvm/utils/TableGen/AsmWriterEmitter.cpp b/llvm/utils/TableGen/AsmWriterEmitter.cpp
1646 +index 24e16f9c4..c24dc6052 100644
1647 +--- a/llvm/utils/TableGen/AsmWriterEmitter.cpp
1648 ++++ b/llvm/utils/TableGen/AsmWriterEmitter.cpp
1649 +@@ -271,16 +271,22 @@ static void UnescapeString(std::string &Str) {
1650 + /// clearing the Instructions vector.
1651 + void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
1652 + Record *AsmWriter = Target.getAsmWriter();
1653 ++#ifndef CAPSTONE
1654 + StringRef ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
1655 ++#endif
1656 + bool PassSubtarget = AsmWriter->getValueAsInt("PassSubtarget");
1657 +
1658 + O <<
1659 + "/// printInstruction - This method is automatically generated by tablegen\n"
1660 + "/// from the instruction set description.\n"
1661 ++#ifdef CAPSTONE
1662 ++ "static void printInstruction(MCInst *MI, SStream *O, MCRegisterInfo *MRI)\n{\n";
1663 ++#else
1664 + "void " << Target.getName() << ClassName
1665 + << "::printInstruction(const MCInst *MI, "
1666 + << (PassSubtarget ? "const MCSubtargetInfo &STI, " : "")
1667 + << "raw_ostream &O) {\n";
1668 ++#endif
1669 +
1670 + // Build an aggregate string, and build a table of offsets into it.
1671 + SequenceToOffsetTable<std::string> StringTable;
1672 +@@ -378,9 +384,16 @@ void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
1673 + }
1674 +
1675 + // Emit the string table itself.
1676 ++#ifdef CAPSTONE
1677 ++ O << "#ifndef CAPSTONE_DIET\n";
1678 ++#endif
1679 + O << " static const char AsmStrs[] = {\n";
1680 + StringTable.emit(O, printChar);
1681 +- O << " };\n\n";
1682 ++ O << " };\n"
1683 ++#ifdef CAPSTONE
1684 ++ << "#endif\n"
1685 ++#endif
1686 ++ << "\n";
1687 +
1688 + // Emit the lookup tables in pieces to minimize wasted bytes.
1689 + unsigned BytesNeeded = ((OpcodeInfoBits - BitsLeft) + 7) / 8;
1690 +@@ -408,21 +421,45 @@ void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
1691 + // If the total bits is more than 32-bits we need to use a 64-bit type.
1692 + if (BitsLeft < (OpcodeInfoBits - 32))
1693 + BitsOS << "(uint64_t)";
1694 +- BitsOS << "OpInfo" << Table << "[MI->getOpcode()] << " << Shift << ";\n";
1695 ++ BitsOS << "OpInfo" << Table << "["
1696 ++#ifdef CAPSTONE
1697 ++ << "MCInst_getOpcode(MI)"
1698 ++#else
1699 ++ << "MI->getOpcode()"
1700 ++#endif
1701 ++ << "] << " << Shift << ";\n";
1702 + // Prepare the shift for the next iteration and increment the table count.
1703 + Shift += TableSize;
1704 + ++Table;
1705 + }
1706 +
1707 + // Emit the initial tab character.
1708 ++#ifndef CAPSTONE
1709 + O << " O << \"\\t\";\n\n";
1710 ++#endif
1711 +
1712 + O << " // Emit the opcode for the instruction.\n";
1713 + O << BitsString;
1714 +
1715 + // Emit the starting string.
1716 +- O << " assert(Bits != 0 && \"Cannot print this instruction.\");\n"
1717 +- << " O << AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << ")-1;\n\n";
1718 ++ O << " "
1719 ++#ifdef CAPSTONE
1720 ++ << "// "
1721 ++#endif
1722 ++ << "assert(Bits != 0 && \"Cannot print this instruction.\");\n"
1723 ++#ifdef CAPSTONE
1724 ++ << "#ifndef CAPSTONE_DIET\n"
1725 ++ << " SStream_concat0(O, "
1726 ++#else
1727 ++ << " O << "
1728 ++#endif
1729 ++ << "AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << ")-1"
1730 ++#ifdef CAPSTONE
1731 ++ << ");\n"
1732 ++ << "#endif\n\n";
1733 ++#else
1734 ++ << ");\n\n";
1735 ++#endif
1736 +
1737 + // Output the table driven operand information.
1738 + BitsLeft = OpcodeInfoBits-AsmStrBits;
1739 +@@ -454,7 +491,11 @@ void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
1740 + O << " switch ((Bits >> "
1741 + << (OpcodeInfoBits-BitsLeft) << ") & "
1742 + << ((1 << NumBits)-1) << ") {\n"
1743 +- << " default: llvm_unreachable(\"Invalid command number.\");\n";
1744 ++ << " default: "
1745 ++#ifdef CAPSTONE
1746 ++ << "assert(0);\n"
1747 ++#endif
1748 ++ << "llvm_unreachable(\"Invalid command number.\");\n";
1749 +
1750 + // Print out all the cases.
1751 + for (unsigned j = 0, e = Commands.size(); j != e; ++j) {
1752 +@@ -535,6 +576,9 @@ emitRegisterNameString(raw_ostream &O, StringRef AltName,
1753 + }
1754 +
1755 + StringTable.layout();
1756 ++#ifdef CAPSTONE
1757 ++ O << "#ifndef CAPSTONE_DIET\n";
1758 ++#endif
1759 + O << " static const char AsmStrs" << AltName << "[] = {\n";
1760 + StringTable.emit(O, printChar);
1761 + O << " };\n\n";
1762 +@@ -551,8 +595,10 @@ emitRegisterNameString(raw_ostream &O, StringRef AltName,
1763 + }
1764 +
1765 + void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
1766 ++#ifndef CAPSTONE
1767 + Record *AsmWriter = Target.getAsmWriter();
1768 + StringRef ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
1769 ++#endif
1770 + const auto &Registers = Target.getRegBank().getRegisters();
1771 + const std::vector<Record*> &AltNameIndices = Target.getRegAltNameIndices();
1772 + bool hasAltNames = AltNameIndices.size() > 1;
1773 +@@ -562,12 +608,22 @@ void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
1774 + "\n\n/// getRegisterName - This method is automatically generated by tblgen\n"
1775 + "/// from the register set description. This returns the assembler name\n"
1776 + "/// for the specified register.\n"
1777 ++#ifdef CAPSTONE
1778 ++ //"static const char *getRegisterName(unsigned RegNo)\n{\n";
1779 ++ "static const char *";
1780 ++ if (hasAltNames)
1781 ++ O << "\ngetRegisterName(unsigned RegNo, unsigned AltIdx)\n{\n";
1782 ++ else
1783 ++ O << "\ngetRegisterName(unsigned RegNo)\n{\n";
1784 ++#else
1785 + "const char *" << Target.getName() << ClassName << "::";
1786 + if (hasAltNames)
1787 + O << "\ngetRegisterName(unsigned RegNo, unsigned AltIdx) {\n";
1788 + else
1789 + O << "getRegisterName(unsigned RegNo) {\n";
1790 +- O << " assert(RegNo && RegNo < " << (Registers.size()+1)
1791 ++#endif
1792 ++ O << " "
1793 ++ << "assert(RegNo && RegNo < " << (Registers.size()+1)
1794 + << " && \"Invalid register number!\");\n"
1795 + << "\n";
1796 +
1797 +@@ -579,12 +635,21 @@ void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
1798 +
1799 + if (hasAltNames) {
1800 + O << " switch(AltIdx) {\n"
1801 ++#ifdef CAPSTONE
1802 ++ << " default: assert(0);\n";
1803 ++#else
1804 + << " default: llvm_unreachable(\"Invalid register alt name index!\");\n";
1805 ++#endif
1806 + for (const Record *R : AltNameIndices) {
1807 + StringRef AltName = R->getName();
1808 + O << " case ";
1809 + if (!Namespace.empty())
1810 +- O << Namespace << "::";
1811 ++ O << Namespace
1812 ++#ifndef CAPSTONE
1813 ++ << "::";
1814 ++#else
1815 ++ << "_";
1816 ++#endif
1817 + O << AltName << ":\n"
1818 + << " assert(*(AsmStrs" << AltName << "+RegAsmOffset" << AltName
1819 + << "[RegNo-1]) &&\n"
1820 +@@ -594,10 +659,22 @@ void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
1821 + }
1822 + O << " }\n";
1823 + } else {
1824 ++#ifdef CAPSTONE
1825 ++ O << " //int i;\n"
1826 ++ << " //for (i = 0; i < sizeof(RegAsmOffset); i++)\n"
1827 ++ << " // printf(\"%s = %u\\n\", AsmStrs+RegAsmOffset[i], i + 1);\n"
1828 ++ << " //printf(\"*************************\\n\");\n"
1829 ++#else
1830 + O << " assert (*(AsmStrs+RegAsmOffset[RegNo-1]) &&\n"
1831 + << " \"Invalid alt name index for register!\");\n"
1832 ++#endif
1833 + << " return AsmStrs+RegAsmOffset[RegNo-1];\n";
1834 + }
1835 ++#ifdef CAPSTONE
1836 ++ O << "#else\n"
1837 ++ << " return NULL;\n"
1838 ++ << "#endif\n";
1839 ++#endif
1840 + O << "}\n";
1841 + }
1842 +
1843 +@@ -1139,9 +1216,21 @@ AsmWriterEmitter::AsmWriterEmitter(RecordKeeper &R) : Records(R), Target(R) {
1844 + }
1845 +
1846 + void AsmWriterEmitter::run(raw_ostream &O) {
1847 ++#ifdef CAPSTONE
1848 ++ O << "/* Capstone Disassembly Engine */\n"
1849 ++ "/* By Nguyen Anh Quynh <[email protected]>, 2013-2015 */\n"
1850 ++ "\n"
1851 ++ "#include <stdio.h>\t// debug\n"
1852 ++ "#include <capstone/platform.h>\n"
1853 ++ "#include <assert.h>\n"
1854 ++ "\n"
1855 ++ "\n";
1856 ++#endif
1857 + EmitPrintInstruction(O);
1858 + EmitGetRegisterName(O);
1859 ++#ifndef CAPSTONE
1860 + EmitPrintAliasInstruction(O);
1861 ++#endif
1862 + }
1863 +
1864 + namespace llvm {
1865 +diff --git a/llvm/utils/TableGen/AsmWriterInst.cpp b/llvm/utils/TableGen/AsmWriterInst.cpp
1866 +index d065a4209..7266d1eda 100644
1867 +--- a/llvm/utils/TableGen/AsmWriterInst.cpp
1868 ++++ b/llvm/utils/TableGen/AsmWriterInst.cpp
1869 +@@ -27,9 +27,13 @@ static bool isIdentChar(char C) {
1870 +
1871 + std::string AsmWriterOperand::getCode(bool PassSubtarget) const {
1872 + if (OperandType == isLiteralTextOperand) {
1873 ++#ifdef CAPSTONE
1874 ++ return "SStream_concat0(O, \"" + Str + "\");";
1875 ++#else
1876 + if (Str.size() == 1)
1877 + return "O << '" + Str + "';";
1878 + return "O << \"" + Str + "\";";
1879 ++#endif
1880 + }
1881 +
1882 + if (OperandType == isLiteralStatementOperand)
1883 +diff --git a/llvm/utils/TableGen/DisassemblerEmitter.cpp b/llvm/utils/TableGen/DisassemblerEmitter.cpp
1884 +index 9e75c7fba..1bc0cfa0d 100644
1885 +--- a/llvm/utils/TableGen/DisassemblerEmitter.cpp
1886 ++++ b/llvm/utils/TableGen/DisassemblerEmitter.cpp
1887 +@@ -105,6 +105,11 @@ extern void EmitFixedLenDecoder(RecordKeeper &RK, raw_ostream &OS,
1888 + void EmitDisassembler(RecordKeeper &Records, raw_ostream &OS) {
1889 + CodeGenTarget Target(Records);
1890 + emitSourceFileHeader(" * " + Target.getName().str() + " Disassembler", OS);
1891 ++#ifdef CAPSTONE
1892 ++ OS << "/* Capstone Disassembly Engine */\n"
1893 ++ "/* By Nguyen Anh Quynh <[email protected]>, 2013-2015 */\n"
1894 ++ "\n";
1895 ++#endif
1896 +
1897 + // X86 uses a custom disassembler.
1898 + if (Target.getName() == "X86") {
1899 +@@ -149,7 +154,12 @@ void EmitDisassembler(RecordKeeper &Records, raw_ostream &OS) {
1900 + }
1901 +
1902 + EmitFixedLenDecoder(Records, OS, Target.getName(),
1903 +- "if (", " == MCDisassembler::Fail)",
1904 ++ "if (",
1905 ++#ifdef CAPSTONE
1906 ++ " == MCDisassembler_Fail)",
1907 ++#else
1908 ++ " == MCDisassembler::Fail)",
1909 ++#endif
1910 + "MCDisassembler::Success", "MCDisassembler::Fail", "");
1911 + }
1912 +
1913 +diff --git a/llvm/utils/TableGen/FixedLenDecoderEmitter.cpp b/llvm/utils/TableGen/FixedLenDecoderEmitter.cpp
1914 +index ea28e06cc..3db428dfa 100644
1915 +--- a/llvm/utils/TableGen/FixedLenDecoderEmitter.cpp
1916 ++++ b/llvm/utils/TableGen/FixedLenDecoderEmitter.cpp
1917 +@@ -741,7 +741,13 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
1918 + ++I;
1919 + unsigned Start = *I++;
1920 + unsigned Len = *I++;
1921 +- OS.indent(Indentation) << "MCD::OPC_ExtractField, " << Start << ", "
1922 ++ OS.indent(Indentation)
1923 ++#ifdef CAPSTONE
1924 ++ << "MCD_OPC_ExtractField"
1925 ++#else
1926 ++ << "MCD::OPC_ExtractField"
1927 ++#endif
1928 ++ << ", " << Start << ", "
1929 + << Len << ", // Inst{";
1930 + if (Len > 1)
1931 + OS << (Start + Len - 1) << "-";
1932 +@@ -750,7 +756,13 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
1933 + }
1934 + case MCD::OPC_FilterValue: {
1935 + ++I;
1936 +- OS.indent(Indentation) << "MCD::OPC_FilterValue, ";
1937 ++ OS.indent(Indentation)
1938 ++#ifdef CAPSTONE
1939 ++ << "MCD_OPC_FilterValue"
1940 ++#else
1941 ++ << "MCD::OPC_FilterValue"
1942 ++#endif
1943 ++ << ", ";
1944 + // The filter value is ULEB128 encoded.
1945 + while (*I >= 128)
1946 + OS << (unsigned)*I++ << ", ";
1947 +@@ -773,7 +785,13 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
1948 + ++I;
1949 + unsigned Start = *I++;
1950 + unsigned Len = *I++;
1951 +- OS.indent(Indentation) << "MCD::OPC_CheckField, " << Start << ", "
1952 ++ OS.indent(Indentation)
1953 ++#ifdef CAPSTONE
1954 ++ << "MCD_OPC_CheckField"
1955 ++#else
1956 ++ << "MCD::OPC_CheckField"
1957 ++#endif
1958 ++ << ", " << Start << ", "
1959 + << Len << ", ";// << Val << ", " << NumToSkip << ",\n";
1960 + // ULEB128 encoded field value.
1961 + for (; *I >= 128; ++I)
1962 +@@ -794,7 +812,13 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
1963 + }
1964 + case MCD::OPC_CheckPredicate: {
1965 + ++I;
1966 +- OS.indent(Indentation) << "MCD::OPC_CheckPredicate, ";
1967 ++ OS.indent(Indentation)
1968 ++#ifdef CAPSTONE
1969 ++ << "MCD_OPC_CheckPredicate"
1970 ++#else
1971 ++ << "MCD::OPC_CheckPredicate"
1972 ++#endif
1973 ++ << ", ";
1974 + for (; *I >= 128; ++I)
1975 + OS << (unsigned)*I << ", ";
1976 + OS << (unsigned)*I++ << ", ";
1977 +@@ -823,7 +847,13 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
1978 + && "ULEB128 value too large!");
1979 + // Decode the Opcode value.
1980 + unsigned Opc = decodeULEB128(Buffer);
1981 +- OS.indent(Indentation) << "MCD::OPC_" << (IsTry ? "Try" : "")
1982 ++ OS.indent(Indentation)
1983 ++#ifdef CAPSTONE
1984 ++ << "MCD_OPC_"
1985 ++#else
1986 ++ << "MCD::OPC_"
1987 ++#endif
1988 ++ << (IsTry ? "Try" : "")
1989 + << "Decode, ";
1990 + for (p = Buffer; *p >= 128; ++p)
1991 + OS << (unsigned)*p << ", ";
1992 +@@ -858,7 +888,12 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
1993 + }
1994 + case MCD::OPC_SoftFail: {
1995 + ++I;
1996 +- OS.indent(Indentation) << "MCD::OPC_SoftFail";
1997 ++ OS.indent(Indentation)
1998 ++#ifdef CAPSTONE
1999 ++ << "MCD_OPC_SoftFail";
2000 ++#else
2001 ++ << "MCD::OPC_SoftFail";
2002 ++#endif
2003 + // Positive mask
2004 + uint64_t Value = 0;
2005 + unsigned Shift = 0;
2006 +@@ -890,7 +925,13 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
2007 + }
2008 + case MCD::OPC_Fail: {
2009 + ++I;
2010 +- OS.indent(Indentation) << "MCD::OPC_Fail,\n";
2011 ++ OS.indent(Indentation)
2012 ++#ifdef CAPSTONE
2013 ++ << "MCD_OPC_Fail"
2014 ++#else
2015 ++ << "MCD::OPC_Fail"
2016 ++#endif
2017 ++ << ",\n";
2018 + break;
2019 + }
2020 + }
2021 +@@ -905,23 +946,46 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
2022 + void FixedLenDecoderEmitter::
2023 + emitPredicateFunction(formatted_raw_ostream &OS, PredicateSet &Predicates,
2024 + unsigned Indentation) const {
2025 ++#ifdef CAPSTONE
2026 ++ OS.indent(Indentation) << "static bool getbool(uint64_t b)\n";
2027 ++ OS.indent(Indentation) << "{\n";
2028 ++ OS.indent(Indentation) << "\treturn b != 0;\n";
2029 ++ OS.indent(Indentation) << "}\n\n";
2030 ++#endif
2031 ++
2032 + // The predicate function is just a big switch statement based on the
2033 + // input predicate index.
2034 + OS.indent(Indentation) << "static bool checkDecoderPredicate(unsigned Idx, "
2035 ++#ifdef CAPSTONE
2036 ++ << "uint64_t Bits)\n{\n";
2037 ++#else
2038 + << "const FeatureBitset& Bits) {\n";
2039 ++#endif
2040 + Indentation += 2;
2041 + if (!Predicates.empty()) {
2042 + OS.indent(Indentation) << "switch (Idx) {\n";
2043 +- OS.indent(Indentation) << "default: llvm_unreachable(\"Invalid index!\");\n";
2044 ++ OS.indent(Indentation) << "default: "
2045 ++#ifdef CAPSTONE
2046 ++ << "assert(0);\n"
2047 ++#endif
2048 ++ << "llvm_unreachable(\"Invalid index!\");\n";
2049 + unsigned Index = 0;
2050 + for (const auto &Predicate : Predicates) {
2051 + OS.indent(Indentation) << "case " << Index++ << ":\n";
2052 +- OS.indent(Indentation+2) << "return (" << Predicate << ");\n";
2053 ++ OS.indent(Indentation+2) << "return "
2054 ++#ifdef CAPSTONE
2055 ++ << "getbool"
2056 ++#endif
2057 ++ << "(" << Predicate << ");\n";
2058 + }
2059 + OS.indent(Indentation) << "}\n";
2060 + } else {
2061 + // No case statement to emit
2062 +- OS.indent(Indentation) << "llvm_unreachable(\"Invalid index!\");\n";
2063 ++ OS.indent(Indentation)
2064 ++#ifdef CAPSTONE
2065 ++ << "assert(0);\n"
2066 ++#endif
2067 ++ << "llvm_unreachable(\"Invalid index!\");\n";
2068 + }
2069 + Indentation -= 2;
2070 + OS.indent(Indentation) << "}\n\n";
2071 +@@ -932,23 +996,39 @@ emitDecoderFunction(formatted_raw_ostream &OS, DecoderSet &Decoders,
2072 + unsigned Indentation) const {
2073 + // The decoder function is just a big switch statement based on the
2074 + // input decoder index.
2075 ++#ifdef CAPSTONE
2076 ++#define EDF_EOL " \\\n"
2077 ++ OS.indent(Indentation) << "#define DecodeToMCInst(fname,fieldname, InsnType) \\\n";
2078 ++ OS.indent(Indentation) << "static DecodeStatus fname(DecodeStatus S, unsigned Idx, InsnType insn, MCInst *MI, \\\n";
2079 ++ OS.indent(Indentation) << " uint64_t Address, const void *Decoder) \\\n";
2080 ++ OS.indent(Indentation) << "{ \\\n";
2081 ++#else
2082 ++#define EDF_EOL "\n"
2083 + OS.indent(Indentation) << "template<typename InsnType>\n";
2084 + OS.indent(Indentation) << "static DecodeStatus decodeToMCInst(DecodeStatus S,"
2085 + << " unsigned Idx, InsnType insn, MCInst &MI,\n";
2086 + OS.indent(Indentation) << " uint64_t "
2087 + << "Address, const void *Decoder, bool &DecodeComplete) {\n";
2088 ++#endif
2089 + Indentation += 2;
2090 ++#ifndef CAPSTONE
2091 + OS.indent(Indentation) << "DecodeComplete = true;\n";
2092 +- OS.indent(Indentation) << "InsnType tmp;\n";
2093 +- OS.indent(Indentation) << "switch (Idx) {\n";
2094 +- OS.indent(Indentation) << "default: llvm_unreachable(\"Invalid index!\");\n";
2095 ++#endif
2096 ++ OS.indent(Indentation) << "InsnType tmp;" EDF_EOL;
2097 ++ OS.indent(Indentation) << "switch (Idx) {" EDF_EOL;
2098 ++ OS.indent(Indentation) << "default:"
2099 ++#ifndef CAPSTONE
2100 ++ << " llvm_unreachable(\"Invalid index!\");\n";
2101 ++#else
2102 ++ << " assert(0);\\\n";
2103 ++#endif
2104 + unsigned Index = 0;
2105 + for (const auto &Decoder : Decoders) {
2106 +- OS.indent(Indentation) << "case " << Index++ << ":\n";
2107 ++ OS.indent(Indentation) << "case " << Index++ << ":" EDF_EOL;
2108 + OS << Decoder;
2109 +- OS.indent(Indentation+2) << "return S;\n";
2110 ++ OS.indent(Indentation+2) << "return S;" EDF_EOL;
2111 + }
2112 +- OS.indent(Indentation) << "}\n";
2113 ++ OS.indent(Indentation) << "}" EDF_EOL;
2114 + Indentation -= 2;
2115 + OS.indent(Indentation) << "}\n\n";
2116 + }
2117 +@@ -1075,16 +1155,21 @@ void FilterChooser::emitBinaryParser(raw_ostream &o, unsigned &Indentation,
2118 + const std::string &Decoder = OpInfo.Decoder;
2119 +
2120 + if (OpInfo.numFields() != 1)
2121 +- o.indent(Indentation) << "tmp = 0;\n";
2122 ++ o.indent(Indentation) << "tmp = 0;" EDF_EOL;
2123 +
2124 + for (const EncodingField &EF : OpInfo) {
2125 + o.indent(Indentation) << "tmp ";
2126 + if (OpInfo.numFields() != 1) o << '|';
2127 +- o << "= fieldFromInstruction"
2128 ++ o << "= "
2129 ++#ifdef CAPSTONE
2130 ++ << "fieldname"
2131 ++#else
2132 ++ << "fieldFromInstruction"
2133 ++#endif
2134 + << "(insn, " << EF.Base << ", " << EF.Width << ')';
2135 + if (OpInfo.numFields() != 1 || EF.Offset != 0)
2136 + o << " << " << EF.Offset;
2137 +- o << ";\n";
2138 ++ o << ";" EDF_EOL;
2139 + }
2140 +
2141 + if (Decoder != "") {
2142 +@@ -1092,8 +1177,12 @@ void FilterChooser::emitBinaryParser(raw_ostream &o, unsigned &Indentation,
2143 + o.indent(Indentation) << Emitter->GuardPrefix << Decoder
2144 + << "(MI, tmp, Address, Decoder)"
2145 + << Emitter->GuardPostfix
2146 ++#ifdef CAPSTONE
2147 ++ << " return MCDisassembler_Fail; \\\n";
2148 ++#else
2149 + << " { " << (OpHasCompleteDecoder ? "" : "DecodeComplete = false; ")
2150 + << "return MCDisassembler::Fail; }\n";
2151 ++#endif
2152 + } else {
2153 + OpHasCompleteDecoder = true;
2154 + o.indent(Indentation) << "MI.addOperand(MCOperand::createImm(tmp));\n";
2155 +@@ -1112,7 +1201,13 @@ void FilterChooser::emitDecoder(raw_ostream &OS, unsigned Indentation,
2156 + << "(MI, insn, Address, Decoder)"
2157 + << Emitter->GuardPostfix
2158 + << " { " << (HasCompleteDecoder ? "" : "DecodeComplete = false; ")
2159 +- << "return MCDisassembler::Fail; }\n";
2160 ++ << "return "
2161 ++#ifdef CAPSTONE
2162 ++ << "MCDisassembler_Fail"
2163 ++#else
2164 ++ << "MCDisassembler::Fail"
2165 ++#endif
2166 ++ << "; }\n";
2167 + break;
2168 + }
2169 +
2170 +@@ -1150,10 +1245,19 @@ unsigned FilterChooser::getDecoderIndex(DecoderSet &Decoders,
2171 + static void emitSinglePredicateMatch(raw_ostream &o, StringRef str,
2172 + const std::string &PredicateNamespace) {
2173 + if (str[0] == '!')
2174 ++#ifdef CAPSTONE
2175 ++ o << "~(Bits & " << PredicateNamespace << "_"
2176 ++ << str.slice(1,str.size()) << ")";
2177 ++#else
2178 + o << "!Bits[" << PredicateNamespace << "::"
2179 + << str.slice(1,str.size()) << "]";
2180 ++#endif
2181 + else
2182 ++#ifdef CAPSTONE
2183 ++ o << "(Bits & " << PredicateNamespace << "_" << str << ")";
2184 ++#else
2185 + o << "Bits[" << PredicateNamespace << "::" << str << "]";
2186 ++#endif
2187 + }
2188 +
2189 + bool FilterChooser::emitPredicateMatch(raw_ostream &o, unsigned &Indentation,
2190 +@@ -2090,7 +2194,18 @@ static void emitFieldFromInstruction(formatted_raw_ostream &OS) {
2191 + << "// * Support shift (<<, >>) with signed and unsigned integers on the "
2192 + "RHS\n"
2193 + << "// * Support put (<<) to raw_ostream&\n"
2194 +- << "template<typename InsnType>\n"
2195 ++#ifdef CAPSTONE
2196 ++ << "#define FieldFromInstruction(fname, InsnType) \\\n"
2197 ++ << "static InsnType fname(InsnType insn, unsigned startBit, unsigned numBits) \\\n"
2198 ++ << "{ \\\n"
2199 ++ << " InsnType fieldMask; \\\n"
2200 ++ << " if (numBits == sizeof(InsnType)*8) \\\n"
2201 ++ << " fieldMask = (InsnType)(-1LL); \\\n"
2202 ++ << " else \\\n"
2203 ++ << " fieldMask = (((InsnType)1 << numBits) - 1) << startBit; \\\n"
2204 ++ << " return (insn & fieldMask) >> startBit; \\\n"
2205 ++#else
2206 ++ << "template<typename InsnType>\n"
2207 + << "#if defined(_MSC_VER) && !defined(__clang__)\n"
2208 + << "__declspec(noinline)\n"
2209 + << "#endif\n"
2210 +@@ -2127,12 +2242,239 @@ static void emitFieldFromInstruction(formatted_raw_ostream &OS) {
2211 + << " unsigned numBits) {\n"
2212 + << " return fieldFromInstruction(insn, startBit, numBits, "
2213 + "std::is_integral<InsnType>());\n"
2214 ++#endif
2215 + << "}\n\n";
2216 + }
2217 +
2218 + // emitDecodeInstruction - Emit the templated helper function
2219 + // decodeInstruction().
2220 + static void emitDecodeInstruction(formatted_raw_ostream &OS) {
2221 ++
2222 ++#if 0
2223 ++ OS << "#define DecodeInstruction(fname, fieldname, decoder, InsnType) \\\n"
2224 ++ << "static DecodeStatus fname(const uint8_t DecodeTable[], MCInst *MI, \\\n"
2225 ++ << " InsnType insn, uint64_t Address, const MCRegisterInfo *MRI, int feature) \\\n"
2226 ++ << "{ \\\n"
2227 ++ << " uint64_t Bits = getFeatureBits(feature); \\\n"
2228 ++ << " const uint8_t *Ptr = DecodeTable; \\\n"
2229 ++ << " uint32_t CurFieldValue = 0, ExpectedValue; \\\n"
2230 ++ << " DecodeStatus S = MCDisassembler_Success; \\\n"
2231 ++ << " unsigned Start, Len, NumToSkip, PIdx, Opc, DecodeIdx; \\\n"
2232 ++ << " InsnType Val, FieldValue, PositiveMask, NegativeMask; \\\n"
2233 ++ << " bool Pred, Fail; \\\n"
2234 ++ << " for (;;) { \\\n"
2235 ++ << " switch (*Ptr) { \\\n"
2236 ++ << " default: \\\n"
2237 ++ << " return MCDisassembler_Fail; \\\n"
2238 ++ << " case MCD_OPC_ExtractField: { \\\n"
2239 ++ << " Start = *++Ptr; \\\n"
2240 ++ << " Len = *++Ptr; \\\n"
2241 ++ << " ++Ptr; \\\n"
2242 ++ << " CurFieldValue = (uint32_t)fieldname(insn, Start, Len); \\\n"
2243 ++ << " break; \\\n"
2244 ++ << " } \\\n"
2245 ++ << " case MCD_OPC_FilterValue: { \\\n"
2246 ++ << " Val = (InsnType)decodeULEB128(++Ptr, &Len); \\\n"
2247 ++ << " Ptr += Len; \\\n"
2248 ++ << " NumToSkip = *Ptr++; \\\n"
2249 ++ << " NumToSkip |= (*Ptr++) << 8; \\\n"
2250 ++ << " if (Val != CurFieldValue) \\\n"
2251 ++ << " Ptr += NumToSkip; \\\n"
2252 ++ << " break; \\\n"
2253 ++ << " } \\\n"
2254 ++ << " case MCD_OPC_CheckField: { \\\n"
2255 ++ << " Start = *++Ptr; \\\n"
2256 ++ << " Len = *++Ptr; \\\n"
2257 ++ << " FieldValue = fieldname(insn, Start, Len); \\\n"
2258 ++ << " ExpectedValue = (uint32_t)decodeULEB128(++Ptr, &Len); \\\n"
2259 ++ << " Ptr += Len; \\\n"
2260 ++ << " NumToSkip = *Ptr++; \\\n"
2261 ++ << " NumToSkip |= (*Ptr++) << 8; \\\n"
2262 ++ << " if (ExpectedValue != FieldValue) \\\n"
2263 ++ << " Ptr += NumToSkip; \\\n"
2264 ++ << " break; \\\n"
2265 ++ << " } \\\n"
2266 ++ << " case MCD_OPC_CheckPredicate: { \\\n"
2267 ++ << " PIdx = (uint32_t)decodeULEB128(++Ptr, &Len); \\\n"
2268 ++ << " Ptr += Len; \\\n"
2269 ++ << " NumToSkip = *Ptr++; \\\n"
2270 ++ << " NumToSkip |= (*Ptr++) << 8; \\\n"
2271 ++ << " Pred = checkDecoderPredicate(PIdx, Bits); \\\n"
2272 ++ << " if (!Pred) \\\n"
2273 ++ << " Ptr += NumToSkip; \\\n"
2274 ++ << " (void)Pred; \\\n"
2275 ++ << " break; \\\n"
2276 ++ << " } \\\n"
2277 ++ << " case MCD_OPC_Decode: { \\\n"
2278 ++ << " Opc = (unsigned)decodeULEB128(++Ptr, &Len); \\\n"
2279 ++ << " Ptr += Len; \\\n"
2280 ++ << " DecodeIdx = (unsigned)decodeULEB128(Ptr, &Len); \\\n"
2281 ++ << " Ptr += Len; \\\n"
2282 ++ << " MCInst_setOpcode(MI, Opc); \\\n"
2283 ++ << " return decoder(S, DecodeIdx, insn, MI, Address, MRI); \\\n"
2284 ++ << " } \\\n"
2285 ++ << " case MCD_OPC_SoftFail: { \\\n"
2286 ++ << " PositiveMask = (InsnType)decodeULEB128(++Ptr, &Len); \\\n"
2287 ++ << " Ptr += Len; \\\n"
2288 ++ << " NegativeMask = (InsnType)decodeULEB128(Ptr, &Len); \\\n"
2289 ++ << " Ptr += Len; \\\n"
2290 ++ << " Fail = (insn & PositiveMask) || (~insn & NegativeMask); \\\n"
2291 ++ << " if (Fail) \\\n"
2292 ++ << " S = MCDisassembler_SoftFail; \\\n"
2293 ++ << " break; \\\n"
2294 ++ << " } \\\n"
2295 ++ << " case MCD_OPC_Fail: { \\\n"
2296 ++ << " return MCDisassembler_Fail; \\\n"
2297 ++ << " } \\\n"
2298 ++ << " } \\\n"
2299 ++ << " } \\\n"
2300 ++#endif
2301 ++
2302 ++#ifdef CAPSTONE
2303 ++ OS << "static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], "
2304 ++
2305 ++ "MCInst *MI,\\\n"
2306 ++ << " InsnType insn, uint64_t "
2307 ++ "Address,\\\n"
2308 ++ << " const void *DisAsm,\\\n"
2309 ++ << " int feature) {\\\n"
2310 ++ << " uint64_t Bits = getFeatureBits(feature); \\\n"
2311 ++ //<< " const FeatureBitset& Bits = STI.getFeatureBits();\n"
2312 ++ << "\\\n"
2313 ++ << " const uint8_t *Ptr = DecodeTable;\\\n"
2314 ++ << " uint32_t CurFieldValue = 0;\\\n"
2315 ++ << " DecodeStatus S = MCDisassembler_Success;\\\n"
2316 ++ << " while (true) {\\\n"
2317 ++ << " ptrdiff_t Loc = Ptr - DecodeTable;\\\n"
2318 ++ << " switch (*Ptr) {\\\n"
2319 ++ << " default:\\\n"
2320 ++ << " return MCDisassembler_Fail;\\\n"
2321 ++ << " case MCD_OPC_ExtractField: {\\\n"
2322 ++ << " unsigned Start = *++Ptr;\\\n"
2323 ++ << " unsigned Len = *++Ptr;\\\n"
2324 ++ << " ++Ptr;\\\n"
2325 ++ << " CurFieldValue = fieldFromInstruction(insn, Start, Len);\\\n"
2326 ++ << " break;\\\n"
2327 ++ << " }\\\n"
2328 ++ << " case MCD_OPC_FilterValue: {\\\n"
2329 ++ << " // Decode the field value.\\\n"
2330 ++ << " unsigned Len;\\\n"
2331 ++ << " InsnType Val = decodeULEB128(++Ptr, &Len);\\\n"
2332 ++ << " Ptr += Len;\\\n"
2333 ++ << " // NumToSkip is a plain 24-bit integer.\\\n"
2334 ++ << " unsigned NumToSkip = *Ptr++;\\\n"
2335 ++ << " NumToSkip |= (*Ptr++) << 8;\\\n"
2336 ++ << " NumToSkip |= (*Ptr++) << 16;\\\n"
2337 ++ << "\\\n"
2338 ++ << " // Perform the filter operation.\\\n"
2339 ++ << " if (Val != CurFieldValue)\\\n"
2340 ++ << " Ptr += NumToSkip;\\\n"
2341 ++ << "\\\n"
2342 ++ << " break;\\\n"
2343 ++ << " }\\\n"
2344 ++ << " case MCD_OPC_CheckField: {\\\n"
2345 ++ << " unsigned Start = *++Ptr;\\\n"
2346 ++ << " unsigned Len = *++Ptr;\\\n"
2347 ++ << " InsnType FieldValue = fieldFromInstruction(insn, Start, Len);\\\n"
2348 ++ << " // Decode the field value.\\\n"
2349 ++ << " uint32_t ExpectedValue = decodeULEB128(++Ptr, &Len);\\\n"
2350 ++ << " Ptr += Len;\\\n"
2351 ++ << " // NumToSkip is a plain 24-bit integer.\\\n"
2352 ++ << " unsigned NumToSkip = *Ptr++;\\\n"
2353 ++ << " NumToSkip |= (*Ptr++) << 8;\\\n"
2354 ++ << " NumToSkip |= (*Ptr++) << 16;\\\n"
2355 ++ << "\\\n"
2356 ++ << " // If the actual and expected values don't match, skip.\\\n"
2357 ++ << " if (ExpectedValue != FieldValue)\\\n"
2358 ++ << " Ptr += NumToSkip;\\\n"
2359 ++ << " break;\\\n"
2360 ++ << " }\\\n"
2361 ++ << " case MCD_OPC_CheckPredicate: {\\\n"
2362 ++ << " unsigned Len;\\\n"
2363 ++ << " // Decode the Predicate Index value.\\\n"
2364 ++ << " unsigned PIdx = decodeULEB128(++Ptr, &Len);\\\n"
2365 ++ << " Ptr += Len;\\\n"
2366 ++ << " // NumToSkip is a plain 24-bit integer.\\\n"
2367 ++ << " unsigned NumToSkip = *Ptr++;\\\n"
2368 ++ << " NumToSkip |= (*Ptr++) << 8;\\\n"
2369 ++ << " NumToSkip |= (*Ptr++) << 16;\\\n"
2370 ++ << " // Check the predicate.\\\n"
2371 ++ << " bool Pred;\\\n"
2372 ++ << " if (!(Pred = checkDecoderPredicate(PIdx, Bits)))\\\n"
2373 ++ << " Ptr += NumToSkip;\\\n"
2374 ++ << " (void)Pred;\\\n"
2375 ++ << " break;\\\n"
2376 ++ << " }\\\n"
2377 ++ << " case MCD_OPC_Decode: {\\\n"
2378 ++ << " unsigned Len;\\\n"
2379 ++ << " // Decode the Opcode value.\\\n"
2380 ++ << " unsigned Opc = decodeULEB128(++Ptr, &Len);\\\n"
2381 ++ << " Ptr += Len;\\\n"
2382 ++ << " unsigned DecodeIdx = decodeULEB128(Ptr, &Len);\\\n"
2383 ++ << " Ptr += Len;\\\n"
2384 ++ << "\\\n"
2385 ++ << " MCInst_clear(MI);\\\n"
2386 ++ << " MCInst_setOpcode(MI, Opc);\\\n"
2387 ++ << " bool DecodeComplete;\\\n"
2388 ++ << " S = decodeToMCInst(S, DecodeIdx, insn, MI, Address, DisAsm, "
2389 ++ "DecodeComplete);\\\n"
2390 ++ << " assert(DecodeComplete);\\\n"
2391 ++ << "\\\n"
2392 ++ << " return S;\\\n"
2393 ++ << " }\\\n"
2394 ++ << " case MCD_OPC_TryDecode: {\\\n"
2395 ++ << " unsigned Len;\\\n"
2396 ++ << " // Decode the Opcode value.\\\n"
2397 ++ << " unsigned Opc = decodeULEB128(++Ptr, &Len);\\\n"
2398 ++ << " Ptr += Len;\\\n"
2399 ++ << " unsigned DecodeIdx = decodeULEB128(Ptr, &Len);\\\n"
2400 ++ << " Ptr += Len;\\\n"
2401 ++ << " // NumToSkip is a plain 24-bit integer.\\\n"
2402 ++ << " unsigned NumToSkip = *Ptr++;\\\n"
2403 ++ << " NumToSkip |= (*Ptr++) << 8;\\\n"
2404 ++ << " NumToSkip |= (*Ptr++) << 16;\\\n"
2405 ++ << "\\\n"
2406 ++ << " // Perform the decode operation.\\\n"
2407 ++ << " MCInst TmpMI;\\\n"
2408 ++ << " MCInst_setOpcode(&TmpMI, Opc);\\\n"
2409 ++ << " bool DecodeComplete;\n"
2410 ++ << " S = decodeToMCInst(S, DecodeIdx, insn, &TmpMI, Address, DisAsm, "
2411 ++ "DecodeComplete);\\\n"
2412 ++ << " if (DecodeComplete) {\\\n"
2413 ++ << " // Decoding complete.\\\n"
2414 ++ << " MI = &TmpMI;\\\n"
2415 ++ << " return S;\\\n"
2416 ++ << " } else {\\\n"
2417 ++ << " assert(S == MCDisassembler_Fail);\\\n"
2418 ++ << " // If the decoding was incomplete, skip.\\\n"
2419 ++ << " Ptr += NumToSkip;\\\n"
2420 ++ << " // Reset decode status. This also drops a SoftFail status "
2421 ++ "that could be\\\n"
2422 ++ << " // set before the decode attempt.\\\n"
2423 ++ << " S = MCDisassembler_Success;\\\n"
2424 ++ << " }\\\n"
2425 ++ << " break;\\\n"
2426 ++ << " }\\\n"
2427 ++ << " case MCD_OPC_SoftFail: {\\\n"
2428 ++ << " // Decode the mask values.\\\n"
2429 ++ << " unsigned Len;\\\n"
2430 ++ << " InsnType PositiveMask = decodeULEB128(++Ptr, &Len);\\\n"
2431 ++ << " Ptr += Len;\\\n"
2432 ++ << " InsnType NegativeMask = decodeULEB128(Ptr, &Len);\\\n"
2433 ++ << " Ptr += Len;\\\n"
2434 ++ << " bool Fail = (insn & PositiveMask) || (~insn & NegativeMask);\\\n"
2435 ++ << " if (Fail)\\\n"
2436 ++ << " S = MCDisassembler_SoftFail;\\\n"
2437 ++ << " break;\\\n"
2438 ++ << " }\\\n"
2439 ++ << " case MCD_OPC_Fail: {\\\n"
2440 ++ << " return MCDisassembler_Fail;\\\n"
2441 ++ << " }\\\n"
2442 ++ << " }\\\n"
2443 ++ << " }\\\n"
2444 ++ << " assert(0);\\\n"
2445 ++
2446 ++#else
2447 + OS << "template<typename InsnType>\n"
2448 + << "static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], "
2449 + "MCInst &MI,\n"
2450 +@@ -2313,12 +2655,19 @@ static void emitDecodeInstruction(formatted_raw_ostream &OS) {
2451 + << " }\n"
2452 + << " llvm_unreachable(\"bogosity detected in disassembler state "
2453 + "machine!\");\n"
2454 ++#endif
2455 + << "}\n\n";
2456 + }
2457 +
2458 + // Emits disassembler code for instruction decoding.
2459 + void FixedLenDecoderEmitter::run(raw_ostream &o) {
2460 + formatted_raw_ostream OS(o);
2461 ++#ifdef CAPSTONE
2462 ++ OS << "#include \"../../MCInst.h\"\n";
2463 ++ OS << "#include \"../../LEB128.h\"\n";
2464 ++ OS << "#include <assert.h>\n";
2465 ++ OS << "\n";
2466 ++#else
2467 + OS << "#include \"llvm/MC/MCInst.h\"\n";
2468 + OS << "#include \"llvm/Support/Debug.h\"\n";
2469 + OS << "#include \"llvm/Support/DataTypes.h\"\n";
2470 +@@ -2327,6 +2676,7 @@ void FixedLenDecoderEmitter::run(raw_ostream &o) {
2471 + OS << "#include <assert.h>\n";
2472 + OS << '\n';
2473 + OS << "namespace llvm {\n\n";
2474 ++#endif
2475 +
2476 + emitFieldFromInstruction(OS);
2477 +
2478 +@@ -2401,7 +2751,13 @@ void FixedLenDecoderEmitter::run(raw_ostream &o) {
2479 + // Emit the main entry point for the decoder, decodeInstruction().
2480 + emitDecodeInstruction(OS);
2481 +
2482 ++#ifdef CAPSTONE
2483 ++ OS << "FieldFromInstruction(fieldFromInstruction, uint64_t)\n";
2484 ++ OS << "DecodeToMCInst(decodeToMCInst, fieldFromInstruction, uint64_t)\n";
2485 ++ OS << "DecodeInstruction(decodeInstruction, fieldFromInstruction, decodeToMCInst, uint64_t)\n";
2486 ++#else
2487 + OS << "\n} // End llvm namespace\n";
2488 ++#endif
2489 + }
2490 +
2491 + namespace llvm {
2492 +diff --git a/llvm/utils/TableGen/InstrInfoEmitter.cpp b/llvm/utils/TableGen/InstrInfoEmitter.cpp
2493 +index fd8775023..797f42a50 100644
2494 +--- a/llvm/utils/TableGen/InstrInfoEmitter.cpp
2495 ++++ b/llvm/utils/TableGen/InstrInfoEmitter.cpp
2496 +@@ -92,6 +92,7 @@ private:
2497 +
2498 + } // end anonymous namespace
2499 +
2500 ++#ifndef CAPSTONE
2501 + static void PrintDefList(const std::vector<Record*> &Uses,
2502 + unsigned Num, raw_ostream &OS) {
2503 + OS << "static const MCPhysReg ImplicitList" << Num << "[] = { ";
2504 +@@ -99,6 +100,7 @@ static void PrintDefList(const std::vector<Record*> &Uses,
2505 + OS << getQualifiedName(U) << ", ";
2506 + OS << "0 };\n";
2507 + }
2508 ++#endif
2509 +
2510 + //===----------------------------------------------------------------------===//
2511 + // Operand Info Emission.
2512 +@@ -434,8 +436,17 @@ void InstrInfoEmitter::emitTIIHelperMethods(raw_ostream &OS,
2513 + // run - Emit the main instruction description records for the target...
2514 + void InstrInfoEmitter::run(raw_ostream &OS) {
2515 + emitSourceFileHeader("Target Instruction Enum Values and Descriptors", OS);
2516 ++
2517 ++#ifdef CAPSTONE
2518 ++ OS << "/* Capstone Disassembly Engine */\n"
2519 ++ "/* By Nguyen Anh Quynh <[email protected]>, 2013-2015 */\n"
2520 ++ "\n"
2521 ++ "\n";
2522 ++#endif
2523 ++
2524 + emitEnums(OS);
2525 +
2526 ++#ifndef CAPSTONE
2527 + OS << "#ifdef GET_INSTRINFO_MC_DESC\n";
2528 + OS << "#undef GET_INSTRINFO_MC_DESC\n";
2529 +
2530 +@@ -563,6 +574,7 @@ void InstrInfoEmitter::run(raw_ostream &OS) {
2531 + emitOperandTypesEnum(OS, Target);
2532 +
2533 + emitMCIIHelperMethods(OS, TargetName);
2534 ++#endif
2535 + }
2536 +
2537 + void InstrInfoEmitter::emitRecord(const CodeGenInstruction &Inst, unsigned Num,
2538 +@@ -680,7 +692,9 @@ void InstrInfoEmitter::emitEnums(raw_ostream &OS) {
2539 + OS << "#ifdef GET_INSTRINFO_ENUM\n";
2540 + OS << "#undef GET_INSTRINFO_ENUM\n";
2541 +
2542 ++#ifndef CAPSTONE
2543 + OS << "namespace llvm {\n\n";
2544 ++#endif
2545 +
2546 + CodeGenTarget Target(Records);
2547 +
2548 +@@ -690,17 +704,39 @@ void InstrInfoEmitter::emitEnums(raw_ostream &OS) {
2549 + if (Namespace.empty())
2550 + PrintFatalError("No instructions defined!");
2551 +
2552 ++#ifndef CAPSTONE
2553 + OS << "namespace " << Namespace << " {\n";
2554 +- OS << " enum {\n";
2555 ++#endif
2556 ++#ifdef CAPSTONE
2557 ++ OS << "\n"
2558 ++#else
2559 ++ OS << " "
2560 ++#endif
2561 ++ << "enum {\n";
2562 + unsigned Num = 0;
2563 + for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue())
2564 +- OS << " " << Inst->TheDef->getName() << "\t= " << Num++ << ",\n";
2565 +- OS << " INSTRUCTION_LIST_END = " << Num << "\n";
2566 ++ OS << " "
2567 ++#ifdef CAPSTONE
2568 ++ << Target.getName() << "_"
2569 ++#endif
2570 ++ << Inst->TheDef->getName() << "\t= " << Num++ << ",\n";
2571 ++ OS << " "
2572 ++#ifdef CAPSTONE
2573 ++ << Target.getName() << "_"
2574 ++#endif
2575 ++ << "INSTRUCTION_LIST_END = " << Num << "\n";
2576 + OS << " };\n\n";
2577 ++#ifndef CAPSTONE
2578 + OS << "} // end " << Namespace << " namespace\n";
2579 + OS << "} // end llvm namespace\n";
2580 +- OS << "#endif // GET_INSTRINFO_ENUM\n\n";
2581 +-
2582 ++#endif
2583 ++ OS << "#endif // GET_INSTRINFO_ENUM\n"
2584 ++#ifndef CAPSTONE
2585 ++ << "\n"
2586 ++#endif
2587 ++ ;
2588 ++
2589 ++#ifndef CAPSTONE
2590 + OS << "#ifdef GET_INSTRINFO_SCHED_ENUM\n";
2591 + OS << "#undef GET_INSTRINFO_SCHED_ENUM\n";
2592 + OS << "namespace llvm {\n\n";
2593 +@@ -717,13 +753,140 @@ void InstrInfoEmitter::emitEnums(raw_ostream &OS) {
2594 + OS << "} // end llvm namespace\n";
2595 +
2596 + OS << "#endif // GET_INSTRINFO_SCHED_ENUM\n\n";
2597 ++#endif
2598 + }
2599 +
2600 + namespace llvm {
2601 +
2602 + void EmitInstrInfo(RecordKeeper &RK, raw_ostream &OS) {
2603 + InstrInfoEmitter(RK).run(OS);
2604 ++#ifndef CAPSTONE
2605 + EmitMapTable(RK, OS);
2606 ++#endif
2607 ++}
2608 ++
2609 ++#ifdef CAPSTONE
2610 ++std::string GetPublicName(const CodeGenInstruction *Inst) {
2611 ++ std::string Name = Inst->TheDef->getName();
2612 ++ // Apply backward compatibility fixups.
2613 ++ // BRNLE -> BNLER.
2614 ++ if (Name.length() >= 5 && Name.substr(0, 5) == "BRAsm") {
2615 ++ Name = "B" + Name.substr(5, Name.length() - 5) + "R";
2616 ++ }
2617 ++ // SSKEOpt -> SSKE.
2618 ++ while (Name.length() >= 3 && Name.substr(Name.length() - 3, 3) == "Opt") {
2619 ++ Name = Name.substr(0, Name.length() - 3);
2620 ++ }
2621 ++ // BRCLAsm -> BRCL.
2622 ++ while (true) {
2623 ++ size_t pos = Name.find("Asm");
2624 ++ if (pos == std::string::npos) {
2625 ++ break;
2626 ++ }
2627 ++ Name = Name.substr(0, pos) + Name.substr(pos + 3);
2628 ++ }
2629 ++ // CPSDRxx -> CPSDR.
2630 ++ if (Name.length() >= 2) {
2631 ++ std::string Suffix2 = Name.substr(Name.length() - 2, 2);
2632 ++ if (Suffix2 == "dd" || Suffix2 == "ds" ||
2633 ++ Suffix2 == "sd" || Suffix2 == "ss") {
2634 ++ Name = Name.substr(0, Name.length() - 2);
2635 ++ }
2636 ++ }
2637 ++ return "SYSZ_INS_" + Name;
2638 ++}
2639 ++
2640 ++std::string GetRegisterName(Record *Reg) {
2641 ++ std::string Name = Reg->getName();
2642 ++ for (char& c : Name) {
2643 ++ c = toupper(c);
2644 ++ }
2645 ++ // R0L, R0D -> R0.
2646 ++ if (Name.length() >= 3 &&
2647 ++ Name[Name.length() - 3] == 'R' &&
2648 ++ (Name[Name.length() - 1] == 'L' ||
2649 ++ Name[Name.length() - 1] == 'D')) {
2650 ++ Name = Name.substr(0, Name.length() - 3) + Name[Name.length() - 2];
2651 ++ }
2652 ++ return "SYSZ_REG_" + Name;
2653 ++}
2654 ++
2655 ++std::string GetGroupName(Record *Pred) {
2656 ++ std::string Name = Pred->getName();
2657 ++ for (char& c : Name) {
2658 ++ c = toupper(c);
2659 ++ }
2660 ++ if (Name.length() >= 7 && Name.substr(0, 7) == "FEATURE") {
2661 ++ Name = Name.substr(7);
2662 ++ }
2663 ++ return "SYSZ_GRP_" + Name;
2664 ++}
2665 ++
2666 ++void EmitMappingInsn(RecordKeeper &RK, raw_ostream &OS) {
2667 ++ OS << "// This is auto-gen data for Capstone engine (www.capstone-engine.org)\n"
2668 ++ "// By Nguyen Anh Quynh <[email protected]>\n"
2669 ++ "\n";
2670 ++ CodeGenTarget Target(RK);
2671 ++ for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
2672 ++ if (Inst->TheDef->getValueAsBit("isPseudo") ||
2673 ++ Inst->TheDef->getValueAsBit("isCodeGenOnly")) {
2674 ++ continue;
2675 ++ }
2676 ++ OS << "{\n"
2677 ++ << "\t" << Target.getName() << "_" << Inst->TheDef->getName() << ", "
2678 ++ << GetPublicName(Inst) << ",\n"
2679 ++ << "#ifndef CAPSTONE_DIET\n"
2680 ++ << "\t{ ";
2681 ++ for (Record *Use : Inst->TheDef->getValueAsListOfDefs("Uses")) {
2682 ++ OS << GetRegisterName(Use) << ", ";
2683 ++ }
2684 ++ OS << "0 }, { ";
2685 ++ for (Record *Def : Inst->TheDef->getValueAsListOfDefs("Defs")) {
2686 ++ OS << GetRegisterName(Def) << ", ";
2687 ++ }
2688 ++ OS << "0 }, { ";
2689 ++ ListInit *Predicates = Inst->TheDef->getValueAsListInit("Predicates");
2690 ++ for (unsigned i = 0; i < Predicates->size(); ++i) {
2691 ++ OS << GetGroupName(Predicates->getElementAsRecord(i)) << ", ";
2692 ++ }
2693 ++ OS << "0 }, "
2694 ++ << Inst->TheDef->getValueAsBit("isBranch")
2695 ++ << ", "
2696 ++ << Inst->TheDef->getValueAsBit("isIndirectBranch")
2697 ++ << "\n"
2698 ++ << "#endif\n"
2699 ++ << "},\n";
2700 ++ }
2701 ++}
2702 ++
2703 ++std::string GetMnemonic(const CodeGenInstruction *Inst) {
2704 ++ std::string Mnemonic = Inst->AsmString;
2705 ++
2706 ++ for (size_t i = 0; i < Mnemonic.length(); i++) {
2707 ++ if (Mnemonic[i] == '\t') {
2708 ++ return Mnemonic.substr(0, i);
2709 ++ }
2710 ++ }
2711 ++ return Mnemonic;
2712 ++}
2713 ++
2714 ++void EmitInsnNameMaps(RecordKeeper &RK, raw_ostream &OS) {
2715 ++ OS << "// This is auto-gen data for Capstone engine (www.capstone-engine.org)\n"
2716 ++ "// By Nguyen Anh Quynh <[email protected]>\n"
2717 ++ "\n";
2718 ++ CodeGenTarget Target(RK);
2719 ++ std::map<std::string, std::string> M;
2720 ++ for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
2721 ++ if (Inst->TheDef->getValueAsBit("isPseudo") ||
2722 ++ Inst->TheDef->getValueAsBit("isCodeGenOnly")) {
2723 ++ continue;
2724 ++ }
2725 ++ M[GetPublicName(Inst)] = GetMnemonic(Inst);
2726 ++ }
2727 ++ for (auto &P : M) {
2728 ++ OS << "\t{ " << P.first << ", \"" << P.second << "\" },\n";
2729 ++ }
2730 + }
2731 ++#endif
2732 +
2733 + } // end llvm namespace
2734 +diff --git a/llvm/utils/TableGen/RegisterInfoEmitter.cpp b/llvm/utils/TableGen/RegisterInfoEmitter.cpp
2735 +index 1b619072c..0df306680 100644
2736 +--- a/llvm/utils/TableGen/RegisterInfoEmitter.cpp
2737 ++++ b/llvm/utils/TableGen/RegisterInfoEmitter.cpp
2738 +@@ -98,6 +98,12 @@ private:
2739 +
2740 + } // end anonymous namespace
2741 +
2742 ++#ifdef CAPSTONE
2743 ++#define NAME_PREFIX Target.getName() << "_" <<
2744 ++#else
2745 ++#define NAME_PREFIX
2746 ++#endif
2747 ++
2748 + // runEnums - Print out enum values for all of the registers.
2749 + void RegisterInfoEmitter::runEnums(raw_ostream &OS,
2750 + CodeGenTarget &Target, CodeGenRegBank &Bank) {
2751 +@@ -106,13 +112,22 @@ void RegisterInfoEmitter::runEnums(raw_ostream &OS,
2752 + // Register enums are stored as uint16_t in the tables. Make sure we'll fit.
2753 + assert(Registers.size() <= 0xffff && "Too many regs to fit in tables");
2754 +
2755 ++#ifndef CAPSTONE
2756 + StringRef Namespace = Registers.front().TheDef->getValueAsString("Namespace");
2757 ++#endif
2758 +
2759 + emitSourceFileHeader("Target Register Enum Values", OS);
2760 +
2761 ++#ifdef CAPSTONE
2762 ++ OS << "/* Capstone Disassembly Engine */\n"
2763 ++ "/* By Nguyen Anh Quynh <[email protected]>, 2013-2015 */\n"
2764 ++ "\n";
2765 ++#endif
2766 ++
2767 + OS << "\n#ifdef GET_REGINFO_ENUM\n";
2768 + OS << "#undef GET_REGINFO_ENUM\n\n";
2769 +
2770 ++#ifndef CAPSTONE
2771 + OS << "namespace llvm {\n\n";
2772 +
2773 + OS << "class MCRegisterClass;\n"
2774 +@@ -121,16 +136,20 @@ void RegisterInfoEmitter::runEnums(raw_ostream &OS,
2775 +
2776 + if (!Namespace.empty())
2777 + OS << "namespace " << Namespace << " {\n";
2778 +- OS << "enum {\n NoRegister,\n";
2779 ++#endif
2780 ++
2781 ++ OS << "enum {\n " << NAME_PREFIX "NoRegister,\n";
2782 +
2783 + for (const auto &Reg : Registers)
2784 +- OS << " " << Reg.getName() << " = " << Reg.EnumValue << ",\n";
2785 ++ OS << " " << NAME_PREFIX Reg.getName() << " = " << Reg.EnumValue << ",\n";
2786 + assert(Registers.size() == Registers.back().EnumValue &&
2787 + "Register enum value mismatch!");
2788 +- OS << " NUM_TARGET_REGS \t// " << Registers.size()+1 << "\n";
2789 ++ OS << " " << NAME_PREFIX "NUM_TARGET_REGS \t// " << Registers.size()+1 << "\n";
2790 + OS << "};\n";
2791 ++#ifndef CAPSTONE
2792 + if (!Namespace.empty())
2793 + OS << "} // end namespace " << Namespace << "\n";
2794 ++#endif
2795 +
2796 + const auto &RegisterClasses = Bank.getRegClasses();
2797 + if (!RegisterClasses.empty()) {
2798 +@@ -139,18 +158,29 @@ void RegisterInfoEmitter::runEnums(raw_ostream &OS,
2799 + assert(RegisterClasses.size() <= 0xffff &&
2800 + "Too many register classes to fit in tables");
2801 +
2802 +- OS << "\n// Register classes\n\n";
2803 ++ OS << "\n// Register classes\n";
2804 ++#ifndef CAPSTONE
2805 ++ OS << "\n";
2806 + if (!Namespace.empty())
2807 + OS << "namespace " << Namespace << " {\n";
2808 ++#endif
2809 + OS << "enum {\n";
2810 + for (const auto &RC : RegisterClasses)
2811 +- OS << " " << RC.getName() << "RegClassID"
2812 ++ OS << " " << NAME_PREFIX RC.getName() << "RegClassID"
2813 + << " = " << RC.EnumValue << ",\n";
2814 +- OS << "\n };\n";
2815 ++#ifdef CAPSTONE
2816 ++ OS
2817 ++#else
2818 ++ OS << "\n "
2819 ++#endif
2820 ++ << "};\n";
2821 ++#ifndef CAPSTONE
2822 + if (!Namespace.empty())
2823 + OS << "} // end namespace " << Namespace << "\n\n";
2824 ++#endif
2825 + }
2826 +
2827 ++#ifndef CAPSTONE
2828 + const std::vector<Record*> &RegAltNameIndices = Target.getRegAltNameIndices();
2829 + // If the only definition is the default NoRegAltName, we don't need to
2830 + // emit anything.
2831 +@@ -181,8 +211,11 @@ void RegisterInfoEmitter::runEnums(raw_ostream &OS,
2832 + if (!Namespace.empty())
2833 + OS << "} // end namespace " << Namespace << "\n\n";
2834 + }
2835 ++#endif
2836 +
2837 ++#ifndef CAPSTONE
2838 + OS << "} // end namespace llvm\n\n";
2839 ++#endif
2840 + OS << "#endif // GET_REGINFO_ENUM\n\n";
2841 + }
2842 +
2843 +@@ -869,7 +902,9 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
2844 +
2845 + const auto &Regs = RegBank.getRegisters();
2846 +
2847 ++#ifndef CAPSTONE
2848 + auto &SubRegIndices = RegBank.getSubRegIndices();
2849 ++#endif
2850 + // The lists of sub-registers and super-registers go in the same array. That
2851 + // allows us to share suffixes.
2852 + typedef std::vector<const CodeGenRegister*> RegVec;
2853 +@@ -961,25 +996,40 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
2854 + LaneMaskSeqs.layout();
2855 + SubRegIdxSeqs.layout();
2856 +
2857 ++#ifndef CAPSTONE
2858 + OS << "namespace llvm {\n\n";
2859 ++#endif
2860 +
2861 + const std::string &TargetName = Target.getName();
2862 +
2863 + // Emit the shared table of differential lists.
2864 +- OS << "extern const MCPhysReg " << TargetName << "RegDiffLists[] = {\n";
2865 ++#ifdef CAPSTONE
2866 ++ OS << "static"
2867 ++#else
2868 ++ OS << "extern"
2869 ++#endif
2870 ++ << " const MCPhysReg " << TargetName << "RegDiffLists[] = {\n";
2871 + DiffSeqs.emit(OS, printDiff16);
2872 + OS << "};\n\n";
2873 +
2874 ++#ifndef CAPSTONE
2875 + // Emit the shared table of regunit lane mask sequences.
2876 + OS << "extern const LaneBitmask " << TargetName << "LaneMaskLists[] = {\n";
2877 + LaneMaskSeqs.emit(OS, printMask, "LaneBitmask::getAll()");
2878 + OS << "};\n\n";
2879 ++#endif
2880 +
2881 + // Emit the table of sub-register indexes.
2882 +- OS << "extern const uint16_t " << TargetName << "SubRegIdxLists[] = {\n";
2883 ++#ifdef CAPSTONE
2884 ++ OS << "static"
2885 ++#else
2886 ++ OS << "extern"
2887 ++#endif
2888 ++ << " const uint16_t " << TargetName << "SubRegIdxLists[] = {\n";
2889 + SubRegIdxSeqs.emit(OS, printSubRegIndex);
2890 + OS << "};\n\n";
2891 +
2892 ++#ifndef CAPSTONE
2893 + // Emit the table of sub-register index sizes.
2894 + OS << "extern const MCRegisterInfo::SubRegCoveredBits "
2895 + << TargetName << "SubRegIdxRanges[] = {\n";
2896 +@@ -989,14 +1039,22 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
2897 + << Idx.getName() << "\n";
2898 + }
2899 + OS << "};\n\n";
2900 ++#endif
2901 +
2902 + // Emit the string table.
2903 + RegStrings.layout();
2904 ++#ifndef CAPSTONE
2905 + OS << "extern const char " << TargetName << "RegStrings[] = {\n";
2906 + RegStrings.emit(OS, printChar);
2907 + OS << "};\n\n";
2908 ++#endif
2909 +
2910 +- OS << "extern const MCRegisterDesc " << TargetName
2911 ++#ifdef CAPSTONE
2912 ++ OS << "static"
2913 ++#else
2914 ++ OS << "extern"
2915 ++#endif
2916 ++ << " const MCRegisterDesc " << TargetName
2917 + << "RegDesc[] = { // Descriptors\n";
2918 + OS << " { " << RegStrings.get("") << ", 0, 0, 0, 0, 0 },\n";
2919 +
2920 +@@ -1012,6 +1070,7 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
2921 + }
2922 + OS << "};\n\n"; // End of register descriptors...
2923 +
2924 ++#ifndef CAPSTONE
2925 + // Emit the table of register unit roots. Each regunit has one or two root
2926 + // registers.
2927 + OS << "extern const MCPhysReg " << TargetName << "RegUnitRoots[][2] = {\n";
2928 +@@ -1025,11 +1084,14 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
2929 + OS << " },\n";
2930 + }
2931 + OS << "};\n\n";
2932 ++#endif
2933 +
2934 + const auto &RegisterClasses = RegBank.getRegClasses();
2935 +
2936 + // Loop over all of the register classes... emitting each one.
2937 ++#ifndef CAPSTONE
2938 + OS << "namespace { // Register classes...\n";
2939 ++#endif
2940 +
2941 + SequenceToOffsetTable<std::string> RegClassStrings;
2942 +
2943 +@@ -1044,15 +1106,28 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
2944 +
2945 + // Emit the register list now.
2946 + OS << " // " << Name << " Register Class...\n"
2947 +- << " const MCPhysReg " << Name
2948 ++ << " "
2949 ++#ifdef CAPSTONE
2950 ++ << "static "
2951 ++#endif
2952 ++ << "const MCPhysReg " << Name
2953 + << "[] = {\n ";
2954 + for (Record *Reg : Order) {
2955 +- OS << getQualifiedName(Reg) << ", ";
2956 ++#ifdef CAPSTONE
2957 ++ OS << NAME_PREFIX Reg->getName()
2958 ++#else
2959 ++ OS << getQualifiedName(Reg)
2960 ++#endif
2961 ++ << ", ";
2962 + }
2963 + OS << "\n };\n\n";
2964 +
2965 + OS << " // " << Name << " Bit set.\n"
2966 +- << " const uint8_t " << Name
2967 ++ << " "
2968 ++#ifdef CAPSTONE
2969 ++ << "static "
2970 ++#endif
2971 ++ << "const uint8_t " << Name
2972 + << "Bits[] = {\n ";
2973 + BitVectorEmitter BVE;
2974 + for (Record *Reg : Order) {
2975 +@@ -1062,14 +1137,23 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
2976 + OS << "\n };\n\n";
2977 +
2978 + }
2979 ++#ifndef CAPSTONE
2980 + OS << "} // end anonymous namespace\n\n";
2981 ++#endif
2982 +
2983 + RegClassStrings.layout();
2984 ++#ifndef CAPSTONE
2985 + OS << "extern const char " << TargetName << "RegClassStrings[] = {\n";
2986 + RegClassStrings.emit(OS, printChar);
2987 + OS << "};\n\n";
2988 ++#endif
2989 +
2990 +- OS << "extern const MCRegisterClass " << TargetName
2991 ++#ifdef CAPSTONE
2992 ++ OS << "static"
2993 ++#else
2994 ++ OS << "extern"
2995 ++#endif
2996 ++ << " const MCRegisterClass " << TargetName
2997 + << "MCRegisterClasses[] = {\n";
2998 +
2999 + for (const auto &RC : RegisterClasses) {
3000 +@@ -1077,13 +1161,20 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
3001 + OS << " { " << RC.getName() << ", " << RC.getName() << "Bits, "
3002 + << RegClassStrings.get(RC.getName()) << ", "
3003 + << RC.getOrder().size() << ", sizeof(" << RC.getName() << "Bits), "
3004 +- << RC.getQualifiedName() + "RegClassID" << ", "
3005 ++#ifdef CAPSTONE
3006 ++ << NAME_PREFIX RC.getName()
3007 ++#else
3008 ++ << RC.getQualifiedName()
3009 ++#endif
3010 ++ << "RegClassID" << ", "
3011 ++ //<< RegSize/8 << ", "
3012 + << RC.CopyCost << ", "
3013 + << ( RC.Allocatable ? "true" : "false" ) << " },\n";
3014 + }
3015 +
3016 + OS << "};\n\n";
3017 +
3018 ++#ifndef CAPSTONE
3019 + EmitRegMappingTables(OS, Regs, false);
3020 +
3021 + // Emit Reg encoding table
3022 +@@ -1102,7 +1193,9 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
3023 + OS << " " << Value << ",\n";
3024 + }
3025 + OS << "};\n"; // End of HW encoding table
3026 ++#endif
3027 +
3028 ++#ifndef CAPSTONE
3029 + // MCRegisterInfo initialization routine.
3030 + OS << "static inline void Init" << TargetName
3031 + << "MCRegisterInfo(MCRegisterInfo *RI, unsigned RA, "
3032 +@@ -1123,7 +1216,12 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
3033 + OS << "}\n\n";
3034 +
3035 + OS << "} // end namespace llvm\n\n";
3036 +- OS << "#endif // GET_REGINFO_MC_DESC\n\n";
3037 ++#endif
3038 ++ OS << "#endif // GET_REGINFO_MC_DESC\n"
3039 ++#ifndef CAPSTONE
3040 ++ << "\n"
3041 ++#endif
3042 ++ ;
3043 + }
3044 +
3045 + void
3046 +@@ -1605,8 +1703,10 @@ void RegisterInfoEmitter::run(raw_ostream &OS) {
3047 + CodeGenRegBank &RegBank = Target.getRegBank();
3048 + runEnums(OS, Target, RegBank);
3049 + runMCDesc(OS, Target, RegBank);
3050 ++#ifndef CAPSTONE
3051 + runTargetHeader(OS, Target, RegBank);
3052 + runTargetDesc(OS, Target, RegBank);
3053 ++#endif
3054 +
3055 + if (RegisterInfoDebug)
3056 + debugDump(errs());
3057 +diff --git a/llvm/utils/TableGen/SubtargetEmitter.cpp b/llvm/utils/TableGen/SubtargetEmitter.cpp
3058 +index 792c957ea..3ddfd1371 100644
3059 +--- a/llvm/utils/TableGen/SubtargetEmitter.cpp
3060 ++++ b/llvm/utils/TableGen/SubtargetEmitter.cpp
3061 +@@ -149,7 +149,9 @@ void SubtargetEmitter::Enumeration(raw_ostream &OS) {
3062 + if (N > MAX_SUBTARGET_FEATURES)
3063 + PrintFatalError("Too many subtarget features! Bump MAX_SUBTARGET_FEATURES.");
3064 +
3065 ++#ifndef CAPSTONE
3066 + OS << "namespace " << Target << " {\n";
3067 ++#endif
3068 +
3069 + // Open enumeration.
3070 + OS << "enum {\n";
3071 +@@ -160,12 +162,22 @@ void SubtargetEmitter::Enumeration(raw_ostream &OS) {
3072 + Record *Def = DefList[i];
3073 +
3074 + // Get and emit name
3075 +- OS << " " << Def->getName() << " = " << i << ",\n";
3076 ++ OS << " "
3077 ++#ifdef CAPSTONE
3078 ++ << Target << "_"
3079 ++#endif
3080 ++ << Def->getName() << " = "
3081 ++#ifdef CAPSTONE
3082 ++ << "1ULL << "
3083 ++#endif
3084 ++ << i << ",\n";
3085 + }
3086 +
3087 + // Close enumeration and namespace
3088 + OS << "};\n";
3089 ++#ifndef CAPSTONE
3090 + OS << "} // end namespace " << Target << "\n";
3091 ++#endif
3092 + }
3093 +
3094 + //
3095 +@@ -1786,14 +1798,27 @@ void SubtargetEmitter::EmitMCInstrAnalysisPredicateFunctions(raw_ostream &OS) {
3096 + void SubtargetEmitter::run(raw_ostream &OS) {
3097 + emitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
3098 +
3099 ++#ifdef CAPSTONE
3100 ++ OS << "/* Capstone Disassembly Engine, http://www.capstone-engine.org */\n"
3101 ++ "/* By Nguyen Anh Quynh <[email protected]>, 2013-2015 */\n"
3102 ++ "\n";
3103 ++#endif
3104 ++
3105 + OS << "\n#ifdef GET_SUBTARGETINFO_ENUM\n";
3106 + OS << "#undef GET_SUBTARGETINFO_ENUM\n\n";
3107 +
3108 ++#ifndef CAPSTONE
3109 + OS << "namespace llvm {\n";
3110 ++#endif
3111 + Enumeration(OS);
3112 ++#ifdef CAPSTONE
3113 ++ OS << "\n";
3114 ++#else
3115 + OS << "} // end namespace llvm\n\n";
3116 ++#endif
3117 + OS << "#endif // GET_SUBTARGETINFO_ENUM\n\n";
3118 +
3119 ++#ifndef CAPSTONE
3120 + OS << "\n#ifdef GET_SUBTARGETINFO_MC_DESC\n";
3121 + OS << "#undef GET_SUBTARGETINFO_MC_DESC\n\n";
3122 +
3123 +@@ -1942,6 +1967,7 @@ void SubtargetEmitter::run(raw_ostream &OS) {
3124 + OS << "#endif // GET_SUBTARGETINFO_CTOR\n\n";
3125 +
3126 + EmitMCInstrAnalysisPredicateFunctions(OS);
3127 ++#endif
3128 + }
3129 +
3130 + namespace llvm {
3131 +diff --git a/llvm/utils/TableGen/TableGen.cpp b/llvm/utils/TableGen/TableGen.cpp
3132 +index 38f81dc39..abe172be2 100644
3133 +--- a/llvm/utils/TableGen/TableGen.cpp
3134 ++++ b/llvm/utils/TableGen/TableGen.cpp
3135 +@@ -27,6 +27,8 @@ enum ActionType {
3136 + GenEmitter,
3137 + GenRegisterInfo,
3138 + GenInstrInfo,
3139 ++ GenMappingInsn,
3140 ++ GenInsnNameMaps,
3141 + GenInstrDocs,
3142 + GenAsmWriter,
3143 + GenAsmMatcher,
3144 +@@ -68,6 +70,10 @@ namespace {
3145 + "Generate registers and register classes info"),
3146 + clEnumValN(GenInstrInfo, "gen-instr-info",
3147 + "Generate instruction descriptions"),
3148 ++ clEnumValN(GenMappingInsn, "gen-mapping-insn",
3149 ++ ""),
3150 ++ clEnumValN(GenInsnNameMaps, "gen-insn-name-maps",
3151 ++ ""),
3152 + clEnumValN(GenInstrDocs, "gen-instr-docs",
3153 + "Generate instruction documentation"),
3154 + clEnumValN(GenCallingConv, "gen-callingconv",
3155 +@@ -143,6 +149,12 @@ bool LLVMTableGenMain(raw_ostream &OS, RecordKeeper &Records) {
3156 + case GenInstrInfo:
3157 + EmitInstrInfo(Records, OS);
3158 + break;
3159 ++ case GenMappingInsn:
3160 ++ EmitMappingInsn(Records, OS);
3161 ++ break;
3162 ++ case GenInsnNameMaps:
3163 ++ EmitInsnNameMaps(Records, OS);
3164 ++ break;
3165 + case GenInstrDocs:
3166 + EmitInstrDocs(Records, OS);
3167 + break;
3168 +diff --git a/llvm/utils/TableGen/TableGenBackends.h b/llvm/utils/TableGen/TableGenBackends.h
3169 +index 135ec65c0..82f787dba 100644
3170 +--- a/llvm/utils/TableGen/TableGenBackends.h
3171 ++++ b/llvm/utils/TableGen/TableGenBackends.h
3172 +@@ -74,6 +74,8 @@ void EmitDFAPacketizer(RecordKeeper &RK, raw_ostream &OS);
3173 + void EmitDisassembler(RecordKeeper &RK, raw_ostream &OS);
3174 + void EmitFastISel(RecordKeeper &RK, raw_ostream &OS);
3175 + void EmitInstrInfo(RecordKeeper &RK, raw_ostream &OS);
3176 ++void EmitMappingInsn(RecordKeeper &RK, raw_ostream &OS);
3177 ++void EmitInsnNameMaps(RecordKeeper &RK, raw_ostream &OS);
3178 + void EmitInstrDocs(RecordKeeper &RK, raw_ostream &OS);
3179 + void EmitPseudoLowering(RecordKeeper &RK, raw_ostream &OS);
3180 + void EmitCompressInst(RecordKeeper &RK, raw_ostream &OS);
3181 +--
3182 +2.20.1
3183 +
3184 diff --git a/llvm/0002-update-TableGen-for-generate-RISCV-port-inc-for-CAPS.patch b/llvm/0002-update-TableGen-for-generate-RISCV-port-inc-for-CAPS.patch
3185 new file mode 100644
3186 index 000000000..a395c32a7
3187 --- /dev/null
3188 +++ b/llvm/0002-update-TableGen-for-generate-RISCV-port-inc-for-CAPS.patch
3189 @@ -0,0 +1,905 @@
3190 +From 40ac7444e7f3679fad852564acca4f30f47fb52d Mon Sep 17 00:00:00 2001
3191 +From: fanfuqiang <[email protected]>
3192 +Date: Thu, 28 Feb 2019 01:37:55 +0800
3193 +Subject: [PATCH] update TableGen for generate RISCV port inc for CAPSTONE
3194 +
3195 +---
3196 + llvm/lib/Target/RISCV/CMakeLists.txt | 2 +-
3197 + llvm/utils/TableGen/AsmWriterEmitter.cpp | 175 ++++++++++++++++--
3198 + .../utils/TableGen/FixedLenDecoderEmitter.cpp | 122 ++++++------
3199 + llvm/utils/TableGen/InstrInfoEmitter.cpp | 14 +-
3200 + llvm/utils/TableGen/RegisterInfoEmitter.cpp | 20 +-
3201 + llvm/utils/TableGen/TableGen.cpp | 6 +
3202 + 6 files changed, 249 insertions(+), 90 deletions(-)
3203 +
3204 +diff --git a/llvm/lib/Target/RISCV/CMakeLists.txt b/llvm/lib/Target/RISCV/CMakeLists.txt
3205 +index 1821f4b01..603aa3f54 100644
3206 +--- a/llvm/lib/Target/RISCV/CMakeLists.txt
3207 ++++ b/llvm/lib/Target/RISCV/CMakeLists.txt
3208 +@@ -6,7 +6,7 @@ tablegen(LLVM RISCVGenCompressInstEmitter.inc -gen-compress-inst-emitter)
3209 + tablegen(LLVM RISCVGenDAGISel.inc -gen-dag-isel)
3210 + tablegen(LLVM RISCVGenDisassemblerTables.inc -gen-disassembler)
3211 + tablegen(LLVM RISCVGenInstrInfo.inc -gen-instr-info)
3212 +-tablegen(LLVM RISCVGenMappingInsn.inc -gen-mapping-insn)
3213 ++tablegen(LLVM RISCVMappingInsn.inc -gen-mapping-insn)
3214 + tablegen(LLVM RISCVGenInsnNameMaps.inc -gen-insn-name-maps)
3215 + tablegen(LLVM RISCVGenMCCodeEmitter.inc -gen-emitter)
3216 + tablegen(LLVM RISCVGenMCPseudoLowering.inc -gen-pseudo-lowering)
3217 +diff --git a/llvm/utils/TableGen/AsmWriterEmitter.cpp b/llvm/utils/TableGen/AsmWriterEmitter.cpp
3218 +index c24dc6052..ac82573fe 100644
3219 +--- a/llvm/utils/TableGen/AsmWriterEmitter.cpp
3220 ++++ b/llvm/utils/TableGen/AsmWriterEmitter.cpp
3221 +@@ -270,12 +270,13 @@ static void UnescapeString(std::string &Str) {
3222 + /// implementation. Destroys all instances of AsmWriterInst information, by
3223 + /// clearing the Instructions vector.
3224 + void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
3225 ++#ifdef CAPSTONE
3226 ++ bool PassSubtarget = false;
3227 ++#else
3228 + Record *AsmWriter = Target.getAsmWriter();
3229 +-#ifndef CAPSTONE
3230 + StringRef ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
3231 +-#endif
3232 + bool PassSubtarget = AsmWriter->getValueAsInt("PassSubtarget");
3233 +-
3234 ++#endif
3235 + O <<
3236 + "/// printInstruction - This method is automatically generated by tablegen\n"
3237 + "/// from the instruction set description.\n"
3238 +@@ -434,7 +435,11 @@ void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
3239 + }
3240 +
3241 + // Emit the initial tab character.
3242 +-#ifndef CAPSTONE
3243 ++#ifdef CAPSTONE
3244 ++ O << "#ifndef CAPSTONE_DIET\n"
3245 ++ << " SStream_concat0(O, \"\\t\");\n"
3246 ++ << "#endif\n\n";
3247 ++#else
3248 + O << " O << \"\\t\";\n\n";
3249 + #endif
3250 +
3251 +@@ -493,10 +498,10 @@ void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
3252 + << ((1 << NumBits)-1) << ") {\n"
3253 + << " default: "
3254 + #ifdef CAPSTONE
3255 +- << "assert(0);\n"
3256 +-#endif
3257 ++ << "assert(0 && \"Invalid command number.\");\n";
3258 ++#else
3259 + << "llvm_unreachable(\"Invalid command number.\");\n";
3260 +-
3261 ++#endif
3262 + // Print out all the cases.
3263 + for (unsigned j = 0, e = Commands.size(); j != e; ++j) {
3264 + O << " case " << j << ":\n";
3265 +@@ -576,9 +581,7 @@ emitRegisterNameString(raw_ostream &O, StringRef AltName,
3266 + }
3267 +
3268 + StringTable.layout();
3269 +-#ifdef CAPSTONE
3270 +- O << "#ifndef CAPSTONE_DIET\n";
3271 +-#endif
3272 ++
3273 + O << " static const char AsmStrs" << AltName << "[] = {\n";
3274 + StringTable.emit(O, printChar);
3275 + O << " };\n\n";
3276 +@@ -625,7 +628,8 @@ void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
3277 + O << " "
3278 + << "assert(RegNo && RegNo < " << (Registers.size()+1)
3279 + << " && \"Invalid register number!\");\n"
3280 +- << "\n";
3281 ++ << "\n"
3282 ++ << "#ifndef CAPSTONE_DIET\n";
3283 +
3284 + if (hasAltNames) {
3285 + for (const Record *R : AltNameIndices)
3286 +@@ -636,7 +640,7 @@ void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
3287 + if (hasAltNames) {
3288 + O << " switch(AltIdx) {\n"
3289 + #ifdef CAPSTONE
3290 +- << " default: assert(0);\n";
3291 ++ << " default: assert(0 && \"Invalid register alt name index!\");\n";
3292 + #else
3293 + << " default: llvm_unreachable(\"Invalid register alt name index!\");\n";
3294 + #endif
3295 +@@ -886,7 +890,9 @@ void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
3296 +
3297 + IAPrinter IAP(CGA.Result->getAsString(), FlatAliasAsmString);
3298 +
3299 ++#ifndef CAPSTONE // Silence the compiler waring.
3300 + StringRef Namespace = Target.getName();
3301 ++#endif
3302 + std::vector<Record *> ReqFeatures;
3303 + if (PassSubtarget) {
3304 + // We only consider ReqFeatures predicates if PassSubtarget
3305 +@@ -902,7 +908,11 @@ void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
3306 + NumMIOps += ResultInstOpnd.MINumOperands;
3307 +
3308 + std::string Cond;
3309 ++#ifdef CAPSTONE
3310 ++ Cond = std::string("MCInst_getNumOperands(MI) == ") + utostr(NumMIOps);
3311 ++#else
3312 + Cond = std::string("MI->getNumOperands() == ") + utostr(NumMIOps);
3313 ++#endif
3314 + IAP.addCond(Cond);
3315 +
3316 + bool CantHandle = false;
3317 +@@ -926,9 +936,11 @@ void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
3318 + }
3319 + break;
3320 + }
3321 +-
3322 ++#ifdef CAPSTONE
3323 ++ std::string Op = "MCInst_getOperand(MI, " + utostr(MIOpNum) + ")";
3324 ++#else
3325 + std::string Op = "MI->getOperand(" + utostr(MIOpNum) + ")";
3326 +-
3327 ++#endif
3328 + const CodeGenInstAlias::ResultOperand &RO = CGA.ResultOperands[i];
3329 +
3330 + switch (RO.Kind) {
3331 +@@ -954,19 +966,39 @@ void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
3332 + if (Rec->isSubClassOf("RegisterOperand"))
3333 + Rec = Rec->getValueAsDef("RegClass");
3334 + if (Rec->isSubClassOf("RegisterClass")) {
3335 ++#ifdef CAPSTONE
3336 ++ IAP.addCond("MCOperand_isReg(" + Op + ")");
3337 ++#else
3338 + IAP.addCond(Op + ".isReg()");
3339 ++#endif
3340 +
3341 + if (!IAP.isOpMapped(ROName)) {
3342 + IAP.addOperand(ROName, MIOpNum, PrintMethodIdx);
3343 + Record *R = CGA.ResultOperands[i].getRecord();
3344 + if (R->isSubClassOf("RegisterOperand"))
3345 + R = R->getValueAsDef("RegClass");
3346 ++
3347 ++#ifdef CAPSTONE
3348 ++ Cond = std::string("MCRegisterClass_contains(") +
3349 ++ "MCRegisterInfo_getRegClass(" + "MRI, " +
3350 ++ Target.getName().str() + "_" + R->getName().str() + "RegClassID)" +
3351 ++ ", " +
3352 ++ "MCOperand_getReg(" + Op + "))";
3353 ++#else
3354 + Cond = std::string("MRI.getRegClass(") + Target.getName().str() +
3355 + "::" + R->getName().str() + "RegClassID).contains(" + Op +
3356 + ".getReg())";
3357 ++#endif
3358 ++
3359 + } else {
3360 ++#ifdef CAPSTONE
3361 ++ Cond = std::string("MCOperand_getReg(") + Op + ") == " +
3362 ++ "MCOperand_getReg(MCInst_getOperand(MI, " +
3363 ++ utostr(IAP.getOpIndex(ROName)) + "))";
3364 ++#else
3365 + Cond = Op + ".getReg() == MI->getOperand(" +
3366 + utostr(IAP.getOpIndex(ROName)) + ").getReg()";
3367 ++#endif
3368 + }
3369 + } else {
3370 + // Assume all printable operands are desired for now. This can be
3371 +@@ -984,8 +1016,12 @@ void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
3372 + break; // No conditions on this operand at all
3373 + }
3374 + Cond = (Target.getName() + ClassName + "ValidateMCOperand(" + Op +
3375 ++ #ifdef CAPSTONE
3376 ++ ", " + utostr(Entry) + ")").str();
3377 ++ #else
3378 + ", STI, " + utostr(Entry) + ")")
3379 + .str();
3380 ++ #endif
3381 + }
3382 + // for all subcases of ResultOperand::K_Record:
3383 + IAP.addCond(Cond);
3384 +@@ -994,9 +1030,15 @@ void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
3385 + case CodeGenInstAlias::ResultOperand::K_Imm: {
3386 + // Just because the alias has an immediate result, doesn't mean the
3387 + // MCInst will. An MCExpr could be present, for example.
3388 ++#ifdef CAPSTONE
3389 ++ IAP.addCond("MCOperand_isImm(" + Op + ")");
3390 ++ Cond = "MCOperand_getImm(" + Op + ") == " +
3391 ++ itostr(CGA.ResultOperands[i].getImm());
3392 ++#else
3393 + IAP.addCond(Op + ".isImm()");
3394 +-
3395 + Cond = Op + ".getImm() == " + itostr(CGA.ResultOperands[i].getImm());
3396 ++#endif
3397 ++
3398 + IAP.addCond(Cond);
3399 + break;
3400 + }
3401 +@@ -1008,8 +1050,14 @@ void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
3402 + break;
3403 + }
3404 +
3405 ++#ifdef CAPSTONE
3406 ++ Cond = "MCOperand_getReg(" + Op + ") == " + Target.getName().str() +
3407 ++ "_" + CGA.ResultOperands[i].getRegister()->getName().str();
3408 ++#else
3409 + Cond = Op + ".getReg() == " + Target.getName().str() + "::" +
3410 + CGA.ResultOperands[i].getRegister()->getName().str();
3411 ++#endif
3412 ++
3413 + IAP.addCond(Cond);
3414 + break;
3415 + }
3416 +@@ -1019,6 +1067,7 @@ void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
3417 +
3418 + if (CantHandle) continue;
3419 +
3420 ++#ifndef CAPSTONE
3421 + for (auto I = ReqFeatures.cbegin(); I != ReqFeatures.cend(); I++) {
3422 + Record *R = *I;
3423 + StringRef AsmCondString = R->getValueAsString("AssemblerCondString");
3424 +@@ -1040,6 +1089,7 @@ void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
3425 + IAP.addCond(Cond);
3426 + }
3427 + }
3428 ++#endif
3429 +
3430 + IAPrinterMap[Aliases.first].push_back(std::move(IAP));
3431 + }
3432 +@@ -1052,10 +1102,17 @@ void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
3433 + std::string Header;
3434 + raw_string_ostream HeaderO(Header);
3435 +
3436 ++#ifdef CAPSTONE
3437 ++ HeaderO << "\nstatic bool printAliasInstr(MCInst *MI, SStream * OS, void *info)"
3438 ++ << "\n"
3439 ++ << "{\n"
3440 ++ << " MCRegisterInfo *MRI = (MCRegisterInfo *) info;\n";
3441 ++#else
3442 + HeaderO << "bool " << Target.getName() << ClassName
3443 + << "::printAliasInstr(const MCInst"
3444 + << " *MI, " << (PassSubtarget ? "const MCSubtargetInfo &STI, " : "")
3445 + << "raw_ostream &OS) {\n";
3446 ++#endif
3447 +
3448 + std::string Cases;
3449 + raw_string_ostream CasesO(Cases);
3450 +@@ -1079,7 +1136,16 @@ void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
3451 +
3452 + if (UniqueIAPs.empty()) continue;
3453 +
3454 ++#ifdef CAPSTONE
3455 ++ // TODO: tricky.
3456 ++ const char* tmpCase = Entry.first.c_str();
3457 ++ assert (Entry.first.size() > 7);
3458 ++ CasesO.indent(2) << "case "
3459 ++ << "RISCV_" << std::string(tmpCase + 7) // strlen("RISCV::) == 7
3460 ++ << ":\n";
3461 ++#else
3462 + CasesO.indent(2) << "case " << Entry.first << ":\n";
3463 ++#endif
3464 +
3465 + for (IAPrinter *IAP : UniqueIAPs) {
3466 + CasesO.indent(4);
3467 +@@ -1100,13 +1166,21 @@ void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
3468 +
3469 + if (!MCOpPredicates.empty())
3470 + O << "static bool " << Target.getName() << ClassName
3471 ++#ifdef CAPSTONE
3472 ++ << "ValidateMCOperand(MCOperand *MCOp,\n"
3473 ++#else
3474 + << "ValidateMCOperand(const MCOperand &MCOp,\n"
3475 + << " const MCSubtargetInfo &STI,\n"
3476 ++#endif
3477 + << " unsigned PredicateIndex);\n";
3478 +
3479 + O << HeaderO.str();
3480 + O.indent(2) << "const char *AsmString;\n";
3481 ++#ifdef CAPSTONE
3482 ++ O.indent(2) << "switch (MCInst_getOpcode(MI)) {\n";
3483 ++#else
3484 + O.indent(2) << "switch (MI->getOpcode()) {\n";
3485 ++#endif
3486 + O.indent(2) << "default: return false;\n";
3487 + O << CasesO.str();
3488 + O.indent(2) << "}\n\n";
3489 +@@ -1114,14 +1188,27 @@ void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
3490 + // Code that prints the alias, replacing the operands with the ones from the
3491 + // MCInst.
3492 + O << " unsigned I = 0;\n";
3493 ++#ifdef CAPSTONE
3494 ++ O << " char *tmpString = cs_strdup(AsmString);\n";
3495 ++#endif
3496 + O << " while (AsmString[I] != ' ' && AsmString[I] != '\\t' &&\n";
3497 + O << " AsmString[I] != '$' && AsmString[I] != '\\0')\n";
3498 + O << " ++I;\n";
3499 ++#ifdef CAPSTONE
3500 ++ O << " tmpString[I] = 0;\n";
3501 ++ O << " SStream_concat0(OS, \"\\t\");\n";
3502 ++ O << " SStream_concat0(OS, tmpString);\n";
3503 ++ O << " SStream_concat0(OS, \"\\n\");\n";
3504 ++#else
3505 + O << " OS << '\\t' << StringRef(AsmString, I);\n";
3506 +-
3507 ++#endif
3508 + O << " if (AsmString[I] != '\\0') {\n";
3509 + O << " if (AsmString[I] == ' ' || AsmString[I] == '\\t') {\n";
3510 ++#ifdef CAPSTONE
3511 ++ O << " SStream_concat0(OS, \"\\t\");\n";
3512 ++#else
3513 + O << " OS << '\\t';\n";
3514 ++#endif
3515 + O << " ++I;\n";
3516 + O << " }\n";
3517 + O << " do {\n";
3518 +@@ -1131,15 +1218,28 @@ void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
3519 + O << " ++I;\n";
3520 + O << " int OpIdx = AsmString[I++] - 1;\n";
3521 + O << " int PrintMethodIdx = AsmString[I++] - 1;\n";
3522 ++#ifdef CAPSTONE
3523 ++ O << " printCustomAliasOperand(MI, OpIdx, PrintMethodIdx, OS);\n";
3524 ++#else
3525 + O << " printCustomAliasOperand(MI, OpIdx, PrintMethodIdx, ";
3526 + O << (PassSubtarget ? "STI, " : "");
3527 + O << "OS);\n";
3528 ++#endif
3529 + O << " } else\n";
3530 ++
3531 ++#ifdef CAPSTONE
3532 ++ O << " printOperand(MI, (unsigned)(AsmString[I++]) - 1, OS);\n";
3533 ++#else
3534 + O << " printOperand(MI, unsigned(AsmString[I++]) - 1, ";
3535 + O << (PassSubtarget ? "STI, " : "");
3536 + O << "OS);\n";
3537 ++#endif
3538 + O << " } else {\n";
3539 ++#ifdef CAPSTONE
3540 ++ O << " SStream_concat0(OS, &AsmString[I++]);\n";
3541 ++#else
3542 + O << " OS << AsmString[I++];\n";
3543 ++#endif
3544 + O << " }\n";
3545 + O << " } while (AsmString[I] != '\\0');\n";
3546 + O << " }\n\n";
3547 +@@ -1150,25 +1250,48 @@ void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
3548 + //////////////////////////////
3549 + // Write out the printCustomAliasOperand function
3550 + //////////////////////////////
3551 +-
3552 ++#ifdef CAPSTONE
3553 ++ O << "static void "
3554 ++#else
3555 + O << "void " << Target.getName() << ClassName << "::"
3556 ++#endif
3557 + << "printCustomAliasOperand(\n"
3558 ++#ifdef CAPSTONE
3559 ++ << " MCInst *MI, unsigned OpIdx,\n"
3560 ++#else
3561 + << " const MCInst *MI, unsigned OpIdx,\n"
3562 ++#endif
3563 + << " unsigned PrintMethodIdx,\n"
3564 ++#ifdef CAPSTONE
3565 ++ << " SStream *OS) {\n";
3566 ++#else
3567 + << (PassSubtarget ? " const MCSubtargetInfo &STI,\n" : "")
3568 + << " raw_ostream &OS) {\n";
3569 ++#endif
3570 + if (PrintMethods.empty())
3571 ++#ifdef CAPSTONE
3572 ++ O << " assert(0 && \"Unknown PrintMethod kind\");\n";
3573 ++#else
3574 + O << " llvm_unreachable(\"Unknown PrintMethod kind\");\n";
3575 ++#endif
3576 + else {
3577 + O << " switch (PrintMethodIdx) {\n"
3578 + << " default:\n"
3579 ++#ifdef CAPSTONE
3580 ++ << " assert(0 && \"Unknown PrintMethod kind\");\n"
3581 ++#else
3582 + << " llvm_unreachable(\"Unknown PrintMethod kind\");\n"
3583 ++#endif
3584 + << " break;\n";
3585 +
3586 + for (unsigned i = 0; i < PrintMethods.size(); ++i) {
3587 + O << " case " << i << ":\n"
3588 + << " " << PrintMethods[i] << "(MI, OpIdx, "
3589 ++#ifdef CAPSTONE
3590 ++ << "OS);\n"
3591 ++#else
3592 + << (PassSubtarget ? "STI, " : "") << "OS);\n"
3593 ++#endif
3594 + << " break;\n";
3595 + }
3596 + O << " }\n";
3597 +@@ -1177,9 +1300,20 @@ void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
3598 +
3599 + if (!MCOpPredicates.empty()) {
3600 + O << "static bool " << Target.getName() << ClassName
3601 ++#ifdef CAPSTONE
3602 ++ << "ValidateMCOperand(MCOperand *MCOp,\n"
3603 ++#else
3604 + << "ValidateMCOperand(const MCOperand &MCOp,\n"
3605 + << " const MCSubtargetInfo &STI,\n"
3606 +- << " unsigned PredicateIndex) {\n"
3607 ++#endif
3608 ++ << " unsigned PredicateIndex) {\n"
3609 ++#ifdef CAPSTONE
3610 ++ << " // TODO: need some constant untils operate the MCOperand,\n"
3611 ++ << " // but current CAPSTONE does't have.\n"
3612 ++ << " // So, We just return true\n"
3613 ++ << " return true;\n\n"
3614 ++ << "#if 0\n"
3615 ++#endif
3616 + << " switch (PredicateIndex) {\n"
3617 + << " default:\n"
3618 + << " llvm_unreachable(\"Unknown MCOperandPredicate kind\");\n"
3619 +@@ -1195,6 +1329,9 @@ void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
3620 + llvm_unreachable("Unexpected MCOperandPredicate field!");
3621 + }
3622 + O << " }\n"
3623 ++#ifdef CAPSTONE
3624 ++ << "#endif\n"
3625 ++#endif
3626 + << "}\n\n";
3627 + }
3628 +
3629 +@@ -1228,7 +1365,7 @@ void AsmWriterEmitter::run(raw_ostream &O) {
3630 + #endif
3631 + EmitPrintInstruction(O);
3632 + EmitGetRegisterName(O);
3633 +-#ifndef CAPSTONE
3634 ++#ifdef CAPSTONE
3635 + EmitPrintAliasInstruction(O);
3636 + #endif
3637 + }
3638 +diff --git a/llvm/utils/TableGen/FixedLenDecoderEmitter.cpp b/llvm/utils/TableGen/FixedLenDecoderEmitter.cpp
3639 +index 3db428dfa..e1bfaa934 100644
3640 +--- a/llvm/utils/TableGen/FixedLenDecoderEmitter.cpp
3641 ++++ b/llvm/utils/TableGen/FixedLenDecoderEmitter.cpp
3642 +@@ -946,13 +946,6 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
3643 + void FixedLenDecoderEmitter::
3644 + emitPredicateFunction(formatted_raw_ostream &OS, PredicateSet &Predicates,
3645 + unsigned Indentation) const {
3646 +-#ifdef CAPSTONE
3647 +- OS.indent(Indentation) << "static bool getbool(uint64_t b)\n";
3648 +- OS.indent(Indentation) << "{\n";
3649 +- OS.indent(Indentation) << "\treturn b != 0;\n";
3650 +- OS.indent(Indentation) << "}\n\n";
3651 +-#endif
3652 +-
3653 + // The predicate function is just a big switch statement based on the
3654 + // input predicate index.
3655 + OS.indent(Indentation) << "static bool checkDecoderPredicate(unsigned Idx, "
3656 +@@ -966,26 +959,25 @@ emitPredicateFunction(formatted_raw_ostream &OS, PredicateSet &Predicates,
3657 + OS.indent(Indentation) << "switch (Idx) {\n";
3658 + OS.indent(Indentation) << "default: "
3659 + #ifdef CAPSTONE
3660 +- << "assert(0);\n"
3661 +-#endif
3662 ++ << "assert(0 && \"Invalid index!\");\n";
3663 ++#else
3664 + << "llvm_unreachable(\"Invalid index!\");\n";
3665 ++#endif
3666 + unsigned Index = 0;
3667 + for (const auto &Predicate : Predicates) {
3668 + OS.indent(Indentation) << "case " << Index++ << ":\n";
3669 + OS.indent(Indentation+2) << "return "
3670 +-#ifdef CAPSTONE
3671 +- << "getbool"
3672 +-#endif
3673 +- << "(" << Predicate << ");\n";
3674 ++ << Predicate << ";\n";
3675 + }
3676 + OS.indent(Indentation) << "}\n";
3677 + } else {
3678 + // No case statement to emit
3679 + OS.indent(Indentation)
3680 + #ifdef CAPSTONE
3681 +- << "assert(0);\n"
3682 +-#endif
3683 ++ << "assert(0 && \"Invalid index!\");\n";
3684 ++#else
3685 + << "llvm_unreachable(\"Invalid index!\");\n";
3686 ++#endif
3687 + }
3688 + Indentation -= 2;
3689 + OS.indent(Indentation) << "}\n\n";
3690 +@@ -998,10 +990,11 @@ emitDecoderFunction(formatted_raw_ostream &OS, DecoderSet &Decoders,
3691 + // input decoder index.
3692 + #ifdef CAPSTONE
3693 + #define EDF_EOL " \\\n"
3694 +- OS.indent(Indentation) << "#define DecodeToMCInst(fname,fieldname, InsnType) \\\n";
3695 +- OS.indent(Indentation) << "static DecodeStatus fname(DecodeStatus S, unsigned Idx, InsnType insn, MCInst *MI, \\\n";
3696 +- OS.indent(Indentation) << " uint64_t Address, const void *Decoder) \\\n";
3697 +- OS.indent(Indentation) << "{ \\\n";
3698 ++ OS.indent(Indentation) << "#define DecodeToMCInst(fname, fieldname, InsnType) \\\n";
3699 ++ OS.indent(Indentation) << "static DecodeStatus fname(DecodeStatus S, unsigned Idx,"
3700 ++ << " InsnType insn, MCInst *MI, \\\n";
3701 ++ OS.indent(Indentation) << " uint64_t Address, const void *Decoder,\\\n";
3702 ++ OS.indent(Indentation) << " bool *DecodeComplete) {\\\n";
3703 + #else
3704 + #define EDF_EOL "\n"
3705 + OS.indent(Indentation) << "template<typename InsnType>\n";
3706 +@@ -1011,16 +1004,18 @@ emitDecoderFunction(formatted_raw_ostream &OS, DecoderSet &Decoders,
3707 + << "Address, const void *Decoder, bool &DecodeComplete) {\n";
3708 + #endif
3709 + Indentation += 2;
3710 +-#ifndef CAPSTONE
3711 ++#ifdef CAPSTONE
3712 ++ OS.indent(Indentation) << "*DecodeComplete = true;\\\n";
3713 ++#else
3714 + OS.indent(Indentation) << "DecodeComplete = true;\n";
3715 + #endif
3716 + OS.indent(Indentation) << "InsnType tmp;" EDF_EOL;
3717 + OS.indent(Indentation) << "switch (Idx) {" EDF_EOL;
3718 + OS.indent(Indentation) << "default:"
3719 +-#ifndef CAPSTONE
3720 +- << " llvm_unreachable(\"Invalid index!\");\n";
3721 ++#ifdef CAPSTONE
3722 ++ << " assert(0 && \"Invalid index!\");\\\n";
3723 + #else
3724 +- << " assert(0);\\\n";
3725 ++ << " llvm_unreachable(\"Invalid index!\");\n";
3726 + #endif
3727 + unsigned Index = 0;
3728 + for (const auto &Decoder : Decoders) {
3729 +@@ -1174,8 +1169,27 @@ void FilterChooser::emitBinaryParser(raw_ostream &o, unsigned &Indentation,
3730 +
3731 + if (Decoder != "") {
3732 + OpHasCompleteDecoder = OpInfo.HasCompleteDecoder;
3733 ++#ifdef CAPSTONE
3734 ++ std::string::size_type posOfLeftAngle = 0, posOfRightAngle = 0;
3735 ++ posOfLeftAngle = Decoder.find("<");
3736 ++ posOfRightAngle = Decoder.find(">");
3737 ++ std::string printDecoder = Decoder;
3738 ++ if (posOfLeftAngle != std::string::npos &&
3739 ++ posOfRightAngle != std::string::npos) {
3740 ++ printDecoder = Decoder.substr(0, posOfLeftAngle);
3741 ++ o.indent(Indentation) << Emitter->GuardPrefix
3742 ++ << printDecoder
3743 ++ << "(MI, tmp, Address, Decoder, "
3744 ++ << Decoder.substr(posOfLeftAngle+1, posOfRightAngle-posOfLeftAngle-1);
3745 ++ } else
3746 ++ o.indent(Indentation) << Emitter->GuardPrefix << Decoder
3747 ++ << "(MI, tmp, Address, Decoder";
3748 ++ // trick.
3749 ++ o << ")"
3750 ++#else
3751 + o.indent(Indentation) << Emitter->GuardPrefix << Decoder
3752 + << "(MI, tmp, Address, Decoder)"
3753 ++#endif
3754 + << Emitter->GuardPostfix
3755 + #ifdef CAPSTONE
3756 + << " return MCDisassembler_Fail; \\\n";
3757 +@@ -1246,7 +1260,7 @@ static void emitSinglePredicateMatch(raw_ostream &o, StringRef str,
3758 + const std::string &PredicateNamespace) {
3759 + if (str[0] == '!')
3760 + #ifdef CAPSTONE
3761 +- o << "~(Bits & " << PredicateNamespace << "_"
3762 ++ o << "!(Bits & " << PredicateNamespace << "_"
3763 + << str.slice(1,str.size()) << ")";
3764 + #else
3765 + o << "!Bits[" << PredicateNamespace << "::"
3766 +@@ -2331,15 +2345,13 @@ static void emitDecodeInstruction(formatted_raw_ostream &OS) {
3767 + #endif
3768 +
3769 + #ifdef CAPSTONE
3770 +- OS << "static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], "
3771 +-
3772 ++ OS << "#define DecodeInstruction(fname, fieldname, decoder, InsnType) \\\n"
3773 ++ << "static DecodeStatus fname(const uint8_t DecodeTable[], "
3774 + "MCInst *MI,\\\n"
3775 +- << " InsnType insn, uint64_t "
3776 ++ << " InsnType insn, uint64_t "
3777 + "Address,\\\n"
3778 +- << " const void *DisAsm,\\\n"
3779 +- << " int feature) {\\\n"
3780 +- << " uint64_t Bits = getFeatureBits(feature); \\\n"
3781 +- //<< " const FeatureBitset& Bits = STI.getFeatureBits();\n"
3782 ++ << " const void *DisAsm, int feature) {\\\n"
3783 ++ << " uint64_t Bits = getFeatureBits(feature);\\\n"
3784 + << "\\\n"
3785 + << " const uint8_t *Ptr = DecodeTable;\\\n"
3786 + << " uint32_t CurFieldValue = 0;\\\n"
3787 +@@ -2353,52 +2365,42 @@ static void emitDecodeInstruction(formatted_raw_ostream &OS) {
3788 + << " unsigned Start = *++Ptr;\\\n"
3789 + << " unsigned Len = *++Ptr;\\\n"
3790 + << " ++Ptr;\\\n"
3791 +- << " CurFieldValue = fieldFromInstruction(insn, Start, Len);\\\n"
3792 ++ << " CurFieldValue = fieldname(insn, Start, Len);\\\n"
3793 + << " break;\\\n"
3794 + << " }\\\n"
3795 + << " case MCD_OPC_FilterValue: {\\\n"
3796 +- << " // Decode the field value.\\\n"
3797 + << " unsigned Len;\\\n"
3798 + << " InsnType Val = decodeULEB128(++Ptr, &Len);\\\n"
3799 + << " Ptr += Len;\\\n"
3800 +- << " // NumToSkip is a plain 24-bit integer.\\\n"
3801 + << " unsigned NumToSkip = *Ptr++;\\\n"
3802 + << " NumToSkip |= (*Ptr++) << 8;\\\n"
3803 + << " NumToSkip |= (*Ptr++) << 16;\\\n"
3804 + << "\\\n"
3805 +- << " // Perform the filter operation.\\\n"
3806 + << " if (Val != CurFieldValue)\\\n"
3807 + << " Ptr += NumToSkip;\\\n"
3808 +- << "\\\n"
3809 + << " break;\\\n"
3810 + << " }\\\n"
3811 + << " case MCD_OPC_CheckField: {\\\n"
3812 + << " unsigned Start = *++Ptr;\\\n"
3813 + << " unsigned Len = *++Ptr;\\\n"
3814 +- << " InsnType FieldValue = fieldFromInstruction(insn, Start, Len);\\\n"
3815 +- << " // Decode the field value.\\\n"
3816 ++ << " InsnType FieldValue = fieldname(insn, Start, Len);\\\n"
3817 + << " uint32_t ExpectedValue = decodeULEB128(++Ptr, &Len);\\\n"
3818 + << " Ptr += Len;\\\n"
3819 +- << " // NumToSkip is a plain 24-bit integer.\\\n"
3820 + << " unsigned NumToSkip = *Ptr++;\\\n"
3821 + << " NumToSkip |= (*Ptr++) << 8;\\\n"
3822 + << " NumToSkip |= (*Ptr++) << 16;\\\n"
3823 + << "\\\n"
3824 +- << " // If the actual and expected values don't match, skip.\\\n"
3825 + << " if (ExpectedValue != FieldValue)\\\n"
3826 + << " Ptr += NumToSkip;\\\n"
3827 + << " break;\\\n"
3828 + << " }\\\n"
3829 + << " case MCD_OPC_CheckPredicate: {\\\n"
3830 + << " unsigned Len;\\\n"
3831 +- << " // Decode the Predicate Index value.\\\n"
3832 + << " unsigned PIdx = decodeULEB128(++Ptr, &Len);\\\n"
3833 + << " Ptr += Len;\\\n"
3834 +- << " // NumToSkip is a plain 24-bit integer.\\\n"
3835 + << " unsigned NumToSkip = *Ptr++;\\\n"
3836 + << " NumToSkip |= (*Ptr++) << 8;\\\n"
3837 + << " NumToSkip |= (*Ptr++) << 16;\\\n"
3838 +- << " // Check the predicate.\\\n"
3839 + << " bool Pred;\\\n"
3840 + << " if (!(Pred = checkDecoderPredicate(PIdx, Bits)))\\\n"
3841 + << " Ptr += NumToSkip;\\\n"
3842 +@@ -2407,7 +2409,6 @@ static void emitDecodeInstruction(formatted_raw_ostream &OS) {
3843 + << " }\\\n"
3844 + << " case MCD_OPC_Decode: {\\\n"
3845 + << " unsigned Len;\\\n"
3846 +- << " // Decode the Opcode value.\\\n"
3847 + << " unsigned Opc = decodeULEB128(++Ptr, &Len);\\\n"
3848 + << " Ptr += Len;\\\n"
3849 + << " unsigned DecodeIdx = decodeULEB128(Ptr, &Len);\\\n"
3850 +@@ -2416,47 +2417,39 @@ static void emitDecodeInstruction(formatted_raw_ostream &OS) {
3851 + << " MCInst_clear(MI);\\\n"
3852 + << " MCInst_setOpcode(MI, Opc);\\\n"
3853 + << " bool DecodeComplete;\\\n"
3854 +- << " S = decodeToMCInst(S, DecodeIdx, insn, MI, Address, DisAsm, "
3855 +- "DecodeComplete);\\\n"
3856 ++ << " S = decoder(S, DecodeIdx, insn, MI, Address, DisAsm, "
3857 ++ "&DecodeComplete);\\\n"
3858 + << " assert(DecodeComplete);\\\n"
3859 + << "\\\n"
3860 + << " return S;\\\n"
3861 + << " }\\\n"
3862 + << " case MCD_OPC_TryDecode: {\\\n"
3863 + << " unsigned Len;\\\n"
3864 +- << " // Decode the Opcode value.\\\n"
3865 + << " unsigned Opc = decodeULEB128(++Ptr, &Len);\\\n"
3866 + << " Ptr += Len;\\\n"
3867 + << " unsigned DecodeIdx = decodeULEB128(Ptr, &Len);\\\n"
3868 + << " Ptr += Len;\\\n"
3869 +- << " // NumToSkip is a plain 24-bit integer.\\\n"
3870 + << " unsigned NumToSkip = *Ptr++;\\\n"
3871 + << " NumToSkip |= (*Ptr++) << 8;\\\n"
3872 + << " NumToSkip |= (*Ptr++) << 16;\\\n"
3873 + << "\\\n"
3874 +- << " // Perform the decode operation.\\\n"
3875 + << " MCInst TmpMI;\\\n"
3876 + << " MCInst_setOpcode(&TmpMI, Opc);\\\n"
3877 +- << " bool DecodeComplete;\n"
3878 +- << " S = decodeToMCInst(S, DecodeIdx, insn, &TmpMI, Address, DisAsm, "
3879 +- "DecodeComplete);\\\n"
3880 ++ << " bool DecodeComplete;\\\n"
3881 ++ << " S = decoder(S, DecodeIdx, insn, &TmpMI, Address, DisAsm, "
3882 ++ "&DecodeComplete);\\\n"
3883 ++ << "\\\n"
3884 + << " if (DecodeComplete) {\\\n"
3885 +- << " // Decoding complete.\\\n"
3886 +- << " MI = &TmpMI;\\\n"
3887 ++ << " *MI = TmpMI;\\\n"
3888 + << " return S;\\\n"
3889 + << " } else {\\\n"
3890 + << " assert(S == MCDisassembler_Fail);\\\n"
3891 +- << " // If the decoding was incomplete, skip.\\\n"
3892 + << " Ptr += NumToSkip;\\\n"
3893 +- << " // Reset decode status. This also drops a SoftFail status "
3894 +- "that could be\\\n"
3895 +- << " // set before the decode attempt.\\\n"
3896 + << " S = MCDisassembler_Success;\\\n"
3897 + << " }\\\n"
3898 + << " break;\\\n"
3899 + << " }\\\n"
3900 + << " case MCD_OPC_SoftFail: {\\\n"
3901 +- << " // Decode the mask values.\\\n"
3902 + << " unsigned Len;\\\n"
3903 + << " InsnType PositiveMask = decodeULEB128(++Ptr, &Len);\\\n"
3904 + << " Ptr += Len;\\\n"
3905 +@@ -2472,8 +2465,8 @@ static void emitDecodeInstruction(formatted_raw_ostream &OS) {
3906 + << " }\\\n"
3907 + << " }\\\n"
3908 + << " }\\\n"
3909 +- << " assert(0);\\\n"
3910 +-
3911 ++ << " assert(0 && \"bogosity detected in disassembler state "
3912 ++ "machine!\");\\\n"
3913 + #else
3914 + OS << "template<typename InsnType>\n"
3915 + << "static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], "
3916 +@@ -2752,9 +2745,10 @@ void FixedLenDecoderEmitter::run(raw_ostream &o) {
3917 + emitDecodeInstruction(OS);
3918 +
3919 + #ifdef CAPSTONE
3920 +- OS << "FieldFromInstruction(fieldFromInstruction, uint64_t)\n";
3921 +- OS << "DecodeToMCInst(decodeToMCInst, fieldFromInstruction, uint64_t)\n";
3922 +- OS << "DecodeInstruction(decodeInstruction, fieldFromInstruction, decodeToMCInst, uint64_t)\n";
3923 ++ OS << "// For RISCV instruction is 32 bits.\n";
3924 ++ OS << "FieldFromInstruction(fieldFromInstruction, uint32_t)\n";
3925 ++ OS << "DecodeToMCInst(decodeToMCInst, fieldFromInstruction, uint32_t)\n";
3926 ++ OS << "DecodeInstruction(decodeInstruction, fieldFromInstruction, decodeToMCInst, uint32_t)\n";
3927 + #else
3928 + OS << "\n} // End llvm namespace\n";
3929 + #endif
3930 +diff --git a/llvm/utils/TableGen/InstrInfoEmitter.cpp b/llvm/utils/TableGen/InstrInfoEmitter.cpp
3931 +index 01605184f..e59dace24 100644
3932 +--- a/llvm/utils/TableGen/InstrInfoEmitter.cpp
3933 ++++ b/llvm/utils/TableGen/InstrInfoEmitter.cpp
3934 +@@ -769,6 +769,7 @@ void EmitInstrInfo(RecordKeeper &RK, raw_ostream &OS) {
3935 + #ifdef CAPSTONE
3936 + std::string GetPublicName(const CodeGenInstruction *Inst) {
3937 + std::string Name = Inst->TheDef->getName();
3938 ++#if 0
3939 + // Apply backward compatibility fixups.
3940 + // BRNLE -> BNLER.
3941 + if (Name.length() >= 5 && Name.substr(0, 5) == "BRAsm") {
3942 +@@ -785,7 +786,7 @@ std::string GetPublicName(const CodeGenInstruction *Inst) {
3943 + break;
3944 + }
3945 + Name = Name.substr(0, pos) + Name.substr(pos + 3);
3946 +- }
3947 ++ }f 0
3948 + // CPSDRxx -> CPSDR.
3949 + if (Name.length() >= 2) {
3950 + std::string Suffix2 = Name.substr(Name.length() - 2, 2);
3951 +@@ -794,7 +795,8 @@ std::string GetPublicName(const CodeGenInstruction *Inst) {
3952 + Name = Name.substr(0, Name.length() - 2);
3953 + }
3954 + }
3955 +- return "SYSZ_INS_" + Name;
3956 ++#endif
3957 ++ return "RISCV_INS_" + Name;
3958 + }
3959 +
3960 + std::string GetRegisterName(Record *Reg) {
3961 +@@ -802,6 +804,7 @@ std::string GetRegisterName(Record *Reg) {
3962 + for (char& c : Name) {
3963 + c = toupper(c);
3964 + }
3965 ++#if 0
3966 + // R0L, R0D -> R0.
3967 + if (Name.length() >= 3 &&
3968 + Name[Name.length() - 3] == 'R' &&
3969 +@@ -809,7 +812,8 @@ std::string GetRegisterName(Record *Reg) {
3970 + Name[Name.length() - 1] == 'D')) {
3971 + Name = Name.substr(0, Name.length() - 3) + Name[Name.length() - 2];
3972 + }
3973 +- return "SYSZ_REG_" + Name;
3974 ++#endif
3975 ++ return "RISCV_REG_" + Name;
3976 + }
3977 +
3978 + std::string GetGroupName(Record *Pred) {
3979 +@@ -817,10 +821,12 @@ std::string GetGroupName(Record *Pred) {
3980 + for (char& c : Name) {
3981 + c = toupper(c);
3982 + }
3983 ++#if 0
3984 + if (Name.length() >= 7 && Name.substr(0, 7) == "FEATURE") {
3985 + Name = Name.substr(7);
3986 + }
3987 +- return "SYSZ_GRP_" + Name;
3988 ++#endif
3989 ++ return "RISCV_GRP_" + Name;
3990 + }
3991 +
3992 + void EmitMappingInsn(RecordKeeper &RK, raw_ostream &OS) {
3993 +diff --git a/llvm/utils/TableGen/RegisterInfoEmitter.cpp b/llvm/utils/TableGen/RegisterInfoEmitter.cpp
3994 +index 0df306680..cf9c352d7 100644
3995 +--- a/llvm/utils/TableGen/RegisterInfoEmitter.cpp
3996 ++++ b/llvm/utils/TableGen/RegisterInfoEmitter.cpp
3997 +@@ -180,12 +180,20 @@ void RegisterInfoEmitter::runEnums(raw_ostream &OS,
3998 + #endif
3999 + }
4000 +
4001 +-#ifndef CAPSTONE
4002 + const std::vector<Record*> &RegAltNameIndices = Target.getRegAltNameIndices();
4003 + // If the only definition is the default NoRegAltName, we don't need to
4004 + // emit anything.
4005 + if (RegAltNameIndices.size() > 1) {
4006 + OS << "\n// Register alternate name indices\n\n";
4007 ++#ifdef CAPSTONE
4008 ++ OS << "enum {\n";
4009 ++ for (unsigned i = 0, e = RegAltNameIndices.size(); i != e; ++i)
4010 ++ OS << " " << NAME_PREFIX RegAltNameIndices[i]->getName()
4011 ++ << ",\t// " << i << "\n";
4012 ++ OS << " " << NAME_PREFIX "NUM_TARGET_REG_ALT_NAMES = "
4013 ++ << RegAltNameIndices.size() << "\n";
4014 ++ OS << "};\n";
4015 ++#else
4016 + if (!Namespace.empty())
4017 + OS << "namespace " << Namespace << " {\n";
4018 + OS << "enum {\n";
4019 +@@ -195,11 +203,19 @@ void RegisterInfoEmitter::runEnums(raw_ostream &OS,
4020 + OS << "};\n";
4021 + if (!Namespace.empty())
4022 + OS << "} // end namespace " << Namespace << "\n\n";
4023 ++#endif
4024 + }
4025 +
4026 + auto &SubRegIndices = Bank.getSubRegIndices();
4027 + if (!SubRegIndices.empty()) {
4028 + OS << "\n// Subregister indices\n\n";
4029 ++#ifdef CAPSTONE
4030 ++ OS << "enum {\n" << " " << NAME_PREFIX "NoSubRegister,\n";
4031 ++ unsigned i = 0;
4032 ++ for (const auto &Idx : SubRegIndices)
4033 ++ OS << " " << NAME_PREFIX Idx.getName() << ",\t// " << ++i << "\n";
4034 ++ OS << " " << NAME_PREFIX "NUM_TARGET_SUBREGS\n};\n";
4035 ++#else
4036 + std::string Namespace = SubRegIndices.front().getNamespace();
4037 + if (!Namespace.empty())
4038 + OS << "namespace " << Namespace << " {\n";
4039 +@@ -210,8 +226,8 @@ void RegisterInfoEmitter::runEnums(raw_ostream &OS,
4040 + OS << " NUM_TARGET_SUBREGS\n};\n";
4041 + if (!Namespace.empty())
4042 + OS << "} // end namespace " << Namespace << "\n\n";
4043 +- }
4044 + #endif
4045 ++ }
4046 +
4047 + #ifndef CAPSTONE
4048 + OS << "} // end namespace llvm\n\n";
4049 +diff --git a/llvm/utils/TableGen/TableGen.cpp b/llvm/utils/TableGen/TableGen.cpp
4050 +index 9e2a868be..7ec93c0e0 100644
4051 +--- a/llvm/utils/TableGen/TableGen.cpp
4052 ++++ b/llvm/utils/TableGen/TableGen.cpp
4053 +@@ -27,8 +27,10 @@ enum ActionType {
4054 + GenEmitter,
4055 + GenRegisterInfo,
4056 + GenInstrInfo,
4057 ++#ifdef CAPSTONE
4058 + GenMappingInsn,
4059 + GenInsnNameMaps,
4060 ++#endif
4061 + GenInstrDocs,
4062 + GenAsmWriter,
4063 + GenAsmMatcher,
4064 +@@ -76,10 +78,12 @@ namespace {
4065 + "Generate registers and register classes info"),
4066 + clEnumValN(GenInstrInfo, "gen-instr-info",
4067 + "Generate instruction descriptions"),
4068 ++#ifdef CAPSTONE
4069 + clEnumValN(GenMappingInsn, "gen-mapping-insn",
4070 + ""),
4071 + clEnumValN(GenInsnNameMaps, "gen-insn-name-maps",
4072 + ""),
4073 ++#endif
4074 + clEnumValN(GenInstrDocs, "gen-instr-docs",
4075 + "Generate instruction documentation"),
4076 + clEnumValN(GenCallingConv, "gen-callingconv",
4077 +@@ -160,12 +164,14 @@ bool LLVMTableGenMain(raw_ostream &OS, RecordKeeper &Records) {
4078 + case GenInstrInfo:
4079 + EmitInstrInfo(Records, OS);
4080 + break;
4081 ++#ifdef CAPSTONE
4082 + case GenMappingInsn:
4083 + EmitMappingInsn(Records, OS);
4084 + break;
4085 + case GenInsnNameMaps:
4086 + EmitInsnNameMaps(Records, OS);
4087 + break;
4088 ++#endif
4089 + case GenInstrDocs:
4090 + EmitInstrDocs(Records, OS);
4091 + break;
4092 +--
4093 +2.20.1
4094 +
4095 diff --git a/llvm/0003-clear-old-patchs.patch b/llvm/0003-clear-old-patchs.patch
4096 new file mode 100644
4097 index 000000000..720024662
4098 --- /dev/null
4099 +++ b/llvm/0003-clear-old-patchs.patch
4100 @@ -0,0 +1,1602 @@
4101 +From 02eecf3f85ad03f12babab3067f2c1bcfff35ed3 Mon Sep 17 00:00:00 2001
4102 +From: fanfuqiang <[email protected]>
4103 +Date: Thu, 28 Feb 2019 01:50:13 +0800
4104 +Subject: [PATCH] clear old patchs
4105 +
4106 +---
4107 + ...apstone-generate-GenRegisterInfo.inc.patch | 338 -------------
4108 + ...pstone-generate-GenSubtargetInfo.inc.patch | 86 ----
4109 + ...3-capstone-generate-GenInstrInfo.inc.patch | 130 -----
4110 + ...e-generate-GenDisassemblerTables.inc.patch | 472 ------------------
4111 + ...5-capstone-generate-GenAsmWriter.inc.patch | 225 ---------
4112 + ...06-capstone-generate-MappingInsn.inc.patch | 174 -------
4113 + ...apstone-generate-GenInsnNameMaps.inc.patch | 110 ----
4114 + 7 files changed, 1535 deletions(-)
4115 + delete mode 100644 llvm/0001-capstone-generate-GenRegisterInfo.inc.patch
4116 + delete mode 100644 llvm/0002-capstone-generate-GenSubtargetInfo.inc.patch
4117 + delete mode 100644 llvm/0003-capstone-generate-GenInstrInfo.inc.patch
4118 + delete mode 100644 llvm/0004-capstone-generate-GenDisassemblerTables.inc.patch
4119 + delete mode 100644 llvm/0005-capstone-generate-GenAsmWriter.inc.patch
4120 + delete mode 100644 llvm/0006-capstone-generate-MappingInsn.inc.patch
4121 + delete mode 100644 llvm/0007-capstone-generate-GenInsnNameMaps.inc.patch
4122 +
4123 +diff --git a/llvm/0001-capstone-generate-GenRegisterInfo.inc.patch b/llvm/0001-capstone-generate-GenRegisterInfo.inc.patch
4124 +deleted file mode 100644
4125 +index b51aa515a..000000000
4126 +--- a/llvm/0001-capstone-generate-GenRegisterInfo.inc.patch
4127 ++++ /dev/null
4128 +@@ -1,338 +0,0 @@
4129 +-From 5d631cb16e7ba5dd0380ff1ee9dda192b1cdad18 Mon Sep 17 00:00:00 2001
4130 +-From: mephi42 <[email protected]>
4131 +-Date: Tue, 7 Aug 2018 17:02:40 +0200
4132 +-Subject: [PATCH 1/7] capstone: generate *GenRegisterInfo.inc
4133 +-
4134 +----
4135 +- utils/TableGen/RegisterInfoEmitter.cpp | 130 ++++++++++++++++++++++---
4136 +- 1 file changed, 115 insertions(+), 15 deletions(-)
4137 +-
4138 +-diff --git a/utils/TableGen/RegisterInfoEmitter.cpp b/utils/TableGen/RegisterInfoEmitter.cpp
4139 +-index 49016cca799..6ebb7148b1b 100644
4140 +---- a/utils/TableGen/RegisterInfoEmitter.cpp
4141 +-+++ b/utils/TableGen/RegisterInfoEmitter.cpp
4142 +-@@ -99,6 +99,12 @@ private:
4143 +-
4144 +- } // end anonymous namespace
4145 +-
4146 +-+#ifdef CAPSTONE
4147 +-+#define NAME_PREFIX Target.getName() << "_" <<
4148 +-+#else
4149 +-+#define NAME_PREFIX
4150 +-+#endif
4151 +-+
4152 +- // runEnums - Print out enum values for all of the registers.
4153 +- void RegisterInfoEmitter::runEnums(raw_ostream &OS,
4154 +- CodeGenTarget &Target, CodeGenRegBank &Bank) {
4155 +-@@ -107,13 +113,22 @@ void RegisterInfoEmitter::runEnums(raw_ostream &OS,
4156 +- // Register enums are stored as uint16_t in the tables. Make sure we'll fit.
4157 +- assert(Registers.size() <= 0xffff && "Too many regs to fit in tables");
4158 +-
4159 +-+#ifndef CAPSTONE
4160 +- StringRef Namespace = Registers.front().TheDef->getValueAsString("Namespace");
4161 +-+#endif
4162 +-
4163 +- emitSourceFileHeader("Target Register Enum Values", OS);
4164 +-
4165 +-+#ifdef CAPSTONE
4166 +-+ OS << "/* Capstone Disassembly Engine */\n"
4167 +-+ "/* By Nguyen Anh Quynh <[email protected]>, 2013-2015 */\n"
4168 +-+ "\n";
4169 +-+#endif
4170 +-+
4171 +- OS << "\n#ifdef GET_REGINFO_ENUM\n";
4172 +- OS << "#undef GET_REGINFO_ENUM\n\n";
4173 +-
4174 +-+#ifndef CAPSTONE
4175 +- OS << "namespace llvm {\n\n";
4176 +-
4177 +- OS << "class MCRegisterClass;\n"
4178 +-@@ -122,16 +137,20 @@ void RegisterInfoEmitter::runEnums(raw_ostream &OS,
4179 +-
4180 +- if (!Namespace.empty())
4181 +- OS << "namespace " << Namespace << " {\n";
4182 +-- OS << "enum {\n NoRegister,\n";
4183 +-+#endif
4184 +-+
4185 +-+ OS << "enum {\n " << NAME_PREFIX "NoRegister,\n";
4186 +-
4187 +- for (const auto &Reg : Registers)
4188 +-- OS << " " << Reg.getName() << " = " << Reg.EnumValue << ",\n";
4189 +-+ OS << " " << NAME_PREFIX Reg.getName() << " = " << Reg.EnumValue << ",\n";
4190 +- assert(Registers.size() == Registers.back().EnumValue &&
4191 +- "Register enum value mismatch!");
4192 +-- OS << " NUM_TARGET_REGS \t// " << Registers.size()+1 << "\n";
4193 +-+ OS << " " << NAME_PREFIX "NUM_TARGET_REGS \t// " << Registers.size()+1 << "\n";
4194 +- OS << "};\n";
4195 +-+#ifndef CAPSTONE
4196 +- if (!Namespace.empty())
4197 +- OS << "} // end namespace " << Namespace << "\n";
4198 +-+#endif
4199 +-
4200 +- const auto &RegisterClasses = Bank.getRegClasses();
4201 +- if (!RegisterClasses.empty()) {
4202 +-@@ -140,18 +159,29 @@ void RegisterInfoEmitter::runEnums(raw_ostream &OS,
4203 +- assert(RegisterClasses.size() <= 0xffff &&
4204 +- "Too many register classes to fit in tables");
4205 +-
4206 +-- OS << "\n// Register classes\n\n";
4207 +-+ OS << "\n// Register classes\n";
4208 +-+#ifndef CAPSTONE
4209 +-+ OS << "\n";
4210 +- if (!Namespace.empty())
4211 +- OS << "namespace " << Namespace << " {\n";
4212 +-+#endif
4213 +- OS << "enum {\n";
4214 +- for (const auto &RC : RegisterClasses)
4215 +-- OS << " " << RC.getName() << "RegClassID"
4216 +-+ OS << " " << NAME_PREFIX RC.getName() << "RegClassID"
4217 +- << " = " << RC.EnumValue << ",\n";
4218 +-- OS << "\n };\n";
4219 +-+#ifdef CAPSTONE
4220 +-+ OS
4221 +-+#else
4222 +-+ OS << "\n "
4223 +-+#endif
4224 +-+ << "};\n";
4225 +-+#ifndef CAPSTONE
4226 +- if (!Namespace.empty())
4227 +- OS << "} // end namespace " << Namespace << "\n\n";
4228 +-+#endif
4229 +- }
4230 +-
4231 +-+#ifndef CAPSTONE
4232 +- const std::vector<Record*> &RegAltNameIndices = Target.getRegAltNameIndices();
4233 +- // If the only definition is the default NoRegAltName, we don't need to
4234 +- // emit anything.
4235 +-@@ -182,8 +212,11 @@ void RegisterInfoEmitter::runEnums(raw_ostream &OS,
4236 +- if (!Namespace.empty())
4237 +- OS << "} // end namespace " << Namespace << "\n\n";
4238 +- }
4239 +-+#endif
4240 +-
4241 +-+#ifndef CAPSTONE
4242 +- OS << "} // end namespace llvm\n\n";
4243 +-+#endif
4244 +- OS << "#endif // GET_REGINFO_ENUM\n\n";
4245 +- }
4246 +-
4247 +-@@ -830,7 +863,9 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
4248 +-
4249 +- const auto &Regs = RegBank.getRegisters();
4250 +-
4251 +-+#ifndef CAPSTONE
4252 +- auto &SubRegIndices = RegBank.getSubRegIndices();
4253 +-+#endif
4254 +- // The lists of sub-registers and super-registers go in the same array. That
4255 +- // allows us to share suffixes.
4256 +- typedef std::vector<const CodeGenRegister*> RegVec;
4257 +-@@ -922,25 +957,40 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
4258 +- LaneMaskSeqs.layout();
4259 +- SubRegIdxSeqs.layout();
4260 +-
4261 +-+#ifndef CAPSTONE
4262 +- OS << "namespace llvm {\n\n";
4263 +-+#endif
4264 +-
4265 +- const std::string &TargetName = Target.getName();
4266 +-
4267 +- // Emit the shared table of differential lists.
4268 +-- OS << "extern const MCPhysReg " << TargetName << "RegDiffLists[] = {\n";
4269 +-+#ifdef CAPSTONE
4270 +-+ OS << "static"
4271 +-+#else
4272 +-+ OS << "extern"
4273 +-+#endif
4274 +-+ << " const MCPhysReg " << TargetName << "RegDiffLists[] = {\n";
4275 +- DiffSeqs.emit(OS, printDiff16);
4276 +- OS << "};\n\n";
4277 +-
4278 +-+#ifndef CAPSTONE
4279 +- // Emit the shared table of regunit lane mask sequences.
4280 +- OS << "extern const LaneBitmask " << TargetName << "LaneMaskLists[] = {\n";
4281 +- LaneMaskSeqs.emit(OS, printMask, "LaneBitmask::getAll()");
4282 +- OS << "};\n\n";
4283 +-+#endif
4284 +-
4285 +- // Emit the table of sub-register indexes.
4286 +-- OS << "extern const uint16_t " << TargetName << "SubRegIdxLists[] = {\n";
4287 +-+#ifdef CAPSTONE
4288 +-+ OS << "static"
4289 +-+#else
4290 +-+ OS << "extern"
4291 +-+#endif
4292 +-+ << " const uint16_t " << TargetName << "SubRegIdxLists[] = {\n";
4293 +- SubRegIdxSeqs.emit(OS, printSubRegIndex);
4294 +- OS << "};\n\n";
4295 +-
4296 +-+#ifndef CAPSTONE
4297 +- // Emit the table of sub-register index sizes.
4298 +- OS << "extern const MCRegisterInfo::SubRegCoveredBits "
4299 +- << TargetName << "SubRegIdxRanges[] = {\n";
4300 +-@@ -950,14 +1000,22 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
4301 +- << Idx.getName() << "\n";
4302 +- }
4303 +- OS << "};\n\n";
4304 +-+#endif
4305 +-
4306 +- // Emit the string table.
4307 +- RegStrings.layout();
4308 +-+#ifndef CAPSTONE
4309 +- OS << "extern const char " << TargetName << "RegStrings[] = {\n";
4310 +- RegStrings.emit(OS, printChar);
4311 +- OS << "};\n\n";
4312 +-+#endif
4313 +-
4314 +-- OS << "extern const MCRegisterDesc " << TargetName
4315 +-+#ifdef CAPSTONE
4316 +-+ OS << "static"
4317 +-+#else
4318 +-+ OS << "extern"
4319 +-+#endif
4320 +-+ << " const MCRegisterDesc " << TargetName
4321 +- << "RegDesc[] = { // Descriptors\n";
4322 +- OS << " { " << RegStrings.get("") << ", 0, 0, 0, 0, 0 },\n";
4323 +-
4324 +-@@ -973,6 +1031,7 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
4325 +- }
4326 +- OS << "};\n\n"; // End of register descriptors...
4327 +-
4328 +-+#ifndef CAPSTONE
4329 +- // Emit the table of register unit roots. Each regunit has one or two root
4330 +- // registers.
4331 +- OS << "extern const MCPhysReg " << TargetName << "RegUnitRoots[][2] = {\n";
4332 +-@@ -986,11 +1045,14 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
4333 +- OS << " },\n";
4334 +- }
4335 +- OS << "};\n\n";
4336 +-+#endif
4337 +-
4338 +- const auto &RegisterClasses = RegBank.getRegClasses();
4339 +-
4340 +- // Loop over all of the register classes... emitting each one.
4341 +-+#ifndef CAPSTONE
4342 +- OS << "namespace { // Register classes...\n";
4343 +-+#endif
4344 +-
4345 +- SequenceToOffsetTable<std::string> RegClassStrings;
4346 +-
4347 +-@@ -1005,15 +1067,28 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
4348 +-
4349 +- // Emit the register list now.
4350 +- OS << " // " << Name << " Register Class...\n"
4351 +-- << " const MCPhysReg " << Name
4352 +-+ << " "
4353 +-+#ifdef CAPSTONE
4354 +-+ << "static "
4355 +-+#endif
4356 +-+ << "const MCPhysReg " << Name
4357 +- << "[] = {\n ";
4358 +- for (Record *Reg : Order) {
4359 +-- OS << getQualifiedName(Reg) << ", ";
4360 +-+#ifdef CAPSTONE
4361 +-+ OS << NAME_PREFIX Reg->getName()
4362 +-+#else
4363 +-+ OS << getQualifiedName(Reg)
4364 +-+#endif
4365 +-+ << ", ";
4366 +- }
4367 +- OS << "\n };\n\n";
4368 +-
4369 +- OS << " // " << Name << " Bit set.\n"
4370 +-- << " const uint8_t " << Name
4371 +-+ << " "
4372 +-+#ifdef CAPSTONE
4373 +-+ << "static "
4374 +-+#endif
4375 +-+ << "const uint8_t " << Name
4376 +- << "Bits[] = {\n ";
4377 +- BitVectorEmitter BVE;
4378 +- for (Record *Reg : Order) {
4379 +-@@ -1023,14 +1098,23 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
4380 +- OS << "\n };\n\n";
4381 +-
4382 +- }
4383 +-+#ifndef CAPSTONE
4384 +- OS << "} // end anonymous namespace\n\n";
4385 +-+#endif
4386 +-
4387 +- RegClassStrings.layout();
4388 +-+#ifndef CAPSTONE
4389 +- OS << "extern const char " << TargetName << "RegClassStrings[] = {\n";
4390 +- RegClassStrings.emit(OS, printChar);
4391 +- OS << "};\n\n";
4392 +-+#endif
4393 +-
4394 +-- OS << "extern const MCRegisterClass " << TargetName
4395 +-+#ifdef CAPSTONE
4396 +-+ OS << "static"
4397 +-+#else
4398 +-+ OS << "extern"
4399 +-+#endif
4400 +-+ << " const MCRegisterClass " << TargetName
4401 +- << "MCRegisterClasses[] = {\n";
4402 +-
4403 +- for (const auto &RC : RegisterClasses) {
4404 +-@@ -1041,7 +1125,12 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
4405 +- OS << " { " << RC.getName() << ", " << RC.getName() << "Bits, "
4406 +- << RegClassStrings.get(RC.getName()) << ", "
4407 +- << RC.getOrder().size() << ", sizeof(" << RC.getName() << "Bits), "
4408 +-- << RC.getQualifiedName() + "RegClassID" << ", "
4409 +-+#ifdef CAPSTONE
4410 +-+ << NAME_PREFIX RC.getName()
4411 +-+#else
4412 +-+ << RC.getQualifiedName()
4413 +-+#endif
4414 +-+ << "RegClassID" << ", "
4415 +- << RegSize/8 << ", "
4416 +- << RC.CopyCost << ", "
4417 +- << ( RC.Allocatable ? "true" : "false" ) << " },\n";
4418 +-@@ -1049,6 +1138,7 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
4419 +-
4420 +- OS << "};\n\n";
4421 +-
4422 +-+#ifndef CAPSTONE
4423 +- EmitRegMappingTables(OS, Regs, false);
4424 +-
4425 +- // Emit Reg encoding table
4426 +-@@ -1067,7 +1157,9 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
4427 +- OS << " " << Value << ",\n";
4428 +- }
4429 +- OS << "};\n"; // End of HW encoding table
4430 +-+#endif
4431 +-
4432 +-+#ifndef CAPSTONE
4433 +- // MCRegisterInfo initialization routine.
4434 +- OS << "static inline void Init" << TargetName
4435 +- << "MCRegisterInfo(MCRegisterInfo *RI, unsigned RA, "
4436 +-@@ -1088,7 +1180,12 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
4437 +- OS << "}\n\n";
4438 +-
4439 +- OS << "} // end namespace llvm\n\n";
4440 +-- OS << "#endif // GET_REGINFO_MC_DESC\n\n";
4441 +-+#endif
4442 +-+ OS << "#endif // GET_REGINFO_MC_DESC\n"
4443 +-+#ifndef CAPSTONE
4444 +-+ << "\n"
4445 +-+#endif
4446 +-+ ;
4447 +- }
4448 +-
4449 +- void
4450 +-@@ -1568,10 +1665,13 @@ RegisterInfoEmitter::runTargetDesc(raw_ostream &OS, CodeGenTarget &Target,
4451 +-
4452 +- void RegisterInfoEmitter::run(raw_ostream &OS) {
4453 +- CodeGenRegBank &RegBank = Target.getRegBank();
4454 +-+
4455 +- runEnums(OS, Target, RegBank);
4456 +- runMCDesc(OS, Target, RegBank);
4457 +-+#ifndef CAPSTONE
4458 +- runTargetHeader(OS, Target, RegBank);
4459 +- runTargetDesc(OS, Target, RegBank);
4460 +-+#endif
4461 +-
4462 +- if (RegisterInfoDebug)
4463 +- debugDump(errs());
4464 +---
4465 +-2.19.1
4466 +-
4467 +diff --git a/llvm/0002-capstone-generate-GenSubtargetInfo.inc.patch b/llvm/0002-capstone-generate-GenSubtargetInfo.inc.patch
4468 +deleted file mode 100644
4469 +index 56ad28256..000000000
4470 +--- a/llvm/0002-capstone-generate-GenSubtargetInfo.inc.patch
4471 ++++ /dev/null
4472 +@@ -1,86 +0,0 @@
4473 +-From 46ca491e1bbbc9ace2a91fe6a7b112c83b9b88cc Mon Sep 17 00:00:00 2001
4474 +-From: mephi42 <[email protected]>
4475 +-Date: Tue, 7 Aug 2018 17:42:59 +0200
4476 +-Subject: [PATCH 2/7] capstone: generate *GenSubtargetInfo.inc
4477 +-
4478 +----
4479 +- utils/TableGen/SubtargetEmitter.cpp | 28 +++++++++++++++++++++++++++-
4480 +- 1 file changed, 27 insertions(+), 1 deletion(-)
4481 +-
4482 +-diff --git a/utils/TableGen/SubtargetEmitter.cpp b/utils/TableGen/SubtargetEmitter.cpp
4483 +-index c5da8d8142f..98ab3240472 100644
4484 +---- a/utils/TableGen/SubtargetEmitter.cpp
4485 +-+++ b/utils/TableGen/SubtargetEmitter.cpp
4486 +-@@ -147,7 +147,9 @@ void SubtargetEmitter::Enumeration(raw_ostream &OS) {
4487 +- if (N > MAX_SUBTARGET_FEATURES)
4488 +- PrintFatalError("Too many subtarget features! Bump MAX_SUBTARGET_FEATURES.");
4489 +-
4490 +-+#ifndef CAPSTONE
4491 +- OS << "namespace " << Target << " {\n";
4492 +-+#endif
4493 +-
4494 +- // Open enumeration.
4495 +- OS << "enum {\n";
4496 +-@@ -158,12 +160,22 @@ void SubtargetEmitter::Enumeration(raw_ostream &OS) {
4497 +- Record *Def = DefList[i];
4498 +-
4499 +- // Get and emit name
4500 +-- OS << " " << Def->getName() << " = " << i << ",\n";
4501 +-+ OS << " "
4502 +-+#ifdef CAPSTONE
4503 +-+ << Target << "_"
4504 +-+#endif
4505 +-+ << Def->getName() << " = "
4506 +-+#ifdef CAPSTONE
4507 +-+ << "1ULL << "
4508 +-+#endif
4509 +-+ << i << ",\n";
4510 +- }
4511 +-
4512 +- // Close enumeration and namespace
4513 +- OS << "};\n";
4514 +-+#ifndef CAPSTONE
4515 +- OS << "} // end namespace " << Target << "\n";
4516 +-+#endif
4517 +- }
4518 +-
4519 +- //
4520 +-@@ -1709,14 +1721,27 @@ void SubtargetEmitter::emitGenMCSubtargetInfo(raw_ostream &OS) {
4521 +- void SubtargetEmitter::run(raw_ostream &OS) {
4522 +- emitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
4523 +-
4524 +-+#ifdef CAPSTONE
4525 +-+ OS << "/* Capstone Disassembly Engine, http://www.capstone-engine.org */\n"
4526 +-+ "/* By Nguyen Anh Quynh <[email protected]>, 2013-2015 */\n"
4527 +-+ "\n";
4528 +-+#endif
4529 +-+
4530 +- OS << "\n#ifdef GET_SUBTARGETINFO_ENUM\n";
4531 +- OS << "#undef GET_SUBTARGETINFO_ENUM\n\n";
4532 +-
4533 +-+#ifndef CAPSTONE
4534 +- OS << "namespace llvm {\n";
4535 +-+#endif
4536 +- Enumeration(OS);
4537 +-+#ifdef CAPSTONE
4538 +-+ OS << "\n";
4539 +-+#else
4540 +- OS << "} // end namespace llvm\n\n";
4541 +-+#endif
4542 +- OS << "#endif // GET_SUBTARGETINFO_ENUM\n\n";
4543 +-
4544 +-+#ifndef CAPSTONE
4545 +- OS << "\n#ifdef GET_SUBTARGETINFO_MC_DESC\n";
4546 +- OS << "#undef GET_SUBTARGETINFO_MC_DESC\n\n";
4547 +-
4548 +-@@ -1857,6 +1882,7 @@ void SubtargetEmitter::run(raw_ostream &OS) {
4549 +- OS << "} // end namespace llvm\n\n";
4550 +-
4551 +- OS << "#endif // GET_SUBTARGETINFO_CTOR\n\n";
4552 +-+#endif
4553 +- }
4554 +-
4555 +- namespace llvm {
4556 +---
4557 +-2.19.1
4558 +-
4559 +diff --git a/llvm/0003-capstone-generate-GenInstrInfo.inc.patch b/llvm/0003-capstone-generate-GenInstrInfo.inc.patch
4560 +deleted file mode 100644
4561 +index 2baa59fc9..000000000
4562 +--- a/llvm/0003-capstone-generate-GenInstrInfo.inc.patch
4563 ++++ /dev/null
4564 +@@ -1,130 +0,0 @@
4565 +-From a73fe8ac18d3ca81fa7a8d8c404cd7e0faf92ddc Mon Sep 17 00:00:00 2001
4566 +-From: mephi42 <[email protected]>
4567 +-Date: Tue, 7 Aug 2018 17:59:43 +0200
4568 +-Subject: [PATCH 3/7] capstone: generate *GenInstrInfo.inc
4569 +-
4570 +----
4571 +- utils/TableGen/InstrInfoEmitter.cpp | 49 ++++++++++++++++++++++++++---
4572 +- 1 file changed, 44 insertions(+), 5 deletions(-)
4573 +-
4574 +-diff --git a/utils/TableGen/InstrInfoEmitter.cpp b/utils/TableGen/InstrInfoEmitter.cpp
4575 +-index 0aff1aa6f94..2f3a2729262 100644
4576 +---- a/utils/TableGen/InstrInfoEmitter.cpp
4577 +-+++ b/utils/TableGen/InstrInfoEmitter.cpp
4578 +-@@ -92,6 +92,7 @@ private:
4579 +-
4580 +- } // end anonymous namespace
4581 +-
4582 +-+#ifndef CAPSTONE
4583 +- static void PrintDefList(const std::vector<Record*> &Uses,
4584 +- unsigned Num, raw_ostream &OS) {
4585 +- OS << "static const MCPhysReg ImplicitList" << Num << "[] = { ";
4586 +-@@ -99,6 +100,7 @@ static void PrintDefList(const std::vector<Record*> &Uses,
4587 +- OS << getQualifiedName(U) << ", ";
4588 +- OS << "0 };\n";
4589 +- }
4590 +-+#endif
4591 +-
4592 +- //===----------------------------------------------------------------------===//
4593 +- // Operand Info Emission.
4594 +-@@ -426,8 +428,17 @@ void InstrInfoEmitter::emitTIIHelperMethods(raw_ostream &OS) {
4595 +- // run - Emit the main instruction description records for the target...
4596 +- void InstrInfoEmitter::run(raw_ostream &OS) {
4597 +- emitSourceFileHeader("Target Instruction Enum Values and Descriptors", OS);
4598 +-+
4599 +-+#ifdef CAPSTONE
4600 +-+ OS << "/* Capstone Disassembly Engine */\n"
4601 +-+ "/* By Nguyen Anh Quynh <[email protected]>, 2013-2015 */\n"
4602 +-+ "\n"
4603 +-+ "\n";
4604 +-+#endif
4605 +-+
4606 +- emitEnums(OS);
4607 +-
4608 +-+#ifndef CAPSTONE
4609 +- OS << "#ifdef GET_INSTRINFO_MC_DESC\n";
4610 +- OS << "#undef GET_INSTRINFO_MC_DESC\n";
4611 +-
4612 +-@@ -545,6 +556,7 @@ void InstrInfoEmitter::run(raw_ostream &OS) {
4613 +- emitOperandTypesEnum(OS, Target);
4614 +-
4615 +- emitMCIIHelperMethods(OS);
4616 +-+#endif
4617 +- }
4618 +-
4619 +- void InstrInfoEmitter::emitRecord(const CodeGenInstruction &Inst, unsigned Num,
4620 +-@@ -659,7 +671,9 @@ void InstrInfoEmitter::emitEnums(raw_ostream &OS) {
4621 +- OS << "#ifdef GET_INSTRINFO_ENUM\n";
4622 +- OS << "#undef GET_INSTRINFO_ENUM\n";
4623 +-
4624 +-+#ifndef CAPSTONE
4625 +- OS << "namespace llvm {\n\n";
4626 +-+#endif
4627 +-
4628 +- CodeGenTarget Target(Records);
4629 +-
4630 +-@@ -669,17 +683,39 @@ void InstrInfoEmitter::emitEnums(raw_ostream &OS) {
4631 +- if (Namespace.empty())
4632 +- PrintFatalError("No instructions defined!");
4633 +-
4634 +-+#ifndef CAPSTONE
4635 +- OS << "namespace " << Namespace << " {\n";
4636 +-- OS << " enum {\n";
4637 +-+#endif
4638 +-+#ifdef CAPSTONE
4639 +-+ OS << "\n"
4640 +-+#else
4641 +-+ OS << " "
4642 +-+#endif
4643 +-+ << "enum {\n";
4644 +- unsigned Num = 0;
4645 +- for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue())
4646 +-- OS << " " << Inst->TheDef->getName() << "\t= " << Num++ << ",\n";
4647 +-- OS << " INSTRUCTION_LIST_END = " << Num << "\n";
4648 +-+ OS << " "
4649 +-+#ifdef CAPSTONE
4650 +-+ << Target.getName() << "_"
4651 +-+#endif
4652 +-+ << Inst->TheDef->getName() << "\t= " << Num++ << ",\n";
4653 +-+ OS << " "
4654 +-+#ifdef CAPSTONE
4655 +-+ << Target.getName() << "_"
4656 +-+#endif
4657 +-+ << "INSTRUCTION_LIST_END = " << Num << "\n";
4658 +- OS << " };\n\n";
4659 +-+#ifndef CAPSTONE
4660 +- OS << "} // end " << Namespace << " namespace\n";
4661 +- OS << "} // end llvm namespace\n";
4662 +-- OS << "#endif // GET_INSTRINFO_ENUM\n\n";
4663 +--
4664 +-+#endif
4665 +-+ OS << "#endif // GET_INSTRINFO_ENUM\n"
4666 +-+#ifndef CAPSTONE
4667 +-+ << "\n"
4668 +-+#endif
4669 +-+ ;
4670 +-+
4671 +-+#ifndef CAPSTONE
4672 +- OS << "#ifdef GET_INSTRINFO_SCHED_ENUM\n";
4673 +- OS << "#undef GET_INSTRINFO_SCHED_ENUM\n";
4674 +- OS << "namespace llvm {\n\n";
4675 +-@@ -696,13 +732,16 @@ void InstrInfoEmitter::emitEnums(raw_ostream &OS) {
4676 +- OS << "} // end llvm namespace\n";
4677 +-
4678 +- OS << "#endif // GET_INSTRINFO_SCHED_ENUM\n\n";
4679 +-+#endif
4680 +- }
4681 +-
4682 +- namespace llvm {
4683 +-
4684 +- void EmitInstrInfo(RecordKeeper &RK, raw_ostream &OS) {
4685 +- InstrInfoEmitter(RK).run(OS);
4686 +-+#ifndef CAPSTONE
4687 +- EmitMapTable(RK, OS);
4688 +-+#endif
4689 +- }
4690 +-
4691 +- } // end llvm namespace
4692 +---
4693 +-2.19.1
4694 +-
4695 +diff --git a/llvm/0004-capstone-generate-GenDisassemblerTables.inc.patch b/llvm/0004-capstone-generate-GenDisassemblerTables.inc.patch
4696 +deleted file mode 100644
4697 +index 0002b81b4..000000000
4698 +--- a/llvm/0004-capstone-generate-GenDisassemblerTables.inc.patch
4699 ++++ /dev/null
4700 +@@ -1,472 +0,0 @@
4701 +-From 29da4c6929679b8ac4019767ab4ebcd83c9894b4 Mon Sep 17 00:00:00 2001
4702 +-From: mephi42 <[email protected]>
4703 +-Date: Tue, 7 Aug 2018 18:20:17 +0200
4704 +-Subject: [PATCH 4/7] capstone: generate *GenDisassemblerTables.inc
4705 +-
4706 +----
4707 +- utils/TableGen/DisassemblerEmitter.cpp | 12 +-
4708 +- utils/TableGen/FixedLenDecoderEmitter.cpp | 248 ++++++++++++++++++++--
4709 +- 2 files changed, 239 insertions(+), 21 deletions(-)
4710 +-
4711 +-diff --git a/utils/TableGen/DisassemblerEmitter.cpp b/utils/TableGen/DisassemblerEmitter.cpp
4712 +-index b99a0a973a2..2ac6d89645c 100644
4713 +---- a/utils/TableGen/DisassemblerEmitter.cpp
4714 +-+++ b/utils/TableGen/DisassemblerEmitter.cpp
4715 +-@@ -106,6 +106,11 @@ extern void EmitFixedLenDecoder(RecordKeeper &RK, raw_ostream &OS,
4716 +- void EmitDisassembler(RecordKeeper &Records, raw_ostream &OS) {
4717 +- CodeGenTarget Target(Records);
4718 +- emitSourceFileHeader(" * " + Target.getName().str() + " Disassembler", OS);
4719 +-+#ifdef CAPSTONE
4720 +-+ OS << "/* Capstone Disassembly Engine */\n"
4721 +-+ "/* By Nguyen Anh Quynh <[email protected]>, 2013-2015 */\n"
4722 +-+ "\n";
4723 +-+#endif
4724 +-
4725 +- // X86 uses a custom disassembler.
4726 +- if (Target.getName() == "X86") {
4727 +-@@ -150,7 +155,12 @@ void EmitDisassembler(RecordKeeper &Records, raw_ostream &OS) {
4728 +- }
4729 +-
4730 +- EmitFixedLenDecoder(Records, OS, Target.getName(),
4731 +-- "if (", " == MCDisassembler::Fail)",
4732 +-+ "if (",
4733 +-+#ifdef CAPSTONE
4734 +-+ " == MCDisassembler_Fail)",
4735 +-+#else
4736 +-+ " == MCDisassembler::Fail)",
4737 +-+#endif
4738 +- "MCDisassembler::Success", "MCDisassembler::Fail", "");
4739 +- }
4740 +-
4741 +-diff --git a/utils/TableGen/FixedLenDecoderEmitter.cpp b/utils/TableGen/FixedLenDecoderEmitter.cpp
4742 +-index fcecc764d44..36845d960d8 100644
4743 +---- a/utils/TableGen/FixedLenDecoderEmitter.cpp
4744 +-+++ b/utils/TableGen/FixedLenDecoderEmitter.cpp
4745 +-@@ -730,7 +730,13 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
4746 +- ++I;
4747 +- unsigned Start = *I++;
4748 +- unsigned Len = *I++;
4749 +-- OS.indent(Indentation) << "MCD::OPC_ExtractField, " << Start << ", "
4750 +-+ OS.indent(Indentation)
4751 +-+#ifdef CAPSTONE
4752 +-+ << "MCD_OPC_ExtractField"
4753 +-+#else
4754 +-+ << "MCD::OPC_ExtractField"
4755 +-+#endif
4756 +-+ << ", " << Start << ", "
4757 +- << Len << ", // Inst{";
4758 +- if (Len > 1)
4759 +- OS << (Start + Len - 1) << "-";
4760 +-@@ -739,7 +745,13 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
4761 +- }
4762 +- case MCD::OPC_FilterValue: {
4763 +- ++I;
4764 +-- OS.indent(Indentation) << "MCD::OPC_FilterValue, ";
4765 +-+ OS.indent(Indentation)
4766 +-+#ifdef CAPSTONE
4767 +-+ << "MCD_OPC_FilterValue"
4768 +-+#else
4769 +-+ << "MCD::OPC_FilterValue"
4770 +-+#endif
4771 +-+ << ", ";
4772 +- // The filter value is ULEB128 encoded.
4773 +- while (*I >= 128)
4774 +- OS << (unsigned)*I++ << ", ";
4775 +-@@ -759,7 +771,13 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
4776 +- ++I;
4777 +- unsigned Start = *I++;
4778 +- unsigned Len = *I++;
4779 +-- OS.indent(Indentation) << "MCD::OPC_CheckField, " << Start << ", "
4780 +-+ OS.indent(Indentation)
4781 +-+#ifdef CAPSTONE
4782 +-+ << "MCD_OPC_CheckField"
4783 +-+#else
4784 +-+ << "MCD::OPC_CheckField"
4785 +-+#endif
4786 +-+ << ", " << Start << ", "
4787 +- << Len << ", ";// << Val << ", " << NumToSkip << ",\n";
4788 +- // ULEB128 encoded field value.
4789 +- for (; *I >= 128; ++I)
4790 +-@@ -777,7 +795,13 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
4791 +- }
4792 +- case MCD::OPC_CheckPredicate: {
4793 +- ++I;
4794 +-- OS.indent(Indentation) << "MCD::OPC_CheckPredicate, ";
4795 +-+ OS.indent(Indentation)
4796 +-+#ifdef CAPSTONE
4797 +-+ << "MCD_OPC_CheckPredicate"
4798 +-+#else
4799 +-+ << "MCD::OPC_CheckPredicate"
4800 +-+#endif
4801 +-+ << ", ";
4802 +- for (; *I >= 128; ++I)
4803 +- OS << (unsigned)*I << ", ";
4804 +- OS << (unsigned)*I++ << ", ";
4805 +-@@ -803,7 +827,13 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
4806 +- && "ULEB128 value too large!");
4807 +- // Decode the Opcode value.
4808 +- unsigned Opc = decodeULEB128(Buffer);
4809 +-- OS.indent(Indentation) << "MCD::OPC_" << (IsTry ? "Try" : "")
4810 +-+ OS.indent(Indentation)
4811 +-+#ifdef CAPSTONE
4812 +-+ << "MCD_OPC_"
4813 +-+#else
4814 +-+ << "MCD::OPC_"
4815 +-+#endif
4816 +-+ << (IsTry ? "Try" : "")
4817 +- << "Decode, ";
4818 +- for (p = Buffer; *p >= 128; ++p)
4819 +- OS << (unsigned)*p << ", ";
4820 +-@@ -837,7 +867,12 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
4821 +- }
4822 +- case MCD::OPC_SoftFail: {
4823 +- ++I;
4824 +-- OS.indent(Indentation) << "MCD::OPC_SoftFail";
4825 +-+ OS.indent(Indentation)
4826 +-+#ifdef CAPSTONE
4827 +-+ << "MCD_OPC_SoftFail";
4828 +-+#else
4829 +-+ << "MCD::OPC_SoftFail";
4830 +-+#endif
4831 +- // Positive mask
4832 +- uint64_t Value = 0;
4833 +- unsigned Shift = 0;
4834 +-@@ -869,7 +904,13 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
4835 +- }
4836 +- case MCD::OPC_Fail: {
4837 +- ++I;
4838 +-- OS.indent(Indentation) << "MCD::OPC_Fail,\n";
4839 +-+ OS.indent(Indentation)
4840 +-+#ifdef CAPSTONE
4841 +-+ << "MCD_OPC_Fail"
4842 +-+#else
4843 +-+ << "MCD::OPC_Fail"
4844 +-+#endif
4845 +-+ << ",\n";
4846 +- break;
4847 +- }
4848 +- }
4849 +-@@ -884,23 +925,46 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
4850 +- void FixedLenDecoderEmitter::
4851 +- emitPredicateFunction(formatted_raw_ostream &OS, PredicateSet &Predicates,
4852 +- unsigned Indentation) const {
4853 +-+#ifdef CAPSTONE
4854 +-+ OS.indent(Indentation) << "static bool getbool(uint64_t b)\n";
4855 +-+ OS.indent(Indentation) << "{\n";
4856 +-+ OS.indent(Indentation) << "\treturn b != 0;\n";
4857 +-+ OS.indent(Indentation) << "}\n\n";
4858 +-+#endif
4859 +-+
4860 +- // The predicate function is just a big switch statement based on the
4861 +- // input predicate index.
4862 +- OS.indent(Indentation) << "static bool checkDecoderPredicate(unsigned Idx, "
4863 +-+#ifdef CAPSTONE
4864 +-+ << "uint64_t Bits)\n{\n";
4865 +-+#else
4866 +- << "const FeatureBitset& Bits) {\n";
4867 +-+#endif
4868 +- Indentation += 2;
4869 +- if (!Predicates.empty()) {
4870 +- OS.indent(Indentation) << "switch (Idx) {\n";
4871 +-- OS.indent(Indentation) << "default: llvm_unreachable(\"Invalid index!\");\n";
4872 +-+ OS.indent(Indentation) << "default: "
4873 +-+#ifdef CAPSTONE
4874 +-+ << "// "
4875 +-+#endif
4876 +-+ << "llvm_unreachable(\"Invalid index!\");\n";
4877 +- unsigned Index = 0;
4878 +- for (const auto &Predicate : Predicates) {
4879 +- OS.indent(Indentation) << "case " << Index++ << ":\n";
4880 +-- OS.indent(Indentation+2) << "return (" << Predicate << ");\n";
4881 +-+ OS.indent(Indentation+2) << "return "
4882 +-+#ifdef CAPSTONE
4883 +-+ << "getbool"
4884 +-+#endif
4885 +-+ << "(" << Predicate << ");\n";
4886 +- }
4887 +- OS.indent(Indentation) << "}\n";
4888 +- } else {
4889 +- // No case statement to emit
4890 +-- OS.indent(Indentation) << "llvm_unreachable(\"Invalid index!\");\n";
4891 +-+ OS.indent(Indentation)
4892 +-+#ifdef CAPSTONE
4893 +-+ << "// "
4894 +-+#endif
4895 +-+ << "llvm_unreachable(\"Invalid index!\");\n";
4896 +- }
4897 +- Indentation -= 2;
4898 +- OS.indent(Indentation) << "}\n\n";
4899 +-@@ -911,23 +975,39 @@ emitDecoderFunction(formatted_raw_ostream &OS, DecoderSet &Decoders,
4900 +- unsigned Indentation) const {
4901 +- // The decoder function is just a big switch statement based on the
4902 +- // input decoder index.
4903 +-+#ifdef CAPSTONE
4904 +-+#define EDF_EOL " \\\n"
4905 +-+ OS.indent(Indentation) << "#define DecodeToMCInst(fname,fieldname, InsnType) \\\n";
4906 +-+ OS.indent(Indentation) << "static DecodeStatus fname(DecodeStatus S, unsigned Idx, InsnType insn, MCInst *MI, \\\n";
4907 +-+ OS.indent(Indentation) << " uint64_t Address, const void *Decoder) \\\n";
4908 +-+ OS.indent(Indentation) << "{ \\\n";
4909 +-+#else
4910 +-+#define EDF_EOL "\n"
4911 +- OS.indent(Indentation) << "template<typename InsnType>\n";
4912 +- OS.indent(Indentation) << "static DecodeStatus decodeToMCInst(DecodeStatus S,"
4913 +- << " unsigned Idx, InsnType insn, MCInst &MI,\n";
4914 +- OS.indent(Indentation) << " uint64_t "
4915 +- << "Address, const void *Decoder, bool &DecodeComplete) {\n";
4916 +-+#endif
4917 +- Indentation += 2;
4918 +-+#ifndef CAPSTONE
4919 +- OS.indent(Indentation) << "DecodeComplete = true;\n";
4920 +-- OS.indent(Indentation) << "InsnType tmp;\n";
4921 +-- OS.indent(Indentation) << "switch (Idx) {\n";
4922 +-- OS.indent(Indentation) << "default: llvm_unreachable(\"Invalid index!\");\n";
4923 +-+#endif
4924 +-+ OS.indent(Indentation) << "InsnType tmp;" EDF_EOL;
4925 +-+ OS.indent(Indentation) << "switch (Idx) {" EDF_EOL;
4926 +-+ OS.indent(Indentation) << "default:"
4927 +-+#ifndef CAPSTONE
4928 +-+ << " llvm_unreachable(\"Invalid index!\");\n";
4929 +-+#else
4930 +-+ << " \\\n";
4931 +-+#endif
4932 +- unsigned Index = 0;
4933 +- for (const auto &Decoder : Decoders) {
4934 +-- OS.indent(Indentation) << "case " << Index++ << ":\n";
4935 +-+ OS.indent(Indentation) << "case " << Index++ << ":" EDF_EOL;
4936 +- OS << Decoder;
4937 +-- OS.indent(Indentation+2) << "return S;\n";
4938 +-+ OS.indent(Indentation+2) << "return S;" EDF_EOL;
4939 +- }
4940 +-- OS.indent(Indentation) << "}\n";
4941 +-+ OS.indent(Indentation) << "}" EDF_EOL;
4942 +- Indentation -= 2;
4943 +- OS.indent(Indentation) << "}\n\n";
4944 +- }
4945 +-@@ -1054,16 +1134,21 @@ void FilterChooser::emitBinaryParser(raw_ostream &o, unsigned &Indentation,
4946 +- const std::string &Decoder = OpInfo.Decoder;
4947 +-
4948 +- if (OpInfo.numFields() != 1)
4949 +-- o.indent(Indentation) << "tmp = 0;\n";
4950 +-+ o.indent(Indentation) << "tmp = 0;" EDF_EOL;
4951 +-
4952 +- for (const EncodingField &EF : OpInfo) {
4953 +- o.indent(Indentation) << "tmp ";
4954 +- if (OpInfo.numFields() != 1) o << '|';
4955 +-- o << "= fieldFromInstruction"
4956 +-+ o << "= "
4957 +-+#ifdef CAPSTONE
4958 +-+ << "fieldname"
4959 +-+#else
4960 +-+ << "fieldFromInstruction"
4961 +-+#endif
4962 +- << "(insn, " << EF.Base << ", " << EF.Width << ')';
4963 +- if (OpInfo.numFields() != 1 || EF.Offset != 0)
4964 +- o << " << " << EF.Offset;
4965 +-- o << ";\n";
4966 +-+ o << ";" EDF_EOL;
4967 +- }
4968 +-
4969 +- if (Decoder != "") {
4970 +-@@ -1071,8 +1156,12 @@ void FilterChooser::emitBinaryParser(raw_ostream &o, unsigned &Indentation,
4971 +- o.indent(Indentation) << Emitter->GuardPrefix << Decoder
4972 +- << "(MI, tmp, Address, Decoder)"
4973 +- << Emitter->GuardPostfix
4974 +-+#ifdef CAPSTONE
4975 +-+ << " return MCDisassembler_Fail; \\\n";
4976 +-+#else
4977 +- << " { " << (OpHasCompleteDecoder ? "" : "DecodeComplete = false; ")
4978 +- << "return MCDisassembler::Fail; }\n";
4979 +-+#endif
4980 +- } else {
4981 +- OpHasCompleteDecoder = true;
4982 +- o.indent(Indentation) << "MI.addOperand(MCOperand::createImm(tmp));\n";
4983 +-@@ -1091,7 +1180,13 @@ void FilterChooser::emitDecoder(raw_ostream &OS, unsigned Indentation,
4984 +- << "(MI, insn, Address, Decoder)"
4985 +- << Emitter->GuardPostfix
4986 +- << " { " << (HasCompleteDecoder ? "" : "DecodeComplete = false; ")
4987 +-- << "return MCDisassembler::Fail; }\n";
4988 +-+ << "return "
4989 +-+#ifdef CAPSTONE
4990 +-+ << "MCDisassembler_Fail"
4991 +-+#else
4992 +-+ << "MCDisassembler::Fail"
4993 +-+#endif
4994 +-+ << "; }\n";
4995 +- break;
4996 +- }
4997 +-
4998 +-@@ -1129,10 +1224,19 @@ unsigned FilterChooser::getDecoderIndex(DecoderSet &Decoders,
4999 +- static void emitSinglePredicateMatch(raw_ostream &o, StringRef str,
5000 +- const std::string &PredicateNamespace) {
5001 +- if (str[0] == '!')
5002 +-+#ifdef CAPSTONE
5003 +-+ o << "~(Bits & " << PredicateNamespace << "_"
5004 +-+ << str.slice(1,str.size()) << ")";
5005 +-+#else
5006 +- o << "!Bits[" << PredicateNamespace << "::"
5007 +- << str.slice(1,str.size()) << "]";
5008 +-+#endif
5009 +- else
5010 +-+#ifdef CAPSTONE
5011 +-+ o << "(Bits & " << PredicateNamespace << "_" << str << ")";
5012 +-+#else
5013 +- o << "Bits[" << PredicateNamespace << "::" << str << "]";
5014 +-+#endif
5015 +- }
5016 +-
5017 +- bool FilterChooser::emitPredicateMatch(raw_ostream &o, unsigned &Indentation,
5018 +-@@ -2047,6 +2151,17 @@ static bool populateInstruction(CodeGenTarget &Target,
5019 +- // fieldFromInstruction().
5020 +- static void emitFieldFromInstruction(formatted_raw_ostream &OS) {
5021 +- OS << "// Helper function for extracting fields from encoded instructions.\n"
5022 +-+#ifdef CAPSTONE
5023 +-+ << "#define FieldFromInstruction(fname, InsnType) \\\n"
5024 +-+ << "static InsnType fname(InsnType insn, unsigned startBit, unsigned numBits) \\\n"
5025 +-+ << "{ \\\n"
5026 +-+ << " InsnType fieldMask; \\\n"
5027 +-+ << " if (numBits == sizeof(InsnType)*8) \\\n"
5028 +-+ << " fieldMask = (InsnType)(-1LL); \\\n"
5029 +-+ << " else \\\n"
5030 +-+ << " fieldMask = (((InsnType)1 << numBits) - 1) << startBit; \\\n"
5031 +-+ << " return (insn & fieldMask) >> startBit; \\\n"
5032 +-+#else
5033 +- << "template<typename InsnType>\n"
5034 +- << "static InsnType fieldFromInstruction(InsnType insn, unsigned startBit,\n"
5035 +- << " unsigned numBits) {\n"
5036 +-@@ -2058,12 +2173,92 @@ static void emitFieldFromInstruction(formatted_raw_ostream &OS) {
5037 +- << " else\n"
5038 +- << " fieldMask = (((InsnType)1 << numBits) - 1) << startBit;\n"
5039 +- << " return (insn & fieldMask) >> startBit;\n"
5040 +-+#endif
5041 +- << "}\n\n";
5042 +- }
5043 +-
5044 +- // emitDecodeInstruction - Emit the templated helper function
5045 +- // decodeInstruction().
5046 +- static void emitDecodeInstruction(formatted_raw_ostream &OS) {
5047 +-+#ifdef CAPSTONE
5048 +-+ OS << "#define DecodeInstruction(fname, fieldname, decoder, InsnType) \\\n"
5049 +-+ << "static DecodeStatus fname(const uint8_t DecodeTable[], MCInst *MI, \\\n"
5050 +-+ << " InsnType insn, uint64_t Address, const MCRegisterInfo *MRI, int feature) \\\n"
5051 +-+ << "{ \\\n"
5052 +-+ << " uint64_t Bits = getFeatureBits(feature); \\\n"
5053 +-+ << " const uint8_t *Ptr = DecodeTable; \\\n"
5054 +-+ << " uint32_t CurFieldValue = 0, ExpectedValue; \\\n"
5055 +-+ << " DecodeStatus S = MCDisassembler_Success; \\\n"
5056 +-+ << " unsigned Start, Len, NumToSkip, PIdx, Opc, DecodeIdx; \\\n"
5057 +-+ << " InsnType Val, FieldValue, PositiveMask, NegativeMask; \\\n"
5058 +-+ << " bool Pred, Fail; \\\n"
5059 +-+ << " for (;;) { \\\n"
5060 +-+ << " switch (*Ptr) { \\\n"
5061 +-+ << " default: \\\n"
5062 +-+ << " return MCDisassembler_Fail; \\\n"
5063 +-+ << " case MCD_OPC_ExtractField: { \\\n"
5064 +-+ << " Start = *++Ptr; \\\n"
5065 +-+ << " Len = *++Ptr; \\\n"
5066 +-+ << " ++Ptr; \\\n"
5067 +-+ << " CurFieldValue = (uint32_t)fieldname(insn, Start, Len); \\\n"
5068 +-+ << " break; \\\n"
5069 +-+ << " } \\\n"
5070 +-+ << " case MCD_OPC_FilterValue: { \\\n"
5071 +-+ << " Val = (InsnType)decodeULEB128(++Ptr, &Len); \\\n"
5072 +-+ << " Ptr += Len; \\\n"
5073 +-+ << " NumToSkip = *Ptr++; \\\n"
5074 +-+ << " NumToSkip |= (*Ptr++) << 8; \\\n"
5075 +-+ << " if (Val != CurFieldValue) \\\n"
5076 +-+ << " Ptr += NumToSkip; \\\n"
5077 +-+ << " break; \\\n"
5078 +-+ << " } \\\n"
5079 +-+ << " case MCD_OPC_CheckField: { \\\n"
5080 +-+ << " Start = *++Ptr; \\\n"
5081 +-+ << " Len = *++Ptr; \\\n"
5082 +-+ << " FieldValue = fieldname(insn, Start, Len); \\\n"
5083 +-+ << " ExpectedValue = (uint32_t)decodeULEB128(++Ptr, &Len); \\\n"
5084 +-+ << " Ptr += Len; \\\n"
5085 +-+ << " NumToSkip = *Ptr++; \\\n"
5086 +-+ << " NumToSkip |= (*Ptr++) << 8; \\\n"
5087 +-+ << " if (ExpectedValue != FieldValue) \\\n"
5088 +-+ << " Ptr += NumToSkip; \\\n"
5089 +-+ << " break; \\\n"
5090 +-+ << " } \\\n"
5091 +-+ << " case MCD_OPC_CheckPredicate: { \\\n"
5092 +-+ << " PIdx = (uint32_t)decodeULEB128(++Ptr, &Len); \\\n"
5093 +-+ << " Ptr += Len; \\\n"
5094 +-+ << " NumToSkip = *Ptr++; \\\n"
5095 +-+ << " NumToSkip |= (*Ptr++) << 8; \\\n"
5096 +-+ << " Pred = checkDecoderPredicate(PIdx, Bits); \\\n"
5097 +-+ << " if (!Pred) \\\n"
5098 +-+ << " Ptr += NumToSkip; \\\n"
5099 +-+ << " (void)Pred; \\\n"
5100 +-+ << " break; \\\n"
5101 +-+ << " } \\\n"
5102 +-+ << " case MCD_OPC_Decode: { \\\n"
5103 +-+ << " Opc = (unsigned)decodeULEB128(++Ptr, &Len); \\\n"
5104 +-+ << " Ptr += Len; \\\n"
5105 +-+ << " DecodeIdx = (unsigned)decodeULEB128(Ptr, &Len); \\\n"
5106 +-+ << " Ptr += Len; \\\n"
5107 +-+ << " MCInst_setOpcode(MI, Opc); \\\n"
5108 +-+ << " return decoder(S, DecodeIdx, insn, MI, Address, MRI); \\\n"
5109 +-+ << " } \\\n"
5110 +-+ << " case MCD_OPC_SoftFail: { \\\n"
5111 +-+ << " PositiveMask = (InsnType)decodeULEB128(++Ptr, &Len); \\\n"
5112 +-+ << " Ptr += Len; \\\n"
5113 +-+ << " NegativeMask = (InsnType)decodeULEB128(Ptr, &Len); \\\n"
5114 +-+ << " Ptr += Len; \\\n"
5115 +-+ << " Fail = (insn & PositiveMask) || (~insn & NegativeMask); \\\n"
5116 +-+ << " if (Fail) \\\n"
5117 +-+ << " S = MCDisassembler_SoftFail; \\\n"
5118 +-+ << " break; \\\n"
5119 +-+ << " } \\\n"
5120 +-+ << " case MCD_OPC_Fail: { \\\n"
5121 +-+ << " return MCDisassembler_Fail; \\\n"
5122 +-+ << " } \\\n"
5123 +-+ << " } \\\n"
5124 +-+ << " } \\\n"
5125 +-+#else
5126 +- OS << "template<typename InsnType>\n"
5127 +- << "static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], "
5128 +- "MCInst &MI,\n"
5129 +-@@ -2240,12 +2435,18 @@ static void emitDecodeInstruction(formatted_raw_ostream &OS) {
5130 +- << " }\n"
5131 +- << " llvm_unreachable(\"bogosity detected in disassembler state "
5132 +- "machine!\");\n"
5133 +-+#endif
5134 +- << "}\n\n";
5135 +- }
5136 +-
5137 +- // Emits disassembler code for instruction decoding.
5138 +- void FixedLenDecoderEmitter::run(raw_ostream &o) {
5139 +- formatted_raw_ostream OS(o);
5140 +-+#ifdef CAPSTONE
5141 +-+ OS << "#include \"../../MCInst.h\"\n";
5142 +-+ OS << "#include \"../../LEB128.h\"\n";
5143 +-+ OS << "\n";
5144 +-+#else
5145 +- OS << "#include \"llvm/MC/MCInst.h\"\n";
5146 +- OS << "#include \"llvm/Support/Debug.h\"\n";
5147 +- OS << "#include \"llvm/Support/DataTypes.h\"\n";
5148 +-@@ -2254,6 +2455,7 @@ void FixedLenDecoderEmitter::run(raw_ostream &o) {
5149 +- OS << "#include <assert.h>\n";
5150 +- OS << '\n';
5151 +- OS << "namespace llvm {\n\n";
5152 +-+#endif
5153 +-
5154 +- emitFieldFromInstruction(OS);
5155 +-
5156 +-@@ -2322,7 +2524,13 @@ void FixedLenDecoderEmitter::run(raw_ostream &o) {
5157 +- // Emit the main entry point for the decoder, decodeInstruction().
5158 +- emitDecodeInstruction(OS);
5159 +-
5160 +-+#ifdef CAPSTONE
5161 +-+ OS << "FieldFromInstruction(fieldFromInstruction, uint64_t)\n";
5162 +-+ OS << "DecodeToMCInst(decodeToMCInst, fieldFromInstruction, uint64_t)\n";
5163 +-+ OS << "DecodeInstruction(decodeInstruction, fieldFromInstruction, decodeToMCInst, uint64_t)\n";
5164 +-+#else
5165 +- OS << "\n} // End llvm namespace\n";
5166 +-+#endif
5167 +- }
5168 +-
5169 +- namespace llvm {
5170 +---
5171 +-2.19.1
5172 +-
5173 +diff --git a/llvm/0005-capstone-generate-GenAsmWriter.inc.patch b/llvm/0005-capstone-generate-GenAsmWriter.inc.patch
5174 +deleted file mode 100644
5175 +index cd1353eb7..000000000
5176 +--- a/llvm/0005-capstone-generate-GenAsmWriter.inc.patch
5177 ++++ /dev/null
5178 +@@ -1,225 +0,0 @@
5179 +-From 5569e48b9cb34a33910e1e850fbfabc999f016a2 Mon Sep 17 00:00:00 2001
5180 +-From: mephi42 <[email protected]>
5181 +-Date: Tue, 7 Aug 2018 20:00:08 +0200
5182 +-Subject: [PATCH 5/7] capstone: generate *GenAsmWriter.inc
5183 +-
5184 +----
5185 +- utils/TableGen/AsmWriterEmitter.cpp | 89 +++++++++++++++++++++++++++--
5186 +- utils/TableGen/AsmWriterInst.cpp | 4 ++
5187 +- 2 files changed, 87 insertions(+), 6 deletions(-)
5188 +-
5189 +-diff --git a/utils/TableGen/AsmWriterEmitter.cpp b/utils/TableGen/AsmWriterEmitter.cpp
5190 +-index 3c4c9c8e5c6..133800d217c 100644
5191 +---- a/utils/TableGen/AsmWriterEmitter.cpp
5192 +-+++ b/utils/TableGen/AsmWriterEmitter.cpp
5193 +-@@ -272,16 +272,22 @@ static void UnescapeString(std::string &Str) {
5194 +- /// clearing the Instructions vector.
5195 +- void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
5196 +- Record *AsmWriter = Target.getAsmWriter();
5197 +-+#ifndef CAPSTONE
5198 +- StringRef ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
5199 +-+#endif
5200 +- bool PassSubtarget = AsmWriter->getValueAsInt("PassSubtarget");
5201 +-
5202 +- O <<
5203 +- "/// printInstruction - This method is automatically generated by tablegen\n"
5204 +- "/// from the instruction set description.\n"
5205 +-+#ifdef CAPSTONE
5206 +-+ "static void printInstruction(MCInst *MI, SStream *O, MCRegisterInfo *MRI)\n{\n";
5207 +-+#else
5208 +- "void " << Target.getName() << ClassName
5209 +- << "::printInstruction(const MCInst *MI, "
5210 +- << (PassSubtarget ? "const MCSubtargetInfo &STI, " : "")
5211 +- << "raw_ostream &O) {\n";
5212 +-+#endif
5213 +-
5214 +- // Build an aggregate string, and build a table of offsets into it.
5215 +- SequenceToOffsetTable<std::string> StringTable;
5216 +-@@ -379,9 +385,16 @@ void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
5217 +- }
5218 +-
5219 +- // Emit the string table itself.
5220 +-+#ifdef CAPSTONE
5221 +-+ O << "#ifndef CAPSTONE_DIET\n";
5222 +-+#endif
5223 +- O << " static const char AsmStrs[] = {\n";
5224 +- StringTable.emit(O, printChar);
5225 +-- O << " };\n\n";
5226 +-+ O << " };\n"
5227 +-+#ifdef CAPSTONE
5228 +-+ << "#endif\n"
5229 +-+#endif
5230 +-+ << "\n";
5231 +-
5232 +- // Emit the lookup tables in pieces to minimize wasted bytes.
5233 +- unsigned BytesNeeded = ((OpcodeInfoBits - BitsLeft) + 7) / 8;
5234 +-@@ -409,21 +422,45 @@ void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
5235 +- // If the total bits is more than 32-bits we need to use a 64-bit type.
5236 +- if (BitsLeft < (OpcodeInfoBits - 32))
5237 +- BitsOS << "(uint64_t)";
5238 +-- BitsOS << "OpInfo" << Table << "[MI->getOpcode()] << " << Shift << ";\n";
5239 +-+ BitsOS << "OpInfo" << Table << "["
5240 +-+#ifdef CAPSTONE
5241 +-+ << "MCInst_getOpcode(MI)"
5242 +-+#else
5243 +-+ << "MI->getOpcode()"
5244 +-+#endif
5245 +-+ << "] << " << Shift << ";\n";
5246 +- // Prepare the shift for the next iteration and increment the table count.
5247 +- Shift += TableSize;
5248 +- ++Table;
5249 +- }
5250 +-
5251 +- // Emit the initial tab character.
5252 +-+#ifndef CAPSTONE
5253 +- O << " O << \"\\t\";\n\n";
5254 +-+#endif
5255 +-
5256 +- O << " // Emit the opcode for the instruction.\n";
5257 +- O << BitsString;
5258 +-
5259 +- // Emit the starting string.
5260 +-- O << " assert(Bits != 0 && \"Cannot print this instruction.\");\n"
5261 +-- << " O << AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << ")-1;\n\n";
5262 +-+ O << " "
5263 +-+#ifdef CAPSTONE
5264 +-+ << "// "
5265 +-+#endif
5266 +-+ << "assert(Bits != 0 && \"Cannot print this instruction.\");\n"
5267 +-+#ifdef CAPSTONE
5268 +-+ << "#ifndef CAPSTONE_DIET\n"
5269 +-+ << " SStream_concat0(O, "
5270 +-+#else
5271 +-+ << " O << "
5272 +-+#endif
5273 +-+ << "AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << ")-1"
5274 +-+#ifdef CAPSTONE
5275 +-+ << ");\n"
5276 +-+ << "#endif\n\n";
5277 +-+#else
5278 +-+ << ");\n\n";
5279 +-+#endif
5280 +-
5281 +- // Output the table driven operand information.
5282 +- BitsLeft = OpcodeInfoBits-AsmStrBits;
5283 +-@@ -455,7 +492,11 @@ void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
5284 +- O << " switch ((Bits >> "
5285 +- << (OpcodeInfoBits-BitsLeft) << ") & "
5286 +- << ((1 << NumBits)-1) << ") {\n"
5287 +-- << " default: llvm_unreachable(\"Invalid command number.\");\n";
5288 +-+ << " default: "
5289 +-+#ifdef CAPSTONE
5290 +-+ << "// "
5291 +-+#endif
5292 +-+ << "llvm_unreachable(\"Invalid command number.\");\n";
5293 +-
5294 +- // Print out all the cases.
5295 +- for (unsigned j = 0, e = Commands.size(); j != e; ++j) {
5296 +-@@ -536,6 +577,9 @@ emitRegisterNameString(raw_ostream &O, StringRef AltName,
5297 +- }
5298 +-
5299 +- StringTable.layout();
5300 +-+#ifdef CAPSTONE
5301 +-+ O << "#ifndef CAPSTONE_DIET\n";
5302 +-+#endif
5303 +- O << " static const char AsmStrs" << AltName << "[] = {\n";
5304 +- StringTable.emit(O, printChar);
5305 +- O << " };\n\n";
5306 +-@@ -552,8 +596,10 @@ emitRegisterNameString(raw_ostream &O, StringRef AltName,
5307 +- }
5308 +-
5309 +- void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
5310 +-+#ifndef CAPSTONE
5311 +- Record *AsmWriter = Target.getAsmWriter();
5312 +- StringRef ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
5313 +-+#endif
5314 +- const auto &Registers = Target.getRegBank().getRegisters();
5315 +- const std::vector<Record*> &AltNameIndices = Target.getRegAltNameIndices();
5316 +- bool hasAltNames = AltNameIndices.size() > 1;
5317 +-@@ -563,12 +609,20 @@ void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
5318 +- "\n\n/// getRegisterName - This method is automatically generated by tblgen\n"
5319 +- "/// from the register set description. This returns the assembler name\n"
5320 +- "/// for the specified register.\n"
5321 +-+#ifdef CAPSTONE
5322 +-+ "static const char *getRegisterName(unsigned RegNo)\n{\n";
5323 +-+#else
5324 +- "const char *" << Target.getName() << ClassName << "::";
5325 +- if (hasAltNames)
5326 +- O << "\ngetRegisterName(unsigned RegNo, unsigned AltIdx) {\n";
5327 +- else
5328 +- O << "getRegisterName(unsigned RegNo) {\n";
5329 +-- O << " assert(RegNo && RegNo < " << (Registers.size()+1)
5330 +-+#endif
5331 +-+ O << " "
5332 +-+#ifdef CAPSTONE
5333 +-+ << "// "
5334 +-+#endif
5335 +-+ << "assert(RegNo && RegNo < " << (Registers.size()+1)
5336 +- << " && \"Invalid register number!\");\n"
5337 +- << "\n";
5338 +-
5339 +-@@ -595,10 +649,22 @@ void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
5340 +- }
5341 +- O << " }\n";
5342 +- } else {
5343 +-+#ifdef CAPSTONE
5344 +-+ O << " //int i;\n"
5345 +-+ << " //for (i = 0; i < sizeof(RegAsmOffset); i++)\n"
5346 +-+ << " // printf(\"%s = %u\\n\", AsmStrs+RegAsmOffset[i], i + 1);\n"
5347 +-+ << " //printf(\"*************************\\n\");\n"
5348 +-+#else
5349 +- O << " assert (*(AsmStrs+RegAsmOffset[RegNo-1]) &&\n"
5350 +- << " \"Invalid alt name index for register!\");\n"
5351 +-+#endif
5352 +- << " return AsmStrs+RegAsmOffset[RegNo-1];\n";
5353 +- }
5354 +-+#ifdef CAPSTONE
5355 +-+ O << "#else\n"
5356 +-+ << " return NULL;\n"
5357 +-+ << "#endif\n";
5358 +-+#endif
5359 +- O << "}\n";
5360 +- }
5361 +-
5362 +-@@ -1135,9 +1201,20 @@ AsmWriterEmitter::AsmWriterEmitter(RecordKeeper &R) : Records(R), Target(R) {
5363 +- }
5364 +-
5365 +- void AsmWriterEmitter::run(raw_ostream &O) {
5366 +-+#ifdef CAPSTONE
5367 +-+ O << "/* Capstone Disassembly Engine */\n"
5368 +-+ "/* By Nguyen Anh Quynh <[email protected]>, 2013-2015 */\n"
5369 +-+ "\n"
5370 +-+ "#include <stdio.h>\t// debug\n"
5371 +-+ "#include <capstone/platform.h>\n"
5372 +-+ "\n"
5373 +-+ "\n";
5374 +-+#endif
5375 +- EmitPrintInstruction(O);
5376 +- EmitGetRegisterName(O);
5377 +-+#ifndef CAPSTONE
5378 +- EmitPrintAliasInstruction(O);
5379 +-+#endif
5380 +- }
5381 +-
5382 +- namespace llvm {
5383 +-diff --git a/utils/TableGen/AsmWriterInst.cpp b/utils/TableGen/AsmWriterInst.cpp
5384 +-index 2c19e5d663d..6fa751e50df 100644
5385 +---- a/utils/TableGen/AsmWriterInst.cpp
5386 +-+++ b/utils/TableGen/AsmWriterInst.cpp
5387 +-@@ -28,9 +28,13 @@ static bool isIdentChar(char C) {
5388 +-
5389 +- std::string AsmWriterOperand::getCode(bool PassSubtarget) const {
5390 +- if (OperandType == isLiteralTextOperand) {
5391 +-+#ifdef CAPSTONE
5392 +-+ return "SStream_concat0(O, \"" + Str + "\");";
5393 +-+#else
5394 +- if (Str.size() == 1)
5395 +- return "O << '" + Str + "';";
5396 +- return "O << \"" + Str + "\";";
5397 +-+#endif
5398 +- }
5399 +-
5400 +- if (OperandType == isLiteralStatementOperand)
5401 +---
5402 +-2.19.1
5403 +-
5404 +diff --git a/llvm/0006-capstone-generate-MappingInsn.inc.patch b/llvm/0006-capstone-generate-MappingInsn.inc.patch
5405 +deleted file mode 100644
5406 +index 7ee22d787..000000000
5407 +--- a/llvm/0006-capstone-generate-MappingInsn.inc.patch
5408 ++++ /dev/null
5409 +@@ -1,174 +0,0 @@
5410 +-From 7a436110ef15c803dc8524af2fb5612bcacbb126 Mon Sep 17 00:00:00 2001
5411 +-From: mephi42 <[email protected]>
5412 +-Date: Tue, 7 Aug 2018 20:55:32 +0200
5413 +-Subject: [PATCH 6/7] capstone: generate *MappingInsn.inc
5414 +-
5415 +----
5416 +- lib/Target/SystemZ/CMakeLists.txt | 1 +
5417 +- utils/TableGen/InstrInfoEmitter.cpp | 95 +++++++++++++++++++++++++++++
5418 +- utils/TableGen/TableGen.cpp | 6 ++
5419 +- utils/TableGen/TableGenBackends.h | 1 +
5420 +- 4 files changed, 103 insertions(+)
5421 +-
5422 +-diff --git a/lib/Target/SystemZ/CMakeLists.txt b/lib/Target/SystemZ/CMakeLists.txt
5423 +-index f83b4242fb4..4b5d9c4a3b2 100644
5424 +---- a/lib/Target/SystemZ/CMakeLists.txt
5425 +-+++ b/lib/Target/SystemZ/CMakeLists.txt
5426 +-@@ -6,6 +6,7 @@ tablegen(LLVM SystemZGenCallingConv.inc -gen-callingconv)
5427 +- tablegen(LLVM SystemZGenDAGISel.inc -gen-dag-isel)
5428 +- tablegen(LLVM SystemZGenDisassemblerTables.inc -gen-disassembler)
5429 +- tablegen(LLVM SystemZGenInstrInfo.inc -gen-instr-info)
5430 +-+tablegen(LLVM SystemZMappingInsn.inc -mapping-insn)
5431 +- tablegen(LLVM SystemZGenMCCodeEmitter.inc -gen-emitter)
5432 +- tablegen(LLVM SystemZGenRegisterInfo.inc -gen-register-info)
5433 +- tablegen(LLVM SystemZGenSubtargetInfo.inc -gen-subtarget)
5434 +-diff --git a/utils/TableGen/InstrInfoEmitter.cpp b/utils/TableGen/InstrInfoEmitter.cpp
5435 +-index 2f3a2729262..14ab1ea8a72 100644
5436 +---- a/utils/TableGen/InstrInfoEmitter.cpp
5437 +-+++ b/utils/TableGen/InstrInfoEmitter.cpp
5438 +-@@ -744,4 +744,99 @@ void EmitInstrInfo(RecordKeeper &RK, raw_ostream &OS) {
5439 +- #endif
5440 +- }
5441 +-
5442 +-+#ifdef CAPSTONE
5443 +-+std::string GetPublicName(const CodeGenInstruction *Inst) {
5444 +-+ std::string Name = Inst->TheDef->getName();
5445 +-+ // Apply backward compatibility fixups.
5446 +-+ // BRNLE -> BNLER.
5447 +-+ if (Name.length() >= 5 && Name.substr(0, 5) == "BRAsm") {
5448 +-+ Name = "B" + Name.substr(5, Name.length() - 5) + "R";
5449 +-+ }
5450 +-+ // SSKEOpt -> SSKE.
5451 +-+ while (Name.length() >= 3 && Name.substr(Name.length() - 3, 3) == "Opt") {
5452 +-+ Name = Name.substr(0, Name.length() - 3);
5453 +-+ }
5454 +-+ // BRCLAsm -> BRCL.
5455 +-+ while (true) {
5456 +-+ size_t pos = Name.find("Asm");
5457 +-+ if (pos == std::string::npos) {
5458 +-+ break;
5459 +-+ }
5460 +-+ Name = Name.substr(0, pos) + Name.substr(pos + 3);
5461 +-+ }
5462 +-+ // CPSDRxx -> CPSDR.
5463 +-+ if (Name.length() >= 2) {
5464 +-+ std::string Suffix2 = Name.substr(Name.length() - 2, 2);
5465 +-+ if (Suffix2 == "dd" || Suffix2 == "ds" ||
5466 +-+ Suffix2 == "sd" || Suffix2 == "ss") {
5467 +-+ Name = Name.substr(0, Name.length() - 2);
5468 +-+ }
5469 +-+ }
5470 +-+ return "SYSZ_INS_" + Name;
5471 +-+}
5472 +-+
5473 +-+std::string GetRegisterName(Record *Reg) {
5474 +-+ std::string Name = Reg->getName();
5475 +-+ for (char& c : Name) {
5476 +-+ c = toupper(c);
5477 +-+ }
5478 +-+ // R0L, R0D -> R0.
5479 +-+ if (Name.length() >= 3 &&
5480 +-+ Name[Name.length() - 3] == 'R' &&
5481 +-+ (Name[Name.length() - 1] == 'L' ||
5482 +-+ Name[Name.length() - 1] == 'D')) {
5483 +-+ Name = Name.substr(0, Name.length() - 3) + Name[Name.length() - 2];
5484 +-+ }
5485 +-+ return "SYSZ_REG_" + Name;
5486 +-+}
5487 +-+
5488 +-+std::string GetGroupName(Record *Pred) {
5489 +-+ std::string Name = Pred->getName();
5490 +-+ for (char& c : Name) {
5491 +-+ c = toupper(c);
5492 +-+ }
5493 +-+ if (Name.length() >= 7 && Name.substr(0, 7) == "FEATURE") {
5494 +-+ Name = Name.substr(7);
5495 +-+ }
5496 +-+ return "SYSZ_GRP_" + Name;
5497 +-+}
5498 +-+
5499 +-+void EmitMappingInsn(RecordKeeper &RK, raw_ostream &OS) {
5500 +-+ OS << "// This is auto-gen data for Capstone engine (www.capstone-engine.org)\n"
5501 +-+ "// By Nguyen Anh Quynh <[email protected]>\n"
5502 +-+ "\n";
5503 +-+ CodeGenTarget Target(RK);
5504 +-+ for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
5505 +-+ if (Inst->TheDef->getValueAsBit("isPseudo") ||
5506 +-+ Inst->TheDef->getValueAsBit("isCodeGenOnly")) {
5507 +-+ continue;
5508 +-+ }
5509 +-+ OS << "{\n"
5510 +-+ << "\t" << Target.getName() << "_" << Inst->TheDef->getName() << ", "
5511 +-+ << GetPublicName(Inst) << ",\n"
5512 +-+ << "#ifndef CAPSTONE_DIET\n"
5513 +-+ << "\t{ ";
5514 +-+ for (Record *Use : Inst->TheDef->getValueAsListOfDefs("Uses")) {
5515 +-+ OS << GetRegisterName(Use) << ", ";
5516 +-+ }
5517 +-+ OS << "0 }, { ";
5518 +-+ for (Record *Def : Inst->TheDef->getValueAsListOfDefs("Defs")) {
5519 +-+ OS << GetRegisterName(Def) << ", ";
5520 +-+ }
5521 +-+ OS << "0 }, { ";
5522 +-+ ListInit *Predicates = Inst->TheDef->getValueAsListInit("Predicates");
5523 +-+ for (unsigned i = 0; i < Predicates->size(); ++i) {
5524 +-+ OS << GetGroupName(Predicates->getElementAsRecord(i)) << ", ";
5525 +-+ }
5526 +-+ OS << "0 }, "
5527 +-+ << Inst->TheDef->getValueAsBit("isBranch")
5528 +-+ << ", "
5529 +-+ << Inst->TheDef->getValueAsBit("isIndirectBranch")
5530 +-+ << "\n"
5531 +-+ << "#endif\n"
5532 +-+ << "},\n";
5533 +-+ }
5534 +-+}
5535 +-+#endif
5536 +-+
5537 +- } // end llvm namespace
5538 +-diff --git a/utils/TableGen/TableGen.cpp b/utils/TableGen/TableGen.cpp
5539 +-index cf1404d8769..bbb4e860536 100644
5540 +---- a/utils/TableGen/TableGen.cpp
5541 +-+++ b/utils/TableGen/TableGen.cpp
5542 +-@@ -27,6 +27,7 @@ enum ActionType {
5543 +- GenEmitter,
5544 +- GenRegisterInfo,
5545 +- GenInstrInfo,
5546 +-+ MappingInsn,
5547 +- GenInstrDocs,
5548 +- GenAsmWriter,
5549 +- GenAsmMatcher,
5550 +-@@ -65,6 +66,8 @@ namespace {
5551 +- "Generate registers and register classes info"),
5552 +- clEnumValN(GenInstrInfo, "gen-instr-info",
5553 +- "Generate instruction descriptions"),
5554 +-+ clEnumValN(MappingInsn, "mapping-insn",
5555 +-+ ""),
5556 +- clEnumValN(GenInstrDocs, "gen-instr-docs",
5557 +- "Generate instruction documentation"),
5558 +- clEnumValN(GenCallingConv, "gen-callingconv",
5559 +-@@ -135,6 +138,9 @@ bool LLVMTableGenMain(raw_ostream &OS, RecordKeeper &Records) {
5560 +- case GenInstrInfo:
5561 +- EmitInstrInfo(Records, OS);
5562 +- break;
5563 +-+ case MappingInsn:
5564 +-+ EmitMappingInsn(Records, OS);
5565 +-+ break;
5566 +- case GenInstrDocs:
5567 +- EmitInstrDocs(Records, OS);
5568 +- break;
5569 +-diff --git a/utils/TableGen/TableGenBackends.h b/utils/TableGen/TableGenBackends.h
5570 +-index 1329a6d833f..a41e46b1db0 100644
5571 +---- a/utils/TableGen/TableGenBackends.h
5572 +-+++ b/utils/TableGen/TableGenBackends.h
5573 +-@@ -75,6 +75,7 @@ void EmitDFAPacketizer(RecordKeeper &RK, raw_ostream &OS);
5574 +- void EmitDisassembler(RecordKeeper &RK, raw_ostream &OS);
5575 +- void EmitFastISel(RecordKeeper &RK, raw_ostream &OS);
5576 +- void EmitInstrInfo(RecordKeeper &RK, raw_ostream &OS);
5577 +-+void EmitMappingInsn(RecordKeeper &RK, raw_ostream &OS);
5578 +- void EmitInstrDocs(RecordKeeper &RK, raw_ostream &OS);
5579 +- void EmitPseudoLowering(RecordKeeper &RK, raw_ostream &OS);
5580 +- void EmitCompressInst(RecordKeeper &RK, raw_ostream &OS);
5581 +---
5582 +-2.19.1
5583 +-
5584 +diff --git a/llvm/0007-capstone-generate-GenInsnNameMaps.inc.patch b/llvm/0007-capstone-generate-GenInsnNameMaps.inc.patch
5585 +deleted file mode 100644
5586 +index 019540d65..000000000
5587 +--- a/llvm/0007-capstone-generate-GenInsnNameMaps.inc.patch
5588 ++++ /dev/null
5589 +@@ -1,110 +0,0 @@
5590 +-From b42f9f2014ec49a22077b6610863d9341a74e142 Mon Sep 17 00:00:00 2001
5591 +-From: mephi42 <[email protected]>
5592 +-Date: Fri, 17 Aug 2018 11:07:39 +0200
5593 +-Subject: [PATCH 7/7] capstone: generate *GenInsnNameMaps.inc
5594 +-
5595 +----
5596 +- lib/Target/SystemZ/CMakeLists.txt | 1 +
5597 +- utils/TableGen/InstrInfoEmitter.cpp | 29 +++++++++++++++++++++++++++++
5598 +- utils/TableGen/TableGen.cpp | 6 ++++++
5599 +- utils/TableGen/TableGenBackends.h | 1 +
5600 +- 4 files changed, 37 insertions(+)
5601 +-
5602 +-diff --git a/lib/Target/SystemZ/CMakeLists.txt b/lib/Target/SystemZ/CMakeLists.txt
5603 +-index 4b5d9c4a3b2..2c64e0a94b8 100644
5604 +---- a/lib/Target/SystemZ/CMakeLists.txt
5605 +-+++ b/lib/Target/SystemZ/CMakeLists.txt
5606 +-@@ -7,6 +7,7 @@ tablegen(LLVM SystemZGenDAGISel.inc -gen-dag-isel)
5607 +- tablegen(LLVM SystemZGenDisassemblerTables.inc -gen-disassembler)
5608 +- tablegen(LLVM SystemZGenInstrInfo.inc -gen-instr-info)
5609 +- tablegen(LLVM SystemZMappingInsn.inc -mapping-insn)
5610 +-+tablegen(LLVM SystemZGenInsnNameMaps.inc -gen-insn-name-maps)
5611 +- tablegen(LLVM SystemZGenMCCodeEmitter.inc -gen-emitter)
5612 +- tablegen(LLVM SystemZGenRegisterInfo.inc -gen-register-info)
5613 +- tablegen(LLVM SystemZGenSubtargetInfo.inc -gen-subtarget)
5614 +-diff --git a/utils/TableGen/InstrInfoEmitter.cpp b/utils/TableGen/InstrInfoEmitter.cpp
5615 +-index 14ab1ea8a72..ccf8170ca62 100644
5616 +---- a/utils/TableGen/InstrInfoEmitter.cpp
5617 +-+++ b/utils/TableGen/InstrInfoEmitter.cpp
5618 +-@@ -837,6 +837,35 @@ void EmitMappingInsn(RecordKeeper &RK, raw_ostream &OS) {
5619 +- << "},\n";
5620 +- }
5621 +- }
5622 +-+
5623 +-+std::string GetMnemonic(const CodeGenInstruction *Inst) {
5624 +-+ std::string Mnemonic = Inst->AsmString;
5625 +-+
5626 +-+ for (size_t i = 0; i < Mnemonic.length(); i++) {
5627 +-+ if (Mnemonic[i] == '\t') {
5628 +-+ return Mnemonic.substr(0, i);
5629 +-+ }
5630 +-+ }
5631 +-+ return Mnemonic;
5632 +-+}
5633 +-+
5634 +-+void EmitInsnNameMaps(RecordKeeper &RK, raw_ostream &OS) {
5635 +-+ OS << "// This is auto-gen data for Capstone engine (www.capstone-engine.org)\n"
5636 +-+ "// By Nguyen Anh Quynh <[email protected]>\n"
5637 +-+ "\n";
5638 +-+ CodeGenTarget Target(RK);
5639 +-+ std::map<std::string, std::string> M;
5640 +-+ for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
5641 +-+ if (Inst->TheDef->getValueAsBit("isPseudo") ||
5642 +-+ Inst->TheDef->getValueAsBit("isCodeGenOnly")) {
5643 +-+ continue;
5644 +-+ }
5645 +-+ M[GetPublicName(Inst)] = GetMnemonic(Inst);
5646 +-+ }
5647 +-+ for (auto &P : M) {
5648 +-+ OS << "\t{ " << P.first << ", \"" << P.second << "\" },\n";
5649 +-+ }
5650 +-+}
5651 +- #endif
5652 +-
5653 +- } // end llvm namespace
5654 +-diff --git a/utils/TableGen/TableGen.cpp b/utils/TableGen/TableGen.cpp
5655 +-index bbb4e860536..27c6603de5a 100644
5656 +---- a/utils/TableGen/TableGen.cpp
5657 +-+++ b/utils/TableGen/TableGen.cpp
5658 +-@@ -28,6 +28,7 @@ enum ActionType {
5659 +- GenRegisterInfo,
5660 +- GenInstrInfo,
5661 +- MappingInsn,
5662 +-+ GenInsnNameMaps,
5663 +- GenInstrDocs,
5664 +- GenAsmWriter,
5665 +- GenAsmMatcher,
5666 +-@@ -68,6 +69,8 @@ namespace {
5667 +- "Generate instruction descriptions"),
5668 +- clEnumValN(MappingInsn, "mapping-insn",
5669 +- ""),
5670 +-+ clEnumValN(GenInsnNameMaps, "gen-insn-name-maps",
5671 +-+ ""),
5672 +- clEnumValN(GenInstrDocs, "gen-instr-docs",
5673 +- "Generate instruction documentation"),
5674 +- clEnumValN(GenCallingConv, "gen-callingconv",
5675 +-@@ -141,6 +144,9 @@ bool LLVMTableGenMain(raw_ostream &OS, RecordKeeper &Records) {
5676 +- case MappingInsn:
5677 +- EmitMappingInsn(Records, OS);
5678 +- break;
5679 +-+ case GenInsnNameMaps:
5680 +-+ EmitInsnNameMaps(Records, OS);
5681 +-+ break;
5682 +- case GenInstrDocs:
5683 +- EmitInstrDocs(Records, OS);
5684 +- break;
5685 +-diff --git a/utils/TableGen/TableGenBackends.h b/utils/TableGen/TableGenBackends.h
5686 +-index a41e46b1db0..5656e5be849 100644
5687 +---- a/utils/TableGen/TableGenBackends.h
5688 +-+++ b/utils/TableGen/TableGenBackends.h
5689 +-@@ -76,6 +76,7 @@ void EmitDisassembler(RecordKeeper &RK, raw_ostream &OS);
5690 +- void EmitFastISel(RecordKeeper &RK, raw_ostream &OS);
5691 +- void EmitInstrInfo(RecordKeeper &RK, raw_ostream &OS);
5692 +- void EmitMappingInsn(RecordKeeper &RK, raw_ostream &OS);
5693 +-+void EmitInsnNameMaps(RecordKeeper &RK, raw_ostream &OS);
5694 +- void EmitInstrDocs(RecordKeeper &RK, raw_ostream &OS);
5695 +- void EmitPseudoLowering(RecordKeeper &RK, raw_ostream &OS);
5696 +- void EmitCompressInst(RecordKeeper &RK, raw_ostream &OS);
5697 +---
5698 +-2.19.1
5699 +-
5700 +--
5701 +2.20.1
5702 +
5703 diff --git a/llvm/utils/TableGen/FixedLenDecoderEmitter.cpp b/llvm/utils/TableGen/FixedLenDecoderEmitter.cpp
5704 index e1bfaa934..bf352f717 100644
5705 --- a/llvm/utils/TableGen/FixedLenDecoderEmitter.cpp
5706 +++ b/llvm/utils/TableGen/FixedLenDecoderEmitter.cpp
5707 @@ -2357,7 +2357,6 @@ static void emitDecodeInstruction(formatted_raw_ostream &OS) {
5708 << " uint32_t CurFieldValue = 0;\\\n"
5709 << " DecodeStatus S = MCDisassembler_Success;\\\n"
5710 << " while (true) {\\\n"
5711 - << " ptrdiff_t Loc = Ptr - DecodeTable;\\\n"
5712 << " switch (*Ptr) {\\\n"
5713 << " default:\\\n"
5714 << " return MCDisassembler_Fail;\\\n"
5715 diff --git a/llvm/utils/TableGen/RegisterInfoEmitter.cpp b/llvm/utils/TableGen/RegisterInfoEmitter.cpp
5716 index cf9c352d7..6064a20cd 100644
5717 --- a/llvm/utils/TableGen/RegisterInfoEmitter.cpp
5718 +++ b/llvm/utils/TableGen/RegisterInfoEmitter.cpp
5719 @@ -1175,17 +1175,17 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
5720 for (const auto &RC : RegisterClasses) {
5721 assert(isInt<8>(RC.CopyCost) && "Copy cost too large.");
5722 OS << " { " << RC.getName() << ", " << RC.getName() << "Bits, "
5723 - << RegClassStrings.get(RC.getName()) << ", "
5724 - << RC.getOrder().size() << ", sizeof(" << RC.getName() << "Bits), "
5725 #ifdef CAPSTONE
5726 - << NAME_PREFIX RC.getName()
5727 + << "sizeof(" << RC.getName() << "Bits) },\n";
5728 #else
5729 + << RegClassStrings.get(RC.getName()) << ", "
5730 + << RC.getOrder().size() << ", sizeof(" << RC.getName() << "Bits), "
5731 << RC.getQualifiedName()
5732 -#endif
5733 << "RegClassID" << ", "
5734 //<< RegSize/8 << ", "
5735 << RC.CopyCost << ", "
5736 << ( RC.Allocatable ? "true" : "false" ) << " },\n";
5737 +#endif
5738 }
5739
5740 OS << "};\n\n";
5741 --
5742 2.20.1
5743