Debian Codegraph @

contrib/riscv_update/0001-capstone-riscv-patchs.patch

raw
1 From 3373228170bbc2324d223bdeca761de3b4565508 Mon Sep 17 00:00:00 2001
2 From: fanfuqiang <[email protected]>
3 Date: Sun, 17 Feb 2019 06:08:44 +0800
4 Subject: [PATCH] capstone riscv patchs
5
6 ---
7 ...apstone-generate-GenRegisterInfo.inc.patch | 338 +++++++++++++
8 ...pstone-generate-GenSubtargetInfo.inc.patch | 86 ++++
9 ...3-capstone-generate-GenInstrInfo.inc.patch | 130 +++++
10 ...e-generate-GenDisassemblerTables.inc.patch | 472 ++++++++++++++++++
11 ...5-capstone-generate-GenAsmWriter.inc.patch | 225 +++++++++
12 ...06-capstone-generate-MappingInsn.inc.patch | 174 +++++++
13 ...apstone-generate-GenInsnNameMaps.inc.patch | 110 ++++
14 llvm/lib/Target/RISCV/CMakeLists.txt | 2 +
15 llvm/utils/TableGen/AsmWriterEmitter.cpp | 103 +++-
16 llvm/utils/TableGen/AsmWriterInst.cpp | 4 +
17 llvm/utils/TableGen/DisassemblerEmitter.cpp | 12 +-
18 .../utils/TableGen/FixedLenDecoderEmitter.cpp | 398 ++++++++++++++-
19 llvm/utils/TableGen/InstrInfoEmitter.cpp | 173 ++++++-
20 llvm/utils/TableGen/RegisterInfoEmitter.cpp | 130 ++++-
21 llvm/utils/TableGen/SubtargetEmitter.cpp | 28 +-
22 llvm/utils/TableGen/TableGen.cpp | 12 +
23 llvm/utils/TableGen/TableGenBackends.h | 2 +
24 17 files changed, 2349 insertions(+), 50 deletions(-)
25 create mode 100644 llvm/0001-capstone-generate-GenRegisterInfo.inc.patch
26 create mode 100644 llvm/0002-capstone-generate-GenSubtargetInfo.inc.patch
27 create mode 100644 llvm/0003-capstone-generate-GenInstrInfo.inc.patch
28 create mode 100644 llvm/0004-capstone-generate-GenDisassemblerTables.inc.patch
29 create mode 100644 llvm/0005-capstone-generate-GenAsmWriter.inc.patch
30 create mode 100644 llvm/0006-capstone-generate-MappingInsn.inc.patch
31 create mode 100644 llvm/0007-capstone-generate-GenInsnNameMaps.inc.patch
32
33 diff --git a/llvm/0001-capstone-generate-GenRegisterInfo.inc.patch b/llvm/0001-capstone-generate-GenRegisterInfo.inc.patch
34 new file mode 100644
35 index 000000000..b51aa515a
36 --- /dev/null
37 +++ b/llvm/0001-capstone-generate-GenRegisterInfo.inc.patch
38 @@ -0,0 +1,338 @@
39 +From 5d631cb16e7ba5dd0380ff1ee9dda192b1cdad18 Mon Sep 17 00:00:00 2001
40 +From: mephi42 <[email protected]>
41 +Date: Tue, 7 Aug 2018 17:02:40 +0200
42 +Subject: [PATCH 1/7] capstone: generate *GenRegisterInfo.inc
43 +
44 +---
45 + utils/TableGen/RegisterInfoEmitter.cpp | 130 ++++++++++++++++++++++---
46 + 1 file changed, 115 insertions(+), 15 deletions(-)
47 +
48 +diff --git a/utils/TableGen/RegisterInfoEmitter.cpp b/utils/TableGen/RegisterInfoEmitter.cpp
49 +index 49016cca799..6ebb7148b1b 100644
50 +--- a/utils/TableGen/RegisterInfoEmitter.cpp
51 ++++ b/utils/TableGen/RegisterInfoEmitter.cpp
52 +@@ -99,6 +99,12 @@ private:
53 +
54 + } // end anonymous namespace
55 +
56 ++#ifdef CAPSTONE
57 ++#define NAME_PREFIX Target.getName() << "_" <<
58 ++#else
59 ++#define NAME_PREFIX
60 ++#endif
61 ++
62 + // runEnums - Print out enum values for all of the registers.
63 + void RegisterInfoEmitter::runEnums(raw_ostream &OS,
64 + CodeGenTarget &Target, CodeGenRegBank &Bank) {
65 +@@ -107,13 +113,22 @@ void RegisterInfoEmitter::runEnums(raw_ostream &OS,
66 + // Register enums are stored as uint16_t in the tables. Make sure we'll fit.
67 + assert(Registers.size() <= 0xffff && "Too many regs to fit in tables");
68 +
69 ++#ifndef CAPSTONE
70 + StringRef Namespace = Registers.front().TheDef->getValueAsString("Namespace");
71 ++#endif
72 +
73 + emitSourceFileHeader("Target Register Enum Values", OS);
74 +
75 ++#ifdef CAPSTONE
76 ++ OS << "/* Capstone Disassembly Engine */\n"
77 ++ "/* By Nguyen Anh Quynh <[email protected]>, 2013-2015 */\n"
78 ++ "\n";
79 ++#endif
80 ++
81 + OS << "\n#ifdef GET_REGINFO_ENUM\n";
82 + OS << "#undef GET_REGINFO_ENUM\n\n";
83 +
84 ++#ifndef CAPSTONE
85 + OS << "namespace llvm {\n\n";
86 +
87 + OS << "class MCRegisterClass;\n"
88 +@@ -122,16 +137,20 @@ void RegisterInfoEmitter::runEnums(raw_ostream &OS,
89 +
90 + if (!Namespace.empty())
91 + OS << "namespace " << Namespace << " {\n";
92 +- OS << "enum {\n NoRegister,\n";
93 ++#endif
94 ++
95 ++ OS << "enum {\n " << NAME_PREFIX "NoRegister,\n";
96 +
97 + for (const auto &Reg : Registers)
98 +- OS << " " << Reg.getName() << " = " << Reg.EnumValue << ",\n";
99 ++ OS << " " << NAME_PREFIX Reg.getName() << " = " << Reg.EnumValue << ",\n";
100 + assert(Registers.size() == Registers.back().EnumValue &&
101 + "Register enum value mismatch!");
102 +- OS << " NUM_TARGET_REGS \t// " << Registers.size()+1 << "\n";
103 ++ OS << " " << NAME_PREFIX "NUM_TARGET_REGS \t// " << Registers.size()+1 << "\n";
104 + OS << "};\n";
105 ++#ifndef CAPSTONE
106 + if (!Namespace.empty())
107 + OS << "} // end namespace " << Namespace << "\n";
108 ++#endif
109 +
110 + const auto &RegisterClasses = Bank.getRegClasses();
111 + if (!RegisterClasses.empty()) {
112 +@@ -140,18 +159,29 @@ void RegisterInfoEmitter::runEnums(raw_ostream &OS,
113 + assert(RegisterClasses.size() <= 0xffff &&
114 + "Too many register classes to fit in tables");
115 +
116 +- OS << "\n// Register classes\n\n";
117 ++ OS << "\n// Register classes\n";
118 ++#ifndef CAPSTONE
119 ++ OS << "\n";
120 + if (!Namespace.empty())
121 + OS << "namespace " << Namespace << " {\n";
122 ++#endif
123 + OS << "enum {\n";
124 + for (const auto &RC : RegisterClasses)
125 +- OS << " " << RC.getName() << "RegClassID"
126 ++ OS << " " << NAME_PREFIX RC.getName() << "RegClassID"
127 + << " = " << RC.EnumValue << ",\n";
128 +- OS << "\n };\n";
129 ++#ifdef CAPSTONE
130 ++ OS
131 ++#else
132 ++ OS << "\n "
133 ++#endif
134 ++ << "};\n";
135 ++#ifndef CAPSTONE
136 + if (!Namespace.empty())
137 + OS << "} // end namespace " << Namespace << "\n\n";
138 ++#endif
139 + }
140 +
141 ++#ifndef CAPSTONE
142 + const std::vector<Record*> &RegAltNameIndices = Target.getRegAltNameIndices();
143 + // If the only definition is the default NoRegAltName, we don't need to
144 + // emit anything.
145 +@@ -182,8 +212,11 @@ void RegisterInfoEmitter::runEnums(raw_ostream &OS,
146 + if (!Namespace.empty())
147 + OS << "} // end namespace " << Namespace << "\n\n";
148 + }
149 ++#endif
150 +
151 ++#ifndef CAPSTONE
152 + OS << "} // end namespace llvm\n\n";
153 ++#endif
154 + OS << "#endif // GET_REGINFO_ENUM\n\n";
155 + }
156 +
157 +@@ -830,7 +863,9 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
158 +
159 + const auto &Regs = RegBank.getRegisters();
160 +
161 ++#ifndef CAPSTONE
162 + auto &SubRegIndices = RegBank.getSubRegIndices();
163 ++#endif
164 + // The lists of sub-registers and super-registers go in the same array. That
165 + // allows us to share suffixes.
166 + typedef std::vector<const CodeGenRegister*> RegVec;
167 +@@ -922,25 +957,40 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
168 + LaneMaskSeqs.layout();
169 + SubRegIdxSeqs.layout();
170 +
171 ++#ifndef CAPSTONE
172 + OS << "namespace llvm {\n\n";
173 ++#endif
174 +
175 + const std::string &TargetName = Target.getName();
176 +
177 + // Emit the shared table of differential lists.
178 +- OS << "extern const MCPhysReg " << TargetName << "RegDiffLists[] = {\n";
179 ++#ifdef CAPSTONE
180 ++ OS << "static"
181 ++#else
182 ++ OS << "extern"
183 ++#endif
184 ++ << " const MCPhysReg " << TargetName << "RegDiffLists[] = {\n";
185 + DiffSeqs.emit(OS, printDiff16);
186 + OS << "};\n\n";
187 +
188 ++#ifndef CAPSTONE
189 + // Emit the shared table of regunit lane mask sequences.
190 + OS << "extern const LaneBitmask " << TargetName << "LaneMaskLists[] = {\n";
191 + LaneMaskSeqs.emit(OS, printMask, "LaneBitmask::getAll()");
192 + OS << "};\n\n";
193 ++#endif
194 +
195 + // Emit the table of sub-register indexes.
196 +- OS << "extern const uint16_t " << TargetName << "SubRegIdxLists[] = {\n";
197 ++#ifdef CAPSTONE
198 ++ OS << "static"
199 ++#else
200 ++ OS << "extern"
201 ++#endif
202 ++ << " const uint16_t " << TargetName << "SubRegIdxLists[] = {\n";
203 + SubRegIdxSeqs.emit(OS, printSubRegIndex);
204 + OS << "};\n\n";
205 +
206 ++#ifndef CAPSTONE
207 + // Emit the table of sub-register index sizes.
208 + OS << "extern const MCRegisterInfo::SubRegCoveredBits "
209 + << TargetName << "SubRegIdxRanges[] = {\n";
210 +@@ -950,14 +1000,22 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
211 + << Idx.getName() << "\n";
212 + }
213 + OS << "};\n\n";
214 ++#endif
215 +
216 + // Emit the string table.
217 + RegStrings.layout();
218 ++#ifndef CAPSTONE
219 + OS << "extern const char " << TargetName << "RegStrings[] = {\n";
220 + RegStrings.emit(OS, printChar);
221 + OS << "};\n\n";
222 ++#endif
223 +
224 +- OS << "extern const MCRegisterDesc " << TargetName
225 ++#ifdef CAPSTONE
226 ++ OS << "static"
227 ++#else
228 ++ OS << "extern"
229 ++#endif
230 ++ << " const MCRegisterDesc " << TargetName
231 + << "RegDesc[] = { // Descriptors\n";
232 + OS << " { " << RegStrings.get("") << ", 0, 0, 0, 0, 0 },\n";
233 +
234 +@@ -973,6 +1031,7 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
235 + }
236 + OS << "};\n\n"; // End of register descriptors...
237 +
238 ++#ifndef CAPSTONE
239 + // Emit the table of register unit roots. Each regunit has one or two root
240 + // registers.
241 + OS << "extern const MCPhysReg " << TargetName << "RegUnitRoots[][2] = {\n";
242 +@@ -986,11 +1045,14 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
243 + OS << " },\n";
244 + }
245 + OS << "};\n\n";
246 ++#endif
247 +
248 + const auto &RegisterClasses = RegBank.getRegClasses();
249 +
250 + // Loop over all of the register classes... emitting each one.
251 ++#ifndef CAPSTONE
252 + OS << "namespace { // Register classes...\n";
253 ++#endif
254 +
255 + SequenceToOffsetTable<std::string> RegClassStrings;
256 +
257 +@@ -1005,15 +1067,28 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
258 +
259 + // Emit the register list now.
260 + OS << " // " << Name << " Register Class...\n"
261 +- << " const MCPhysReg " << Name
262 ++ << " "
263 ++#ifdef CAPSTONE
264 ++ << "static "
265 ++#endif
266 ++ << "const MCPhysReg " << Name
267 + << "[] = {\n ";
268 + for (Record *Reg : Order) {
269 +- OS << getQualifiedName(Reg) << ", ";
270 ++#ifdef CAPSTONE
271 ++ OS << NAME_PREFIX Reg->getName()
272 ++#else
273 ++ OS << getQualifiedName(Reg)
274 ++#endif
275 ++ << ", ";
276 + }
277 + OS << "\n };\n\n";
278 +
279 + OS << " // " << Name << " Bit set.\n"
280 +- << " const uint8_t " << Name
281 ++ << " "
282 ++#ifdef CAPSTONE
283 ++ << "static "
284 ++#endif
285 ++ << "const uint8_t " << Name
286 + << "Bits[] = {\n ";
287 + BitVectorEmitter BVE;
288 + for (Record *Reg : Order) {
289 +@@ -1023,14 +1098,23 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
290 + OS << "\n };\n\n";
291 +
292 + }
293 ++#ifndef CAPSTONE
294 + OS << "} // end anonymous namespace\n\n";
295 ++#endif
296 +
297 + RegClassStrings.layout();
298 ++#ifndef CAPSTONE
299 + OS << "extern const char " << TargetName << "RegClassStrings[] = {\n";
300 + RegClassStrings.emit(OS, printChar);
301 + OS << "};\n\n";
302 ++#endif
303 +
304 +- OS << "extern const MCRegisterClass " << TargetName
305 ++#ifdef CAPSTONE
306 ++ OS << "static"
307 ++#else
308 ++ OS << "extern"
309 ++#endif
310 ++ << " const MCRegisterClass " << TargetName
311 + << "MCRegisterClasses[] = {\n";
312 +
313 + for (const auto &RC : RegisterClasses) {
314 +@@ -1041,7 +1125,12 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
315 + OS << " { " << RC.getName() << ", " << RC.getName() << "Bits, "
316 + << RegClassStrings.get(RC.getName()) << ", "
317 + << RC.getOrder().size() << ", sizeof(" << RC.getName() << "Bits), "
318 +- << RC.getQualifiedName() + "RegClassID" << ", "
319 ++#ifdef CAPSTONE
320 ++ << NAME_PREFIX RC.getName()
321 ++#else
322 ++ << RC.getQualifiedName()
323 ++#endif
324 ++ << "RegClassID" << ", "
325 + << RegSize/8 << ", "
326 + << RC.CopyCost << ", "
327 + << ( RC.Allocatable ? "true" : "false" ) << " },\n";
328 +@@ -1049,6 +1138,7 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
329 +
330 + OS << "};\n\n";
331 +
332 ++#ifndef CAPSTONE
333 + EmitRegMappingTables(OS, Regs, false);
334 +
335 + // Emit Reg encoding table
336 +@@ -1067,7 +1157,9 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
337 + OS << " " << Value << ",\n";
338 + }
339 + OS << "};\n"; // End of HW encoding table
340 ++#endif
341 +
342 ++#ifndef CAPSTONE
343 + // MCRegisterInfo initialization routine.
344 + OS << "static inline void Init" << TargetName
345 + << "MCRegisterInfo(MCRegisterInfo *RI, unsigned RA, "
346 +@@ -1088,7 +1180,12 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
347 + OS << "}\n\n";
348 +
349 + OS << "} // end namespace llvm\n\n";
350 +- OS << "#endif // GET_REGINFO_MC_DESC\n\n";
351 ++#endif
352 ++ OS << "#endif // GET_REGINFO_MC_DESC\n"
353 ++#ifndef CAPSTONE
354 ++ << "\n"
355 ++#endif
356 ++ ;
357 + }
358 +
359 + void
360 +@@ -1568,10 +1665,13 @@ RegisterInfoEmitter::runTargetDesc(raw_ostream &OS, CodeGenTarget &Target,
361 +
362 + void RegisterInfoEmitter::run(raw_ostream &OS) {
363 + CodeGenRegBank &RegBank = Target.getRegBank();
364 ++
365 + runEnums(OS, Target, RegBank);
366 + runMCDesc(OS, Target, RegBank);
367 ++#ifndef CAPSTONE
368 + runTargetHeader(OS, Target, RegBank);
369 + runTargetDesc(OS, Target, RegBank);
370 ++#endif
371 +
372 + if (RegisterInfoDebug)
373 + debugDump(errs());
374 +--
375 +2.19.1
376 +
377 diff --git a/llvm/0002-capstone-generate-GenSubtargetInfo.inc.patch b/llvm/0002-capstone-generate-GenSubtargetInfo.inc.patch
378 new file mode 100644
379 index 000000000..56ad28256
380 --- /dev/null
381 +++ b/llvm/0002-capstone-generate-GenSubtargetInfo.inc.patch
382 @@ -0,0 +1,86 @@
383 +From 46ca491e1bbbc9ace2a91fe6a7b112c83b9b88cc Mon Sep 17 00:00:00 2001
384 +From: mephi42 <[email protected]>
385 +Date: Tue, 7 Aug 2018 17:42:59 +0200
386 +Subject: [PATCH 2/7] capstone: generate *GenSubtargetInfo.inc
387 +
388 +---
389 + utils/TableGen/SubtargetEmitter.cpp | 28 +++++++++++++++++++++++++++-
390 + 1 file changed, 27 insertions(+), 1 deletion(-)
391 +
392 +diff --git a/utils/TableGen/SubtargetEmitter.cpp b/utils/TableGen/SubtargetEmitter.cpp
393 +index c5da8d8142f..98ab3240472 100644
394 +--- a/utils/TableGen/SubtargetEmitter.cpp
395 ++++ b/utils/TableGen/SubtargetEmitter.cpp
396 +@@ -147,7 +147,9 @@ void SubtargetEmitter::Enumeration(raw_ostream &OS) {
397 + if (N > MAX_SUBTARGET_FEATURES)
398 + PrintFatalError("Too many subtarget features! Bump MAX_SUBTARGET_FEATURES.");
399 +
400 ++#ifndef CAPSTONE
401 + OS << "namespace " << Target << " {\n";
402 ++#endif
403 +
404 + // Open enumeration.
405 + OS << "enum {\n";
406 +@@ -158,12 +160,22 @@ void SubtargetEmitter::Enumeration(raw_ostream &OS) {
407 + Record *Def = DefList[i];
408 +
409 + // Get and emit name
410 +- OS << " " << Def->getName() << " = " << i << ",\n";
411 ++ OS << " "
412 ++#ifdef CAPSTONE
413 ++ << Target << "_"
414 ++#endif
415 ++ << Def->getName() << " = "
416 ++#ifdef CAPSTONE
417 ++ << "1ULL << "
418 ++#endif
419 ++ << i << ",\n";
420 + }
421 +
422 + // Close enumeration and namespace
423 + OS << "};\n";
424 ++#ifndef CAPSTONE
425 + OS << "} // end namespace " << Target << "\n";
426 ++#endif
427 + }
428 +
429 + //
430 +@@ -1709,14 +1721,27 @@ void SubtargetEmitter::emitGenMCSubtargetInfo(raw_ostream &OS) {
431 + void SubtargetEmitter::run(raw_ostream &OS) {
432 + emitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
433 +
434 ++#ifdef CAPSTONE
435 ++ OS << "/* Capstone Disassembly Engine, http://www.capstone-engine.org */\n"
436 ++ "/* By Nguyen Anh Quynh <[email protected]>, 2013-2015 */\n"
437 ++ "\n";
438 ++#endif
439 ++
440 + OS << "\n#ifdef GET_SUBTARGETINFO_ENUM\n";
441 + OS << "#undef GET_SUBTARGETINFO_ENUM\n\n";
442 +
443 ++#ifndef CAPSTONE
444 + OS << "namespace llvm {\n";
445 ++#endif
446 + Enumeration(OS);
447 ++#ifdef CAPSTONE
448 ++ OS << "\n";
449 ++#else
450 + OS << "} // end namespace llvm\n\n";
451 ++#endif
452 + OS << "#endif // GET_SUBTARGETINFO_ENUM\n\n";
453 +
454 ++#ifndef CAPSTONE
455 + OS << "\n#ifdef GET_SUBTARGETINFO_MC_DESC\n";
456 + OS << "#undef GET_SUBTARGETINFO_MC_DESC\n\n";
457 +
458 +@@ -1857,6 +1882,7 @@ void SubtargetEmitter::run(raw_ostream &OS) {
459 + OS << "} // end namespace llvm\n\n";
460 +
461 + OS << "#endif // GET_SUBTARGETINFO_CTOR\n\n";
462 ++#endif
463 + }
464 +
465 + namespace llvm {
466 +--
467 +2.19.1
468 +
469 diff --git a/llvm/0003-capstone-generate-GenInstrInfo.inc.patch b/llvm/0003-capstone-generate-GenInstrInfo.inc.patch
470 new file mode 100644
471 index 000000000..2baa59fc9
472 --- /dev/null
473 +++ b/llvm/0003-capstone-generate-GenInstrInfo.inc.patch
474 @@ -0,0 +1,130 @@
475 +From a73fe8ac18d3ca81fa7a8d8c404cd7e0faf92ddc Mon Sep 17 00:00:00 2001
476 +From: mephi42 <[email protected]>
477 +Date: Tue, 7 Aug 2018 17:59:43 +0200
478 +Subject: [PATCH 3/7] capstone: generate *GenInstrInfo.inc
479 +
480 +---
481 + utils/TableGen/InstrInfoEmitter.cpp | 49 ++++++++++++++++++++++++++---
482 + 1 file changed, 44 insertions(+), 5 deletions(-)
483 +
484 +diff --git a/utils/TableGen/InstrInfoEmitter.cpp b/utils/TableGen/InstrInfoEmitter.cpp
485 +index 0aff1aa6f94..2f3a2729262 100644
486 +--- a/utils/TableGen/InstrInfoEmitter.cpp
487 ++++ b/utils/TableGen/InstrInfoEmitter.cpp
488 +@@ -92,6 +92,7 @@ private:
489 +
490 + } // end anonymous namespace
491 +
492 ++#ifndef CAPSTONE
493 + static void PrintDefList(const std::vector<Record*> &Uses,
494 + unsigned Num, raw_ostream &OS) {
495 + OS << "static const MCPhysReg ImplicitList" << Num << "[] = { ";
496 +@@ -99,6 +100,7 @@ static void PrintDefList(const std::vector<Record*> &Uses,
497 + OS << getQualifiedName(U) << ", ";
498 + OS << "0 };\n";
499 + }
500 ++#endif
501 +
502 + //===----------------------------------------------------------------------===//
503 + // Operand Info Emission.
504 +@@ -426,8 +428,17 @@ void InstrInfoEmitter::emitTIIHelperMethods(raw_ostream &OS) {
505 + // run - Emit the main instruction description records for the target...
506 + void InstrInfoEmitter::run(raw_ostream &OS) {
507 + emitSourceFileHeader("Target Instruction Enum Values and Descriptors", OS);
508 ++
509 ++#ifdef CAPSTONE
510 ++ OS << "/* Capstone Disassembly Engine */\n"
511 ++ "/* By Nguyen Anh Quynh <[email protected]>, 2013-2015 */\n"
512 ++ "\n"
513 ++ "\n";
514 ++#endif
515 ++
516 + emitEnums(OS);
517 +
518 ++#ifndef CAPSTONE
519 + OS << "#ifdef GET_INSTRINFO_MC_DESC\n";
520 + OS << "#undef GET_INSTRINFO_MC_DESC\n";
521 +
522 +@@ -545,6 +556,7 @@ void InstrInfoEmitter::run(raw_ostream &OS) {
523 + emitOperandTypesEnum(OS, Target);
524 +
525 + emitMCIIHelperMethods(OS);
526 ++#endif
527 + }
528 +
529 + void InstrInfoEmitter::emitRecord(const CodeGenInstruction &Inst, unsigned Num,
530 +@@ -659,7 +671,9 @@ void InstrInfoEmitter::emitEnums(raw_ostream &OS) {
531 + OS << "#ifdef GET_INSTRINFO_ENUM\n";
532 + OS << "#undef GET_INSTRINFO_ENUM\n";
533 +
534 ++#ifndef CAPSTONE
535 + OS << "namespace llvm {\n\n";
536 ++#endif
537 +
538 + CodeGenTarget Target(Records);
539 +
540 +@@ -669,17 +683,39 @@ void InstrInfoEmitter::emitEnums(raw_ostream &OS) {
541 + if (Namespace.empty())
542 + PrintFatalError("No instructions defined!");
543 +
544 ++#ifndef CAPSTONE
545 + OS << "namespace " << Namespace << " {\n";
546 +- OS << " enum {\n";
547 ++#endif
548 ++#ifdef CAPSTONE
549 ++ OS << "\n"
550 ++#else
551 ++ OS << " "
552 ++#endif
553 ++ << "enum {\n";
554 + unsigned Num = 0;
555 + for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue())
556 +- OS << " " << Inst->TheDef->getName() << "\t= " << Num++ << ",\n";
557 +- OS << " INSTRUCTION_LIST_END = " << Num << "\n";
558 ++ OS << " "
559 ++#ifdef CAPSTONE
560 ++ << Target.getName() << "_"
561 ++#endif
562 ++ << Inst->TheDef->getName() << "\t= " << Num++ << ",\n";
563 ++ OS << " "
564 ++#ifdef CAPSTONE
565 ++ << Target.getName() << "_"
566 ++#endif
567 ++ << "INSTRUCTION_LIST_END = " << Num << "\n";
568 + OS << " };\n\n";
569 ++#ifndef CAPSTONE
570 + OS << "} // end " << Namespace << " namespace\n";
571 + OS << "} // end llvm namespace\n";
572 +- OS << "#endif // GET_INSTRINFO_ENUM\n\n";
573 +-
574 ++#endif
575 ++ OS << "#endif // GET_INSTRINFO_ENUM\n"
576 ++#ifndef CAPSTONE
577 ++ << "\n"
578 ++#endif
579 ++ ;
580 ++
581 ++#ifndef CAPSTONE
582 + OS << "#ifdef GET_INSTRINFO_SCHED_ENUM\n";
583 + OS << "#undef GET_INSTRINFO_SCHED_ENUM\n";
584 + OS << "namespace llvm {\n\n";
585 +@@ -696,13 +732,16 @@ void InstrInfoEmitter::emitEnums(raw_ostream &OS) {
586 + OS << "} // end llvm namespace\n";
587 +
588 + OS << "#endif // GET_INSTRINFO_SCHED_ENUM\n\n";
589 ++#endif
590 + }
591 +
592 + namespace llvm {
593 +
594 + void EmitInstrInfo(RecordKeeper &RK, raw_ostream &OS) {
595 + InstrInfoEmitter(RK).run(OS);
596 ++#ifndef CAPSTONE
597 + EmitMapTable(RK, OS);
598 ++#endif
599 + }
600 +
601 + } // end llvm namespace
602 +--
603 +2.19.1
604 +
605 diff --git a/llvm/0004-capstone-generate-GenDisassemblerTables.inc.patch b/llvm/0004-capstone-generate-GenDisassemblerTables.inc.patch
606 new file mode 100644
607 index 000000000..0002b81b4
608 --- /dev/null
609 +++ b/llvm/0004-capstone-generate-GenDisassemblerTables.inc.patch
610 @@ -0,0 +1,472 @@
611 +From 29da4c6929679b8ac4019767ab4ebcd83c9894b4 Mon Sep 17 00:00:00 2001
612 +From: mephi42 <[email protected]>
613 +Date: Tue, 7 Aug 2018 18:20:17 +0200
614 +Subject: [PATCH 4/7] capstone: generate *GenDisassemblerTables.inc
615 +
616 +---
617 + utils/TableGen/DisassemblerEmitter.cpp | 12 +-
618 + utils/TableGen/FixedLenDecoderEmitter.cpp | 248 ++++++++++++++++++++--
619 + 2 files changed, 239 insertions(+), 21 deletions(-)
620 +
621 +diff --git a/utils/TableGen/DisassemblerEmitter.cpp b/utils/TableGen/DisassemblerEmitter.cpp
622 +index b99a0a973a2..2ac6d89645c 100644
623 +--- a/utils/TableGen/DisassemblerEmitter.cpp
624 ++++ b/utils/TableGen/DisassemblerEmitter.cpp
625 +@@ -106,6 +106,11 @@ extern void EmitFixedLenDecoder(RecordKeeper &RK, raw_ostream &OS,
626 + void EmitDisassembler(RecordKeeper &Records, raw_ostream &OS) {
627 + CodeGenTarget Target(Records);
628 + emitSourceFileHeader(" * " + Target.getName().str() + " Disassembler", OS);
629 ++#ifdef CAPSTONE
630 ++ OS << "/* Capstone Disassembly Engine */\n"
631 ++ "/* By Nguyen Anh Quynh <[email protected]>, 2013-2015 */\n"
632 ++ "\n";
633 ++#endif
634 +
635 + // X86 uses a custom disassembler.
636 + if (Target.getName() == "X86") {
637 +@@ -150,7 +155,12 @@ void EmitDisassembler(RecordKeeper &Records, raw_ostream &OS) {
638 + }
639 +
640 + EmitFixedLenDecoder(Records, OS, Target.getName(),
641 +- "if (", " == MCDisassembler::Fail)",
642 ++ "if (",
643 ++#ifdef CAPSTONE
644 ++ " == MCDisassembler_Fail)",
645 ++#else
646 ++ " == MCDisassembler::Fail)",
647 ++#endif
648 + "MCDisassembler::Success", "MCDisassembler::Fail", "");
649 + }
650 +
651 +diff --git a/utils/TableGen/FixedLenDecoderEmitter.cpp b/utils/TableGen/FixedLenDecoderEmitter.cpp
652 +index fcecc764d44..36845d960d8 100644
653 +--- a/utils/TableGen/FixedLenDecoderEmitter.cpp
654 ++++ b/utils/TableGen/FixedLenDecoderEmitter.cpp
655 +@@ -730,7 +730,13 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
656 + ++I;
657 + unsigned Start = *I++;
658 + unsigned Len = *I++;
659 +- OS.indent(Indentation) << "MCD::OPC_ExtractField, " << Start << ", "
660 ++ OS.indent(Indentation)
661 ++#ifdef CAPSTONE
662 ++ << "MCD_OPC_ExtractField"
663 ++#else
664 ++ << "MCD::OPC_ExtractField"
665 ++#endif
666 ++ << ", " << Start << ", "
667 + << Len << ", // Inst{";
668 + if (Len > 1)
669 + OS << (Start + Len - 1) << "-";
670 +@@ -739,7 +745,13 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
671 + }
672 + case MCD::OPC_FilterValue: {
673 + ++I;
674 +- OS.indent(Indentation) << "MCD::OPC_FilterValue, ";
675 ++ OS.indent(Indentation)
676 ++#ifdef CAPSTONE
677 ++ << "MCD_OPC_FilterValue"
678 ++#else
679 ++ << "MCD::OPC_FilterValue"
680 ++#endif
681 ++ << ", ";
682 + // The filter value is ULEB128 encoded.
683 + while (*I >= 128)
684 + OS << (unsigned)*I++ << ", ";
685 +@@ -759,7 +771,13 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
686 + ++I;
687 + unsigned Start = *I++;
688 + unsigned Len = *I++;
689 +- OS.indent(Indentation) << "MCD::OPC_CheckField, " << Start << ", "
690 ++ OS.indent(Indentation)
691 ++#ifdef CAPSTONE
692 ++ << "MCD_OPC_CheckField"
693 ++#else
694 ++ << "MCD::OPC_CheckField"
695 ++#endif
696 ++ << ", " << Start << ", "
697 + << Len << ", ";// << Val << ", " << NumToSkip << ",\n";
698 + // ULEB128 encoded field value.
699 + for (; *I >= 128; ++I)
700 +@@ -777,7 +795,13 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
701 + }
702 + case MCD::OPC_CheckPredicate: {
703 + ++I;
704 +- OS.indent(Indentation) << "MCD::OPC_CheckPredicate, ";
705 ++ OS.indent(Indentation)
706 ++#ifdef CAPSTONE
707 ++ << "MCD_OPC_CheckPredicate"
708 ++#else
709 ++ << "MCD::OPC_CheckPredicate"
710 ++#endif
711 ++ << ", ";
712 + for (; *I >= 128; ++I)
713 + OS << (unsigned)*I << ", ";
714 + OS << (unsigned)*I++ << ", ";
715 +@@ -803,7 +827,13 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
716 + && "ULEB128 value too large!");
717 + // Decode the Opcode value.
718 + unsigned Opc = decodeULEB128(Buffer);
719 +- OS.indent(Indentation) << "MCD::OPC_" << (IsTry ? "Try" : "")
720 ++ OS.indent(Indentation)
721 ++#ifdef CAPSTONE
722 ++ << "MCD_OPC_"
723 ++#else
724 ++ << "MCD::OPC_"
725 ++#endif
726 ++ << (IsTry ? "Try" : "")
727 + << "Decode, ";
728 + for (p = Buffer; *p >= 128; ++p)
729 + OS << (unsigned)*p << ", ";
730 +@@ -837,7 +867,12 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
731 + }
732 + case MCD::OPC_SoftFail: {
733 + ++I;
734 +- OS.indent(Indentation) << "MCD::OPC_SoftFail";
735 ++ OS.indent(Indentation)
736 ++#ifdef CAPSTONE
737 ++ << "MCD_OPC_SoftFail";
738 ++#else
739 ++ << "MCD::OPC_SoftFail";
740 ++#endif
741 + // Positive mask
742 + uint64_t Value = 0;
743 + unsigned Shift = 0;
744 +@@ -869,7 +904,13 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
745 + }
746 + case MCD::OPC_Fail: {
747 + ++I;
748 +- OS.indent(Indentation) << "MCD::OPC_Fail,\n";
749 ++ OS.indent(Indentation)
750 ++#ifdef CAPSTONE
751 ++ << "MCD_OPC_Fail"
752 ++#else
753 ++ << "MCD::OPC_Fail"
754 ++#endif
755 ++ << ",\n";
756 + break;
757 + }
758 + }
759 +@@ -884,23 +925,46 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
760 + void FixedLenDecoderEmitter::
761 + emitPredicateFunction(formatted_raw_ostream &OS, PredicateSet &Predicates,
762 + unsigned Indentation) const {
763 ++#ifdef CAPSTONE
764 ++ OS.indent(Indentation) << "static bool getbool(uint64_t b)\n";
765 ++ OS.indent(Indentation) << "{\n";
766 ++ OS.indent(Indentation) << "\treturn b != 0;\n";
767 ++ OS.indent(Indentation) << "}\n\n";
768 ++#endif
769 ++
770 + // The predicate function is just a big switch statement based on the
771 + // input predicate index.
772 + OS.indent(Indentation) << "static bool checkDecoderPredicate(unsigned Idx, "
773 ++#ifdef CAPSTONE
774 ++ << "uint64_t Bits)\n{\n";
775 ++#else
776 + << "const FeatureBitset& Bits) {\n";
777 ++#endif
778 + Indentation += 2;
779 + if (!Predicates.empty()) {
780 + OS.indent(Indentation) << "switch (Idx) {\n";
781 +- OS.indent(Indentation) << "default: llvm_unreachable(\"Invalid index!\");\n";
782 ++ OS.indent(Indentation) << "default: "
783 ++#ifdef CAPSTONE
784 ++ << "// "
785 ++#endif
786 ++ << "llvm_unreachable(\"Invalid index!\");\n";
787 + unsigned Index = 0;
788 + for (const auto &Predicate : Predicates) {
789 + OS.indent(Indentation) << "case " << Index++ << ":\n";
790 +- OS.indent(Indentation+2) << "return (" << Predicate << ");\n";
791 ++ OS.indent(Indentation+2) << "return "
792 ++#ifdef CAPSTONE
793 ++ << "getbool"
794 ++#endif
795 ++ << "(" << Predicate << ");\n";
796 + }
797 + OS.indent(Indentation) << "}\n";
798 + } else {
799 + // No case statement to emit
800 +- OS.indent(Indentation) << "llvm_unreachable(\"Invalid index!\");\n";
801 ++ OS.indent(Indentation)
802 ++#ifdef CAPSTONE
803 ++ << "// "
804 ++#endif
805 ++ << "llvm_unreachable(\"Invalid index!\");\n";
806 + }
807 + Indentation -= 2;
808 + OS.indent(Indentation) << "}\n\n";
809 +@@ -911,23 +975,39 @@ emitDecoderFunction(formatted_raw_ostream &OS, DecoderSet &Decoders,
810 + unsigned Indentation) const {
811 + // The decoder function is just a big switch statement based on the
812 + // input decoder index.
813 ++#ifdef CAPSTONE
814 ++#define EDF_EOL " \\\n"
815 ++ OS.indent(Indentation) << "#define DecodeToMCInst(fname,fieldname, InsnType) \\\n";
816 ++ OS.indent(Indentation) << "static DecodeStatus fname(DecodeStatus S, unsigned Idx, InsnType insn, MCInst *MI, \\\n";
817 ++ OS.indent(Indentation) << " uint64_t Address, const void *Decoder) \\\n";
818 ++ OS.indent(Indentation) << "{ \\\n";
819 ++#else
820 ++#define EDF_EOL "\n"
821 + OS.indent(Indentation) << "template<typename InsnType>\n";
822 + OS.indent(Indentation) << "static DecodeStatus decodeToMCInst(DecodeStatus S,"
823 + << " unsigned Idx, InsnType insn, MCInst &MI,\n";
824 + OS.indent(Indentation) << " uint64_t "
825 + << "Address, const void *Decoder, bool &DecodeComplete) {\n";
826 ++#endif
827 + Indentation += 2;
828 ++#ifndef CAPSTONE
829 + OS.indent(Indentation) << "DecodeComplete = true;\n";
830 +- OS.indent(Indentation) << "InsnType tmp;\n";
831 +- OS.indent(Indentation) << "switch (Idx) {\n";
832 +- OS.indent(Indentation) << "default: llvm_unreachable(\"Invalid index!\");\n";
833 ++#endif
834 ++ OS.indent(Indentation) << "InsnType tmp;" EDF_EOL;
835 ++ OS.indent(Indentation) << "switch (Idx) {" EDF_EOL;
836 ++ OS.indent(Indentation) << "default:"
837 ++#ifndef CAPSTONE
838 ++ << " llvm_unreachable(\"Invalid index!\");\n";
839 ++#else
840 ++ << " \\\n";
841 ++#endif
842 + unsigned Index = 0;
843 + for (const auto &Decoder : Decoders) {
844 +- OS.indent(Indentation) << "case " << Index++ << ":\n";
845 ++ OS.indent(Indentation) << "case " << Index++ << ":" EDF_EOL;
846 + OS << Decoder;
847 +- OS.indent(Indentation+2) << "return S;\n";
848 ++ OS.indent(Indentation+2) << "return S;" EDF_EOL;
849 + }
850 +- OS.indent(Indentation) << "}\n";
851 ++ OS.indent(Indentation) << "}" EDF_EOL;
852 + Indentation -= 2;
853 + OS.indent(Indentation) << "}\n\n";
854 + }
855 +@@ -1054,16 +1134,21 @@ void FilterChooser::emitBinaryParser(raw_ostream &o, unsigned &Indentation,
856 + const std::string &Decoder = OpInfo.Decoder;
857 +
858 + if (OpInfo.numFields() != 1)
859 +- o.indent(Indentation) << "tmp = 0;\n";
860 ++ o.indent(Indentation) << "tmp = 0;" EDF_EOL;
861 +
862 + for (const EncodingField &EF : OpInfo) {
863 + o.indent(Indentation) << "tmp ";
864 + if (OpInfo.numFields() != 1) o << '|';
865 +- o << "= fieldFromInstruction"
866 ++ o << "= "
867 ++#ifdef CAPSTONE
868 ++ << "fieldname"
869 ++#else
870 ++ << "fieldFromInstruction"
871 ++#endif
872 + << "(insn, " << EF.Base << ", " << EF.Width << ')';
873 + if (OpInfo.numFields() != 1 || EF.Offset != 0)
874 + o << " << " << EF.Offset;
875 +- o << ";\n";
876 ++ o << ";" EDF_EOL;
877 + }
878 +
879 + if (Decoder != "") {
880 +@@ -1071,8 +1156,12 @@ void FilterChooser::emitBinaryParser(raw_ostream &o, unsigned &Indentation,
881 + o.indent(Indentation) << Emitter->GuardPrefix << Decoder
882 + << "(MI, tmp, Address, Decoder)"
883 + << Emitter->GuardPostfix
884 ++#ifdef CAPSTONE
885 ++ << " return MCDisassembler_Fail; \\\n";
886 ++#else
887 + << " { " << (OpHasCompleteDecoder ? "" : "DecodeComplete = false; ")
888 + << "return MCDisassembler::Fail; }\n";
889 ++#endif
890 + } else {
891 + OpHasCompleteDecoder = true;
892 + o.indent(Indentation) << "MI.addOperand(MCOperand::createImm(tmp));\n";
893 +@@ -1091,7 +1180,13 @@ void FilterChooser::emitDecoder(raw_ostream &OS, unsigned Indentation,
894 + << "(MI, insn, Address, Decoder)"
895 + << Emitter->GuardPostfix
896 + << " { " << (HasCompleteDecoder ? "" : "DecodeComplete = false; ")
897 +- << "return MCDisassembler::Fail; }\n";
898 ++ << "return "
899 ++#ifdef CAPSTONE
900 ++ << "MCDisassembler_Fail"
901 ++#else
902 ++ << "MCDisassembler::Fail"
903 ++#endif
904 ++ << "; }\n";
905 + break;
906 + }
907 +
908 +@@ -1129,10 +1224,19 @@ unsigned FilterChooser::getDecoderIndex(DecoderSet &Decoders,
909 + static void emitSinglePredicateMatch(raw_ostream &o, StringRef str,
910 + const std::string &PredicateNamespace) {
911 + if (str[0] == '!')
912 ++#ifdef CAPSTONE
913 ++ o << "~(Bits & " << PredicateNamespace << "_"
914 ++ << str.slice(1,str.size()) << ")";
915 ++#else
916 + o << "!Bits[" << PredicateNamespace << "::"
917 + << str.slice(1,str.size()) << "]";
918 ++#endif
919 + else
920 ++#ifdef CAPSTONE
921 ++ o << "(Bits & " << PredicateNamespace << "_" << str << ")";
922 ++#else
923 + o << "Bits[" << PredicateNamespace << "::" << str << "]";
924 ++#endif
925 + }
926 +
927 + bool FilterChooser::emitPredicateMatch(raw_ostream &o, unsigned &Indentation,
928 +@@ -2047,6 +2151,17 @@ static bool populateInstruction(CodeGenTarget &Target,
929 + // fieldFromInstruction().
930 + static void emitFieldFromInstruction(formatted_raw_ostream &OS) {
931 + OS << "// Helper function for extracting fields from encoded instructions.\n"
932 ++#ifdef CAPSTONE
933 ++ << "#define FieldFromInstruction(fname, InsnType) \\\n"
934 ++ << "static InsnType fname(InsnType insn, unsigned startBit, unsigned numBits) \\\n"
935 ++ << "{ \\\n"
936 ++ << " InsnType fieldMask; \\\n"
937 ++ << " if (numBits == sizeof(InsnType)*8) \\\n"
938 ++ << " fieldMask = (InsnType)(-1LL); \\\n"
939 ++ << " else \\\n"
940 ++ << " fieldMask = (((InsnType)1 << numBits) - 1) << startBit; \\\n"
941 ++ << " return (insn & fieldMask) >> startBit; \\\n"
942 ++#else
943 + << "template<typename InsnType>\n"
944 + << "static InsnType fieldFromInstruction(InsnType insn, unsigned startBit,\n"
945 + << " unsigned numBits) {\n"
946 +@@ -2058,12 +2173,92 @@ static void emitFieldFromInstruction(formatted_raw_ostream &OS) {
947 + << " else\n"
948 + << " fieldMask = (((InsnType)1 << numBits) - 1) << startBit;\n"
949 + << " return (insn & fieldMask) >> startBit;\n"
950 ++#endif
951 + << "}\n\n";
952 + }
953 +
954 + // emitDecodeInstruction - Emit the templated helper function
955 + // decodeInstruction().
956 + static void emitDecodeInstruction(formatted_raw_ostream &OS) {
957 ++#ifdef CAPSTONE
958 ++ OS << "#define DecodeInstruction(fname, fieldname, decoder, InsnType) \\\n"
959 ++ << "static DecodeStatus fname(const uint8_t DecodeTable[], MCInst *MI, \\\n"
960 ++ << " InsnType insn, uint64_t Address, const MCRegisterInfo *MRI, int feature) \\\n"
961 ++ << "{ \\\n"
962 ++ << " uint64_t Bits = getFeatureBits(feature); \\\n"
963 ++ << " const uint8_t *Ptr = DecodeTable; \\\n"
964 ++ << " uint32_t CurFieldValue = 0, ExpectedValue; \\\n"
965 ++ << " DecodeStatus S = MCDisassembler_Success; \\\n"
966 ++ << " unsigned Start, Len, NumToSkip, PIdx, Opc, DecodeIdx; \\\n"
967 ++ << " InsnType Val, FieldValue, PositiveMask, NegativeMask; \\\n"
968 ++ << " bool Pred, Fail; \\\n"
969 ++ << " for (;;) { \\\n"
970 ++ << " switch (*Ptr) { \\\n"
971 ++ << " default: \\\n"
972 ++ << " return MCDisassembler_Fail; \\\n"
973 ++ << " case MCD_OPC_ExtractField: { \\\n"
974 ++ << " Start = *++Ptr; \\\n"
975 ++ << " Len = *++Ptr; \\\n"
976 ++ << " ++Ptr; \\\n"
977 ++ << " CurFieldValue = (uint32_t)fieldname(insn, Start, Len); \\\n"
978 ++ << " break; \\\n"
979 ++ << " } \\\n"
980 ++ << " case MCD_OPC_FilterValue: { \\\n"
981 ++ << " Val = (InsnType)decodeULEB128(++Ptr, &Len); \\\n"
982 ++ << " Ptr += Len; \\\n"
983 ++ << " NumToSkip = *Ptr++; \\\n"
984 ++ << " NumToSkip |= (*Ptr++) << 8; \\\n"
985 ++ << " if (Val != CurFieldValue) \\\n"
986 ++ << " Ptr += NumToSkip; \\\n"
987 ++ << " break; \\\n"
988 ++ << " } \\\n"
989 ++ << " case MCD_OPC_CheckField: { \\\n"
990 ++ << " Start = *++Ptr; \\\n"
991 ++ << " Len = *++Ptr; \\\n"
992 ++ << " FieldValue = fieldname(insn, Start, Len); \\\n"
993 ++ << " ExpectedValue = (uint32_t)decodeULEB128(++Ptr, &Len); \\\n"
994 ++ << " Ptr += Len; \\\n"
995 ++ << " NumToSkip = *Ptr++; \\\n"
996 ++ << " NumToSkip |= (*Ptr++) << 8; \\\n"
997 ++ << " if (ExpectedValue != FieldValue) \\\n"
998 ++ << " Ptr += NumToSkip; \\\n"
999 ++ << " break; \\\n"
1000 ++ << " } \\\n"
1001 ++ << " case MCD_OPC_CheckPredicate: { \\\n"
1002 ++ << " PIdx = (uint32_t)decodeULEB128(++Ptr, &Len); \\\n"
1003 ++ << " Ptr += Len; \\\n"
1004 ++ << " NumToSkip = *Ptr++; \\\n"
1005 ++ << " NumToSkip |= (*Ptr++) << 8; \\\n"
1006 ++ << " Pred = checkDecoderPredicate(PIdx, Bits); \\\n"
1007 ++ << " if (!Pred) \\\n"
1008 ++ << " Ptr += NumToSkip; \\\n"
1009 ++ << " (void)Pred; \\\n"
1010 ++ << " break; \\\n"
1011 ++ << " } \\\n"
1012 ++ << " case MCD_OPC_Decode: { \\\n"
1013 ++ << " Opc = (unsigned)decodeULEB128(++Ptr, &Len); \\\n"
1014 ++ << " Ptr += Len; \\\n"
1015 ++ << " DecodeIdx = (unsigned)decodeULEB128(Ptr, &Len); \\\n"
1016 ++ << " Ptr += Len; \\\n"
1017 ++ << " MCInst_setOpcode(MI, Opc); \\\n"
1018 ++ << " return decoder(S, DecodeIdx, insn, MI, Address, MRI); \\\n"
1019 ++ << " } \\\n"
1020 ++ << " case MCD_OPC_SoftFail: { \\\n"
1021 ++ << " PositiveMask = (InsnType)decodeULEB128(++Ptr, &Len); \\\n"
1022 ++ << " Ptr += Len; \\\n"
1023 ++ << " NegativeMask = (InsnType)decodeULEB128(Ptr, &Len); \\\n"
1024 ++ << " Ptr += Len; \\\n"
1025 ++ << " Fail = (insn & PositiveMask) || (~insn & NegativeMask); \\\n"
1026 ++ << " if (Fail) \\\n"
1027 ++ << " S = MCDisassembler_SoftFail; \\\n"
1028 ++ << " break; \\\n"
1029 ++ << " } \\\n"
1030 ++ << " case MCD_OPC_Fail: { \\\n"
1031 ++ << " return MCDisassembler_Fail; \\\n"
1032 ++ << " } \\\n"
1033 ++ << " } \\\n"
1034 ++ << " } \\\n"
1035 ++#else
1036 + OS << "template<typename InsnType>\n"
1037 + << "static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], "
1038 + "MCInst &MI,\n"
1039 +@@ -2240,12 +2435,18 @@ static void emitDecodeInstruction(formatted_raw_ostream &OS) {
1040 + << " }\n"
1041 + << " llvm_unreachable(\"bogosity detected in disassembler state "
1042 + "machine!\");\n"
1043 ++#endif
1044 + << "}\n\n";
1045 + }
1046 +
1047 + // Emits disassembler code for instruction decoding.
1048 + void FixedLenDecoderEmitter::run(raw_ostream &o) {
1049 + formatted_raw_ostream OS(o);
1050 ++#ifdef CAPSTONE
1051 ++ OS << "#include \"../../MCInst.h\"\n";
1052 ++ OS << "#include \"../../LEB128.h\"\n";
1053 ++ OS << "\n";
1054 ++#else
1055 + OS << "#include \"llvm/MC/MCInst.h\"\n";
1056 + OS << "#include \"llvm/Support/Debug.h\"\n";
1057 + OS << "#include \"llvm/Support/DataTypes.h\"\n";
1058 +@@ -2254,6 +2455,7 @@ void FixedLenDecoderEmitter::run(raw_ostream &o) {
1059 + OS << "#include <assert.h>\n";
1060 + OS << '\n';
1061 + OS << "namespace llvm {\n\n";
1062 ++#endif
1063 +
1064 + emitFieldFromInstruction(OS);
1065 +
1066 +@@ -2322,7 +2524,13 @@ void FixedLenDecoderEmitter::run(raw_ostream &o) {
1067 + // Emit the main entry point for the decoder, decodeInstruction().
1068 + emitDecodeInstruction(OS);
1069 +
1070 ++#ifdef CAPSTONE
1071 ++ OS << "FieldFromInstruction(fieldFromInstruction, uint64_t)\n";
1072 ++ OS << "DecodeToMCInst(decodeToMCInst, fieldFromInstruction, uint64_t)\n";
1073 ++ OS << "DecodeInstruction(decodeInstruction, fieldFromInstruction, decodeToMCInst, uint64_t)\n";
1074 ++#else
1075 + OS << "\n} // End llvm namespace\n";
1076 ++#endif
1077 + }
1078 +
1079 + namespace llvm {
1080 +--
1081 +2.19.1
1082 +
1083 diff --git a/llvm/0005-capstone-generate-GenAsmWriter.inc.patch b/llvm/0005-capstone-generate-GenAsmWriter.inc.patch
1084 new file mode 100644
1085 index 000000000..cd1353eb7
1086 --- /dev/null
1087 +++ b/llvm/0005-capstone-generate-GenAsmWriter.inc.patch
1088 @@ -0,0 +1,225 @@
1089 +From 5569e48b9cb34a33910e1e850fbfabc999f016a2 Mon Sep 17 00:00:00 2001
1090 +From: mephi42 <[email protected]>
1091 +Date: Tue, 7 Aug 2018 20:00:08 +0200
1092 +Subject: [PATCH 5/7] capstone: generate *GenAsmWriter.inc
1093 +
1094 +---
1095 + utils/TableGen/AsmWriterEmitter.cpp | 89 +++++++++++++++++++++++++++--
1096 + utils/TableGen/AsmWriterInst.cpp | 4 ++
1097 + 2 files changed, 87 insertions(+), 6 deletions(-)
1098 +
1099 +diff --git a/utils/TableGen/AsmWriterEmitter.cpp b/utils/TableGen/AsmWriterEmitter.cpp
1100 +index 3c4c9c8e5c6..133800d217c 100644
1101 +--- a/utils/TableGen/AsmWriterEmitter.cpp
1102 ++++ b/utils/TableGen/AsmWriterEmitter.cpp
1103 +@@ -272,16 +272,22 @@ static void UnescapeString(std::string &Str) {
1104 + /// clearing the Instructions vector.
1105 + void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
1106 + Record *AsmWriter = Target.getAsmWriter();
1107 ++#ifndef CAPSTONE
1108 + StringRef ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
1109 ++#endif
1110 + bool PassSubtarget = AsmWriter->getValueAsInt("PassSubtarget");
1111 +
1112 + O <<
1113 + "/// printInstruction - This method is automatically generated by tablegen\n"
1114 + "/// from the instruction set description.\n"
1115 ++#ifdef CAPSTONE
1116 ++ "static void printInstruction(MCInst *MI, SStream *O, MCRegisterInfo *MRI)\n{\n";
1117 ++#else
1118 + "void " << Target.getName() << ClassName
1119 + << "::printInstruction(const MCInst *MI, "
1120 + << (PassSubtarget ? "const MCSubtargetInfo &STI, " : "")
1121 + << "raw_ostream &O) {\n";
1122 ++#endif
1123 +
1124 + // Build an aggregate string, and build a table of offsets into it.
1125 + SequenceToOffsetTable<std::string> StringTable;
1126 +@@ -379,9 +385,16 @@ void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
1127 + }
1128 +
1129 + // Emit the string table itself.
1130 ++#ifdef CAPSTONE
1131 ++ O << "#ifndef CAPSTONE_DIET\n";
1132 ++#endif
1133 + O << " static const char AsmStrs[] = {\n";
1134 + StringTable.emit(O, printChar);
1135 +- O << " };\n\n";
1136 ++ O << " };\n"
1137 ++#ifdef CAPSTONE
1138 ++ << "#endif\n"
1139 ++#endif
1140 ++ << "\n";
1141 +
1142 + // Emit the lookup tables in pieces to minimize wasted bytes.
1143 + unsigned BytesNeeded = ((OpcodeInfoBits - BitsLeft) + 7) / 8;
1144 +@@ -409,21 +422,45 @@ void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
1145 + // If the total bits is more than 32-bits we need to use a 64-bit type.
1146 + if (BitsLeft < (OpcodeInfoBits - 32))
1147 + BitsOS << "(uint64_t)";
1148 +- BitsOS << "OpInfo" << Table << "[MI->getOpcode()] << " << Shift << ";\n";
1149 ++ BitsOS << "OpInfo" << Table << "["
1150 ++#ifdef CAPSTONE
1151 ++ << "MCInst_getOpcode(MI)"
1152 ++#else
1153 ++ << "MI->getOpcode()"
1154 ++#endif
1155 ++ << "] << " << Shift << ";\n";
1156 + // Prepare the shift for the next iteration and increment the table count.
1157 + Shift += TableSize;
1158 + ++Table;
1159 + }
1160 +
1161 + // Emit the initial tab character.
1162 ++#ifndef CAPSTONE
1163 + O << " O << \"\\t\";\n\n";
1164 ++#endif
1165 +
1166 + O << " // Emit the opcode for the instruction.\n";
1167 + O << BitsString;
1168 +
1169 + // Emit the starting string.
1170 +- O << " assert(Bits != 0 && \"Cannot print this instruction.\");\n"
1171 +- << " O << AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << ")-1;\n\n";
1172 ++ O << " "
1173 ++#ifdef CAPSTONE
1174 ++ << "// "
1175 ++#endif
1176 ++ << "assert(Bits != 0 && \"Cannot print this instruction.\");\n"
1177 ++#ifdef CAPSTONE
1178 ++ << "#ifndef CAPSTONE_DIET\n"
1179 ++ << " SStream_concat0(O, "
1180 ++#else
1181 ++ << " O << "
1182 ++#endif
1183 ++ << "AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << ")-1"
1184 ++#ifdef CAPSTONE
1185 ++ << ");\n"
1186 ++ << "#endif\n\n";
1187 ++#else
1188 ++ << ");\n\n";
1189 ++#endif
1190 +
1191 + // Output the table driven operand information.
1192 + BitsLeft = OpcodeInfoBits-AsmStrBits;
1193 +@@ -455,7 +492,11 @@ void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
1194 + O << " switch ((Bits >> "
1195 + << (OpcodeInfoBits-BitsLeft) << ") & "
1196 + << ((1 << NumBits)-1) << ") {\n"
1197 +- << " default: llvm_unreachable(\"Invalid command number.\");\n";
1198 ++ << " default: "
1199 ++#ifdef CAPSTONE
1200 ++ << "// "
1201 ++#endif
1202 ++ << "llvm_unreachable(\"Invalid command number.\");\n";
1203 +
1204 + // Print out all the cases.
1205 + for (unsigned j = 0, e = Commands.size(); j != e; ++j) {
1206 +@@ -536,6 +577,9 @@ emitRegisterNameString(raw_ostream &O, StringRef AltName,
1207 + }
1208 +
1209 + StringTable.layout();
1210 ++#ifdef CAPSTONE
1211 ++ O << "#ifndef CAPSTONE_DIET\n";
1212 ++#endif
1213 + O << " static const char AsmStrs" << AltName << "[] = {\n";
1214 + StringTable.emit(O, printChar);
1215 + O << " };\n\n";
1216 +@@ -552,8 +596,10 @@ emitRegisterNameString(raw_ostream &O, StringRef AltName,
1217 + }
1218 +
1219 + void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
1220 ++#ifndef CAPSTONE
1221 + Record *AsmWriter = Target.getAsmWriter();
1222 + StringRef ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
1223 ++#endif
1224 + const auto &Registers = Target.getRegBank().getRegisters();
1225 + const std::vector<Record*> &AltNameIndices = Target.getRegAltNameIndices();
1226 + bool hasAltNames = AltNameIndices.size() > 1;
1227 +@@ -563,12 +609,20 @@ void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
1228 + "\n\n/// getRegisterName - This method is automatically generated by tblgen\n"
1229 + "/// from the register set description. This returns the assembler name\n"
1230 + "/// for the specified register.\n"
1231 ++#ifdef CAPSTONE
1232 ++ "static const char *getRegisterName(unsigned RegNo)\n{\n";
1233 ++#else
1234 + "const char *" << Target.getName() << ClassName << "::";
1235 + if (hasAltNames)
1236 + O << "\ngetRegisterName(unsigned RegNo, unsigned AltIdx) {\n";
1237 + else
1238 + O << "getRegisterName(unsigned RegNo) {\n";
1239 +- O << " assert(RegNo && RegNo < " << (Registers.size()+1)
1240 ++#endif
1241 ++ O << " "
1242 ++#ifdef CAPSTONE
1243 ++ << "// "
1244 ++#endif
1245 ++ << "assert(RegNo && RegNo < " << (Registers.size()+1)
1246 + << " && \"Invalid register number!\");\n"
1247 + << "\n";
1248 +
1249 +@@ -595,10 +649,22 @@ void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
1250 + }
1251 + O << " }\n";
1252 + } else {
1253 ++#ifdef CAPSTONE
1254 ++ O << " //int i;\n"
1255 ++ << " //for (i = 0; i < sizeof(RegAsmOffset); i++)\n"
1256 ++ << " // printf(\"%s = %u\\n\", AsmStrs+RegAsmOffset[i], i + 1);\n"
1257 ++ << " //printf(\"*************************\\n\");\n"
1258 ++#else
1259 + O << " assert (*(AsmStrs+RegAsmOffset[RegNo-1]) &&\n"
1260 + << " \"Invalid alt name index for register!\");\n"
1261 ++#endif
1262 + << " return AsmStrs+RegAsmOffset[RegNo-1];\n";
1263 + }
1264 ++#ifdef CAPSTONE
1265 ++ O << "#else\n"
1266 ++ << " return NULL;\n"
1267 ++ << "#endif\n";
1268 ++#endif
1269 + O << "}\n";
1270 + }
1271 +
1272 +@@ -1135,9 +1201,20 @@ AsmWriterEmitter::AsmWriterEmitter(RecordKeeper &R) : Records(R), Target(R) {
1273 + }
1274 +
1275 + void AsmWriterEmitter::run(raw_ostream &O) {
1276 ++#ifdef CAPSTONE
1277 ++ O << "/* Capstone Disassembly Engine */\n"
1278 ++ "/* By Nguyen Anh Quynh <[email protected]>, 2013-2015 */\n"
1279 ++ "\n"
1280 ++ "#include <stdio.h>\t// debug\n"
1281 ++ "#include <capstone/platform.h>\n"
1282 ++ "\n"
1283 ++ "\n";
1284 ++#endif
1285 + EmitPrintInstruction(O);
1286 + EmitGetRegisterName(O);
1287 ++#ifndef CAPSTONE
1288 + EmitPrintAliasInstruction(O);
1289 ++#endif
1290 + }
1291 +
1292 + namespace llvm {
1293 +diff --git a/utils/TableGen/AsmWriterInst.cpp b/utils/TableGen/AsmWriterInst.cpp
1294 +index 2c19e5d663d..6fa751e50df 100644
1295 +--- a/utils/TableGen/AsmWriterInst.cpp
1296 ++++ b/utils/TableGen/AsmWriterInst.cpp
1297 +@@ -28,9 +28,13 @@ static bool isIdentChar(char C) {
1298 +
1299 + std::string AsmWriterOperand::getCode(bool PassSubtarget) const {
1300 + if (OperandType == isLiteralTextOperand) {
1301 ++#ifdef CAPSTONE
1302 ++ return "SStream_concat0(O, \"" + Str + "\");";
1303 ++#else
1304 + if (Str.size() == 1)
1305 + return "O << '" + Str + "';";
1306 + return "O << \"" + Str + "\";";
1307 ++#endif
1308 + }
1309 +
1310 + if (OperandType == isLiteralStatementOperand)
1311 +--
1312 +2.19.1
1313 +
1314 diff --git a/llvm/0006-capstone-generate-MappingInsn.inc.patch b/llvm/0006-capstone-generate-MappingInsn.inc.patch
1315 new file mode 100644
1316 index 000000000..7ee22d787
1317 --- /dev/null
1318 +++ b/llvm/0006-capstone-generate-MappingInsn.inc.patch
1319 @@ -0,0 +1,174 @@
1320 +From 7a436110ef15c803dc8524af2fb5612bcacbb126 Mon Sep 17 00:00:00 2001
1321 +From: mephi42 <[email protected]>
1322 +Date: Tue, 7 Aug 2018 20:55:32 +0200
1323 +Subject: [PATCH 6/7] capstone: generate *MappingInsn.inc
1324 +
1325 +---
1326 + lib/Target/SystemZ/CMakeLists.txt | 1 +
1327 + utils/TableGen/InstrInfoEmitter.cpp | 95 +++++++++++++++++++++++++++++
1328 + utils/TableGen/TableGen.cpp | 6 ++
1329 + utils/TableGen/TableGenBackends.h | 1 +
1330 + 4 files changed, 103 insertions(+)
1331 +
1332 +diff --git a/lib/Target/SystemZ/CMakeLists.txt b/lib/Target/SystemZ/CMakeLists.txt
1333 +index f83b4242fb4..4b5d9c4a3b2 100644
1334 +--- a/lib/Target/SystemZ/CMakeLists.txt
1335 ++++ b/lib/Target/SystemZ/CMakeLists.txt
1336 +@@ -6,6 +6,7 @@ tablegen(LLVM SystemZGenCallingConv.inc -gen-callingconv)
1337 + tablegen(LLVM SystemZGenDAGISel.inc -gen-dag-isel)
1338 + tablegen(LLVM SystemZGenDisassemblerTables.inc -gen-disassembler)
1339 + tablegen(LLVM SystemZGenInstrInfo.inc -gen-instr-info)
1340 ++tablegen(LLVM SystemZMappingInsn.inc -mapping-insn)
1341 + tablegen(LLVM SystemZGenMCCodeEmitter.inc -gen-emitter)
1342 + tablegen(LLVM SystemZGenRegisterInfo.inc -gen-register-info)
1343 + tablegen(LLVM SystemZGenSubtargetInfo.inc -gen-subtarget)
1344 +diff --git a/utils/TableGen/InstrInfoEmitter.cpp b/utils/TableGen/InstrInfoEmitter.cpp
1345 +index 2f3a2729262..14ab1ea8a72 100644
1346 +--- a/utils/TableGen/InstrInfoEmitter.cpp
1347 ++++ b/utils/TableGen/InstrInfoEmitter.cpp
1348 +@@ -744,4 +744,99 @@ void EmitInstrInfo(RecordKeeper &RK, raw_ostream &OS) {
1349 + #endif
1350 + }
1351 +
1352 ++#ifdef CAPSTONE
1353 ++std::string GetPublicName(const CodeGenInstruction *Inst) {
1354 ++ std::string Name = Inst->TheDef->getName();
1355 ++ // Apply backward compatibility fixups.
1356 ++ // BRNLE -> BNLER.
1357 ++ if (Name.length() >= 5 && Name.substr(0, 5) == "BRAsm") {
1358 ++ Name = "B" + Name.substr(5, Name.length() - 5) + "R";
1359 ++ }
1360 ++ // SSKEOpt -> SSKE.
1361 ++ while (Name.length() >= 3 && Name.substr(Name.length() - 3, 3) == "Opt") {
1362 ++ Name = Name.substr(0, Name.length() - 3);
1363 ++ }
1364 ++ // BRCLAsm -> BRCL.
1365 ++ while (true) {
1366 ++ size_t pos = Name.find("Asm");
1367 ++ if (pos == std::string::npos) {
1368 ++ break;
1369 ++ }
1370 ++ Name = Name.substr(0, pos) + Name.substr(pos + 3);
1371 ++ }
1372 ++ // CPSDRxx -> CPSDR.
1373 ++ if (Name.length() >= 2) {
1374 ++ std::string Suffix2 = Name.substr(Name.length() - 2, 2);
1375 ++ if (Suffix2 == "dd" || Suffix2 == "ds" ||
1376 ++ Suffix2 == "sd" || Suffix2 == "ss") {
1377 ++ Name = Name.substr(0, Name.length() - 2);
1378 ++ }
1379 ++ }
1380 ++ return "SYSZ_INS_" + Name;
1381 ++}
1382 ++
1383 ++std::string GetRegisterName(Record *Reg) {
1384 ++ std::string Name = Reg->getName();
1385 ++ for (char& c : Name) {
1386 ++ c = toupper(c);
1387 ++ }
1388 ++ // R0L, R0D -> R0.
1389 ++ if (Name.length() >= 3 &&
1390 ++ Name[Name.length() - 3] == 'R' &&
1391 ++ (Name[Name.length() - 1] == 'L' ||
1392 ++ Name[Name.length() - 1] == 'D')) {
1393 ++ Name = Name.substr(0, Name.length() - 3) + Name[Name.length() - 2];
1394 ++ }
1395 ++ return "SYSZ_REG_" + Name;
1396 ++}
1397 ++
1398 ++std::string GetGroupName(Record *Pred) {
1399 ++ std::string Name = Pred->getName();
1400 ++ for (char& c : Name) {
1401 ++ c = toupper(c);
1402 ++ }
1403 ++ if (Name.length() >= 7 && Name.substr(0, 7) == "FEATURE") {
1404 ++ Name = Name.substr(7);
1405 ++ }
1406 ++ return "SYSZ_GRP_" + Name;
1407 ++}
1408 ++
1409 ++void EmitMappingInsn(RecordKeeper &RK, raw_ostream &OS) {
1410 ++ OS << "// This is auto-gen data for Capstone engine (www.capstone-engine.org)\n"
1411 ++ "// By Nguyen Anh Quynh <[email protected]>\n"
1412 ++ "\n";
1413 ++ CodeGenTarget Target(RK);
1414 ++ for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
1415 ++ if (Inst->TheDef->getValueAsBit("isPseudo") ||
1416 ++ Inst->TheDef->getValueAsBit("isCodeGenOnly")) {
1417 ++ continue;
1418 ++ }
1419 ++ OS << "{\n"
1420 ++ << "\t" << Target.getName() << "_" << Inst->TheDef->getName() << ", "
1421 ++ << GetPublicName(Inst) << ",\n"
1422 ++ << "#ifndef CAPSTONE_DIET\n"
1423 ++ << "\t{ ";
1424 ++ for (Record *Use : Inst->TheDef->getValueAsListOfDefs("Uses")) {
1425 ++ OS << GetRegisterName(Use) << ", ";
1426 ++ }
1427 ++ OS << "0 }, { ";
1428 ++ for (Record *Def : Inst->TheDef->getValueAsListOfDefs("Defs")) {
1429 ++ OS << GetRegisterName(Def) << ", ";
1430 ++ }
1431 ++ OS << "0 }, { ";
1432 ++ ListInit *Predicates = Inst->TheDef->getValueAsListInit("Predicates");
1433 ++ for (unsigned i = 0; i < Predicates->size(); ++i) {
1434 ++ OS << GetGroupName(Predicates->getElementAsRecord(i)) << ", ";
1435 ++ }
1436 ++ OS << "0 }, "
1437 ++ << Inst->TheDef->getValueAsBit("isBranch")
1438 ++ << ", "
1439 ++ << Inst->TheDef->getValueAsBit("isIndirectBranch")
1440 ++ << "\n"
1441 ++ << "#endif\n"
1442 ++ << "},\n";
1443 ++ }
1444 ++}
1445 ++#endif
1446 ++
1447 + } // end llvm namespace
1448 +diff --git a/utils/TableGen/TableGen.cpp b/utils/TableGen/TableGen.cpp
1449 +index cf1404d8769..bbb4e860536 100644
1450 +--- a/utils/TableGen/TableGen.cpp
1451 ++++ b/utils/TableGen/TableGen.cpp
1452 +@@ -27,6 +27,7 @@ enum ActionType {
1453 + GenEmitter,
1454 + GenRegisterInfo,
1455 + GenInstrInfo,
1456 ++ MappingInsn,
1457 + GenInstrDocs,
1458 + GenAsmWriter,
1459 + GenAsmMatcher,
1460 +@@ -65,6 +66,8 @@ namespace {
1461 + "Generate registers and register classes info"),
1462 + clEnumValN(GenInstrInfo, "gen-instr-info",
1463 + "Generate instruction descriptions"),
1464 ++ clEnumValN(MappingInsn, "mapping-insn",
1465 ++ ""),
1466 + clEnumValN(GenInstrDocs, "gen-instr-docs",
1467 + "Generate instruction documentation"),
1468 + clEnumValN(GenCallingConv, "gen-callingconv",
1469 +@@ -135,6 +138,9 @@ bool LLVMTableGenMain(raw_ostream &OS, RecordKeeper &Records) {
1470 + case GenInstrInfo:
1471 + EmitInstrInfo(Records, OS);
1472 + break;
1473 ++ case MappingInsn:
1474 ++ EmitMappingInsn(Records, OS);
1475 ++ break;
1476 + case GenInstrDocs:
1477 + EmitInstrDocs(Records, OS);
1478 + break;
1479 +diff --git a/utils/TableGen/TableGenBackends.h b/utils/TableGen/TableGenBackends.h
1480 +index 1329a6d833f..a41e46b1db0 100644
1481 +--- a/utils/TableGen/TableGenBackends.h
1482 ++++ b/utils/TableGen/TableGenBackends.h
1483 +@@ -75,6 +75,7 @@ void EmitDFAPacketizer(RecordKeeper &RK, raw_ostream &OS);
1484 + void EmitDisassembler(RecordKeeper &RK, raw_ostream &OS);
1485 + void EmitFastISel(RecordKeeper &RK, raw_ostream &OS);
1486 + void EmitInstrInfo(RecordKeeper &RK, raw_ostream &OS);
1487 ++void EmitMappingInsn(RecordKeeper &RK, raw_ostream &OS);
1488 + void EmitInstrDocs(RecordKeeper &RK, raw_ostream &OS);
1489 + void EmitPseudoLowering(RecordKeeper &RK, raw_ostream &OS);
1490 + void EmitCompressInst(RecordKeeper &RK, raw_ostream &OS);
1491 +--
1492 +2.19.1
1493 +
1494 diff --git a/llvm/0007-capstone-generate-GenInsnNameMaps.inc.patch b/llvm/0007-capstone-generate-GenInsnNameMaps.inc.patch
1495 new file mode 100644
1496 index 000000000..019540d65
1497 --- /dev/null
1498 +++ b/llvm/0007-capstone-generate-GenInsnNameMaps.inc.patch
1499 @@ -0,0 +1,110 @@
1500 +From b42f9f2014ec49a22077b6610863d9341a74e142 Mon Sep 17 00:00:00 2001
1501 +From: mephi42 <[email protected]>
1502 +Date: Fri, 17 Aug 2018 11:07:39 +0200
1503 +Subject: [PATCH 7/7] capstone: generate *GenInsnNameMaps.inc
1504 +
1505 +---
1506 + lib/Target/SystemZ/CMakeLists.txt | 1 +
1507 + utils/TableGen/InstrInfoEmitter.cpp | 29 +++++++++++++++++++++++++++++
1508 + utils/TableGen/TableGen.cpp | 6 ++++++
1509 + utils/TableGen/TableGenBackends.h | 1 +
1510 + 4 files changed, 37 insertions(+)
1511 +
1512 +diff --git a/lib/Target/SystemZ/CMakeLists.txt b/lib/Target/SystemZ/CMakeLists.txt
1513 +index 4b5d9c4a3b2..2c64e0a94b8 100644
1514 +--- a/lib/Target/SystemZ/CMakeLists.txt
1515 ++++ b/lib/Target/SystemZ/CMakeLists.txt
1516 +@@ -7,6 +7,7 @@ tablegen(LLVM SystemZGenDAGISel.inc -gen-dag-isel)
1517 + tablegen(LLVM SystemZGenDisassemblerTables.inc -gen-disassembler)
1518 + tablegen(LLVM SystemZGenInstrInfo.inc -gen-instr-info)
1519 + tablegen(LLVM SystemZMappingInsn.inc -mapping-insn)
1520 ++tablegen(LLVM SystemZGenInsnNameMaps.inc -gen-insn-name-maps)
1521 + tablegen(LLVM SystemZGenMCCodeEmitter.inc -gen-emitter)
1522 + tablegen(LLVM SystemZGenRegisterInfo.inc -gen-register-info)
1523 + tablegen(LLVM SystemZGenSubtargetInfo.inc -gen-subtarget)
1524 +diff --git a/utils/TableGen/InstrInfoEmitter.cpp b/utils/TableGen/InstrInfoEmitter.cpp
1525 +index 14ab1ea8a72..ccf8170ca62 100644
1526 +--- a/utils/TableGen/InstrInfoEmitter.cpp
1527 ++++ b/utils/TableGen/InstrInfoEmitter.cpp
1528 +@@ -837,6 +837,35 @@ void EmitMappingInsn(RecordKeeper &RK, raw_ostream &OS) {
1529 + << "},\n";
1530 + }
1531 + }
1532 ++
1533 ++std::string GetMnemonic(const CodeGenInstruction *Inst) {
1534 ++ std::string Mnemonic = Inst->AsmString;
1535 ++
1536 ++ for (size_t i = 0; i < Mnemonic.length(); i++) {
1537 ++ if (Mnemonic[i] == '\t') {
1538 ++ return Mnemonic.substr(0, i);
1539 ++ }
1540 ++ }
1541 ++ return Mnemonic;
1542 ++}
1543 ++
1544 ++void EmitInsnNameMaps(RecordKeeper &RK, raw_ostream &OS) {
1545 ++ OS << "// This is auto-gen data for Capstone engine (www.capstone-engine.org)\n"
1546 ++ "// By Nguyen Anh Quynh <[email protected]>\n"
1547 ++ "\n";
1548 ++ CodeGenTarget Target(RK);
1549 ++ std::map<std::string, std::string> M;
1550 ++ for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
1551 ++ if (Inst->TheDef->getValueAsBit("isPseudo") ||
1552 ++ Inst->TheDef->getValueAsBit("isCodeGenOnly")) {
1553 ++ continue;
1554 ++ }
1555 ++ M[GetPublicName(Inst)] = GetMnemonic(Inst);
1556 ++ }
1557 ++ for (auto &P : M) {
1558 ++ OS << "\t{ " << P.first << ", \"" << P.second << "\" },\n";
1559 ++ }
1560 ++}
1561 + #endif
1562 +
1563 + } // end llvm namespace
1564 +diff --git a/utils/TableGen/TableGen.cpp b/utils/TableGen/TableGen.cpp
1565 +index bbb4e860536..27c6603de5a 100644
1566 +--- a/utils/TableGen/TableGen.cpp
1567 ++++ b/utils/TableGen/TableGen.cpp
1568 +@@ -28,6 +28,7 @@ enum ActionType {
1569 + GenRegisterInfo,
1570 + GenInstrInfo,
1571 + MappingInsn,
1572 ++ GenInsnNameMaps,
1573 + GenInstrDocs,
1574 + GenAsmWriter,
1575 + GenAsmMatcher,
1576 +@@ -68,6 +69,8 @@ namespace {
1577 + "Generate instruction descriptions"),
1578 + clEnumValN(MappingInsn, "mapping-insn",
1579 + ""),
1580 ++ clEnumValN(GenInsnNameMaps, "gen-insn-name-maps",
1581 ++ ""),
1582 + clEnumValN(GenInstrDocs, "gen-instr-docs",
1583 + "Generate instruction documentation"),
1584 + clEnumValN(GenCallingConv, "gen-callingconv",
1585 +@@ -141,6 +144,9 @@ bool LLVMTableGenMain(raw_ostream &OS, RecordKeeper &Records) {
1586 + case MappingInsn:
1587 + EmitMappingInsn(Records, OS);
1588 + break;
1589 ++ case GenInsnNameMaps:
1590 ++ EmitInsnNameMaps(Records, OS);
1591 ++ break;
1592 + case GenInstrDocs:
1593 + EmitInstrDocs(Records, OS);
1594 + break;
1595 +diff --git a/utils/TableGen/TableGenBackends.h b/utils/TableGen/TableGenBackends.h
1596 +index a41e46b1db0..5656e5be849 100644
1597 +--- a/utils/TableGen/TableGenBackends.h
1598 ++++ b/utils/TableGen/TableGenBackends.h
1599 +@@ -76,6 +76,7 @@ void EmitDisassembler(RecordKeeper &RK, raw_ostream &OS);
1600 + void EmitFastISel(RecordKeeper &RK, raw_ostream &OS);
1601 + void EmitInstrInfo(RecordKeeper &RK, raw_ostream &OS);
1602 + void EmitMappingInsn(RecordKeeper &RK, raw_ostream &OS);
1603 ++void EmitInsnNameMaps(RecordKeeper &RK, raw_ostream &OS);
1604 + void EmitInstrDocs(RecordKeeper &RK, raw_ostream &OS);
1605 + void EmitPseudoLowering(RecordKeeper &RK, raw_ostream &OS);
1606 + void EmitCompressInst(RecordKeeper &RK, raw_ostream &OS);
1607 +--
1608 +2.19.1
1609 +
1610 diff --git a/llvm/lib/Target/RISCV/CMakeLists.txt b/llvm/lib/Target/RISCV/CMakeLists.txt
1611 index 07c32cb31..1821f4b01 100644
1612 --- a/llvm/lib/Target/RISCV/CMakeLists.txt
1613 +++ b/llvm/lib/Target/RISCV/CMakeLists.txt
1614 @@ -6,6 +6,8 @@ tablegen(LLVM RISCVGenCompressInstEmitter.inc -gen-compress-inst-emitter)
1615 tablegen(LLVM RISCVGenDAGISel.inc -gen-dag-isel)
1616 tablegen(LLVM RISCVGenDisassemblerTables.inc -gen-disassembler)
1617 tablegen(LLVM RISCVGenInstrInfo.inc -gen-instr-info)
1618 +tablegen(LLVM RISCVGenMappingInsn.inc -gen-mapping-insn)
1619 +tablegen(LLVM RISCVGenInsnNameMaps.inc -gen-insn-name-maps)
1620 tablegen(LLVM RISCVGenMCCodeEmitter.inc -gen-emitter)
1621 tablegen(LLVM RISCVGenMCPseudoLowering.inc -gen-pseudo-lowering)
1622 tablegen(LLVM RISCVGenRegisterInfo.inc -gen-register-info)
1623 diff --git a/llvm/utils/TableGen/AsmWriterEmitter.cpp b/llvm/utils/TableGen/AsmWriterEmitter.cpp
1624 index 24e16f9c4..c24dc6052 100644
1625 --- a/llvm/utils/TableGen/AsmWriterEmitter.cpp
1626 +++ b/llvm/utils/TableGen/AsmWriterEmitter.cpp
1627 @@ -271,16 +271,22 @@ static void UnescapeString(std::string &Str) {
1628 /// clearing the Instructions vector.
1629 void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
1630 Record *AsmWriter = Target.getAsmWriter();
1631 +#ifndef CAPSTONE
1632 StringRef ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
1633 +#endif
1634 bool PassSubtarget = AsmWriter->getValueAsInt("PassSubtarget");
1635
1636 O <<
1637 "/// printInstruction - This method is automatically generated by tablegen\n"
1638 "/// from the instruction set description.\n"
1639 +#ifdef CAPSTONE
1640 + "static void printInstruction(MCInst *MI, SStream *O, MCRegisterInfo *MRI)\n{\n";
1641 +#else
1642 "void " << Target.getName() << ClassName
1643 << "::printInstruction(const MCInst *MI, "
1644 << (PassSubtarget ? "const MCSubtargetInfo &STI, " : "")
1645 << "raw_ostream &O) {\n";
1646 +#endif
1647
1648 // Build an aggregate string, and build a table of offsets into it.
1649 SequenceToOffsetTable<std::string> StringTable;
1650 @@ -378,9 +384,16 @@ void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
1651 }
1652
1653 // Emit the string table itself.
1654 +#ifdef CAPSTONE
1655 + O << "#ifndef CAPSTONE_DIET\n";
1656 +#endif
1657 O << " static const char AsmStrs[] = {\n";
1658 StringTable.emit(O, printChar);
1659 - O << " };\n\n";
1660 + O << " };\n"
1661 +#ifdef CAPSTONE
1662 + << "#endif\n"
1663 +#endif
1664 + << "\n";
1665
1666 // Emit the lookup tables in pieces to minimize wasted bytes.
1667 unsigned BytesNeeded = ((OpcodeInfoBits - BitsLeft) + 7) / 8;
1668 @@ -408,21 +421,45 @@ void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
1669 // If the total bits is more than 32-bits we need to use a 64-bit type.
1670 if (BitsLeft < (OpcodeInfoBits - 32))
1671 BitsOS << "(uint64_t)";
1672 - BitsOS << "OpInfo" << Table << "[MI->getOpcode()] << " << Shift << ";\n";
1673 + BitsOS << "OpInfo" << Table << "["
1674 +#ifdef CAPSTONE
1675 + << "MCInst_getOpcode(MI)"
1676 +#else
1677 + << "MI->getOpcode()"
1678 +#endif
1679 + << "] << " << Shift << ";\n";
1680 // Prepare the shift for the next iteration and increment the table count.
1681 Shift += TableSize;
1682 ++Table;
1683 }
1684
1685 // Emit the initial tab character.
1686 +#ifndef CAPSTONE
1687 O << " O << \"\\t\";\n\n";
1688 +#endif
1689
1690 O << " // Emit the opcode for the instruction.\n";
1691 O << BitsString;
1692
1693 // Emit the starting string.
1694 - O << " assert(Bits != 0 && \"Cannot print this instruction.\");\n"
1695 - << " O << AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << ")-1;\n\n";
1696 + O << " "
1697 +#ifdef CAPSTONE
1698 + << "// "
1699 +#endif
1700 + << "assert(Bits != 0 && \"Cannot print this instruction.\");\n"
1701 +#ifdef CAPSTONE
1702 + << "#ifndef CAPSTONE_DIET\n"
1703 + << " SStream_concat0(O, "
1704 +#else
1705 + << " O << "
1706 +#endif
1707 + << "AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << ")-1"
1708 +#ifdef CAPSTONE
1709 + << ");\n"
1710 + << "#endif\n\n";
1711 +#else
1712 + << ");\n\n";
1713 +#endif
1714
1715 // Output the table driven operand information.
1716 BitsLeft = OpcodeInfoBits-AsmStrBits;
1717 @@ -454,7 +491,11 @@ void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
1718 O << " switch ((Bits >> "
1719 << (OpcodeInfoBits-BitsLeft) << ") & "
1720 << ((1 << NumBits)-1) << ") {\n"
1721 - << " default: llvm_unreachable(\"Invalid command number.\");\n";
1722 + << " default: "
1723 +#ifdef CAPSTONE
1724 + << "assert(0);\n"
1725 +#endif
1726 + << "llvm_unreachable(\"Invalid command number.\");\n";
1727
1728 // Print out all the cases.
1729 for (unsigned j = 0, e = Commands.size(); j != e; ++j) {
1730 @@ -535,6 +576,9 @@ emitRegisterNameString(raw_ostream &O, StringRef AltName,
1731 }
1732
1733 StringTable.layout();
1734 +#ifdef CAPSTONE
1735 + O << "#ifndef CAPSTONE_DIET\n";
1736 +#endif
1737 O << " static const char AsmStrs" << AltName << "[] = {\n";
1738 StringTable.emit(O, printChar);
1739 O << " };\n\n";
1740 @@ -551,8 +595,10 @@ emitRegisterNameString(raw_ostream &O, StringRef AltName,
1741 }
1742
1743 void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
1744 +#ifndef CAPSTONE
1745 Record *AsmWriter = Target.getAsmWriter();
1746 StringRef ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
1747 +#endif
1748 const auto &Registers = Target.getRegBank().getRegisters();
1749 const std::vector<Record*> &AltNameIndices = Target.getRegAltNameIndices();
1750 bool hasAltNames = AltNameIndices.size() > 1;
1751 @@ -562,12 +608,22 @@ void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
1752 "\n\n/// getRegisterName - This method is automatically generated by tblgen\n"
1753 "/// from the register set description. This returns the assembler name\n"
1754 "/// for the specified register.\n"
1755 +#ifdef CAPSTONE
1756 + //"static const char *getRegisterName(unsigned RegNo)\n{\n";
1757 + "static const char *";
1758 + if (hasAltNames)
1759 + O << "\ngetRegisterName(unsigned RegNo, unsigned AltIdx)\n{\n";
1760 + else
1761 + O << "\ngetRegisterName(unsigned RegNo)\n{\n";
1762 +#else
1763 "const char *" << Target.getName() << ClassName << "::";
1764 if (hasAltNames)
1765 O << "\ngetRegisterName(unsigned RegNo, unsigned AltIdx) {\n";
1766 else
1767 O << "getRegisterName(unsigned RegNo) {\n";
1768 - O << " assert(RegNo && RegNo < " << (Registers.size()+1)
1769 +#endif
1770 + O << " "
1771 + << "assert(RegNo && RegNo < " << (Registers.size()+1)
1772 << " && \"Invalid register number!\");\n"
1773 << "\n";
1774
1775 @@ -579,12 +635,21 @@ void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
1776
1777 if (hasAltNames) {
1778 O << " switch(AltIdx) {\n"
1779 +#ifdef CAPSTONE
1780 + << " default: assert(0);\n";
1781 +#else
1782 << " default: llvm_unreachable(\"Invalid register alt name index!\");\n";
1783 +#endif
1784 for (const Record *R : AltNameIndices) {
1785 StringRef AltName = R->getName();
1786 O << " case ";
1787 if (!Namespace.empty())
1788 - O << Namespace << "::";
1789 + O << Namespace
1790 +#ifndef CAPSTONE
1791 + << "::";
1792 +#else
1793 + << "_";
1794 +#endif
1795 O << AltName << ":\n"
1796 << " assert(*(AsmStrs" << AltName << "+RegAsmOffset" << AltName
1797 << "[RegNo-1]) &&\n"
1798 @@ -594,10 +659,22 @@ void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
1799 }
1800 O << " }\n";
1801 } else {
1802 +#ifdef CAPSTONE
1803 + O << " //int i;\n"
1804 + << " //for (i = 0; i < sizeof(RegAsmOffset); i++)\n"
1805 + << " // printf(\"%s = %u\\n\", AsmStrs+RegAsmOffset[i], i + 1);\n"
1806 + << " //printf(\"*************************\\n\");\n"
1807 +#else
1808 O << " assert (*(AsmStrs+RegAsmOffset[RegNo-1]) &&\n"
1809 << " \"Invalid alt name index for register!\");\n"
1810 +#endif
1811 << " return AsmStrs+RegAsmOffset[RegNo-1];\n";
1812 }
1813 +#ifdef CAPSTONE
1814 + O << "#else\n"
1815 + << " return NULL;\n"
1816 + << "#endif\n";
1817 +#endif
1818 O << "}\n";
1819 }
1820
1821 @@ -1139,9 +1216,21 @@ AsmWriterEmitter::AsmWriterEmitter(RecordKeeper &R) : Records(R), Target(R) {
1822 }
1823
1824 void AsmWriterEmitter::run(raw_ostream &O) {
1825 +#ifdef CAPSTONE
1826 + O << "/* Capstone Disassembly Engine */\n"
1827 + "/* By Nguyen Anh Quynh <[email protected]>, 2013-2015 */\n"
1828 + "\n"
1829 + "#include <stdio.h>\t// debug\n"
1830 + "#include <capstone/platform.h>\n"
1831 + "#include <assert.h>\n"
1832 + "\n"
1833 + "\n";
1834 +#endif
1835 EmitPrintInstruction(O);
1836 EmitGetRegisterName(O);
1837 +#ifndef CAPSTONE
1838 EmitPrintAliasInstruction(O);
1839 +#endif
1840 }
1841
1842 namespace llvm {
1843 diff --git a/llvm/utils/TableGen/AsmWriterInst.cpp b/llvm/utils/TableGen/AsmWriterInst.cpp
1844 index d065a4209..7266d1eda 100644
1845 --- a/llvm/utils/TableGen/AsmWriterInst.cpp
1846 +++ b/llvm/utils/TableGen/AsmWriterInst.cpp
1847 @@ -27,9 +27,13 @@ static bool isIdentChar(char C) {
1848
1849 std::string AsmWriterOperand::getCode(bool PassSubtarget) const {
1850 if (OperandType == isLiteralTextOperand) {
1851 +#ifdef CAPSTONE
1852 + return "SStream_concat0(O, \"" + Str + "\");";
1853 +#else
1854 if (Str.size() == 1)
1855 return "O << '" + Str + "';";
1856 return "O << \"" + Str + "\";";
1857 +#endif
1858 }
1859
1860 if (OperandType == isLiteralStatementOperand)
1861 diff --git a/llvm/utils/TableGen/DisassemblerEmitter.cpp b/llvm/utils/TableGen/DisassemblerEmitter.cpp
1862 index 9e75c7fba..1bc0cfa0d 100644
1863 --- a/llvm/utils/TableGen/DisassemblerEmitter.cpp
1864 +++ b/llvm/utils/TableGen/DisassemblerEmitter.cpp
1865 @@ -105,6 +105,11 @@ extern void EmitFixedLenDecoder(RecordKeeper &RK, raw_ostream &OS,
1866 void EmitDisassembler(RecordKeeper &Records, raw_ostream &OS) {
1867 CodeGenTarget Target(Records);
1868 emitSourceFileHeader(" * " + Target.getName().str() + " Disassembler", OS);
1869 +#ifdef CAPSTONE
1870 + OS << "/* Capstone Disassembly Engine */\n"
1871 + "/* By Nguyen Anh Quynh <[email protected]>, 2013-2015 */\n"
1872 + "\n";
1873 +#endif
1874
1875 // X86 uses a custom disassembler.
1876 if (Target.getName() == "X86") {
1877 @@ -149,7 +154,12 @@ void EmitDisassembler(RecordKeeper &Records, raw_ostream &OS) {
1878 }
1879
1880 EmitFixedLenDecoder(Records, OS, Target.getName(),
1881 - "if (", " == MCDisassembler::Fail)",
1882 + "if (",
1883 +#ifdef CAPSTONE
1884 + " == MCDisassembler_Fail)",
1885 +#else
1886 + " == MCDisassembler::Fail)",
1887 +#endif
1888 "MCDisassembler::Success", "MCDisassembler::Fail", "");
1889 }
1890
1891 diff --git a/llvm/utils/TableGen/FixedLenDecoderEmitter.cpp b/llvm/utils/TableGen/FixedLenDecoderEmitter.cpp
1892 index ea28e06cc..3db428dfa 100644
1893 --- a/llvm/utils/TableGen/FixedLenDecoderEmitter.cpp
1894 +++ b/llvm/utils/TableGen/FixedLenDecoderEmitter.cpp
1895 @@ -741,7 +741,13 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
1896 ++I;
1897 unsigned Start = *I++;
1898 unsigned Len = *I++;
1899 - OS.indent(Indentation) << "MCD::OPC_ExtractField, " << Start << ", "
1900 + OS.indent(Indentation)
1901 +#ifdef CAPSTONE
1902 + << "MCD_OPC_ExtractField"
1903 +#else
1904 + << "MCD::OPC_ExtractField"
1905 +#endif
1906 + << ", " << Start << ", "
1907 << Len << ", // Inst{";
1908 if (Len > 1)
1909 OS << (Start + Len - 1) << "-";
1910 @@ -750,7 +756,13 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
1911 }
1912 case MCD::OPC_FilterValue: {
1913 ++I;
1914 - OS.indent(Indentation) << "MCD::OPC_FilterValue, ";
1915 + OS.indent(Indentation)
1916 +#ifdef CAPSTONE
1917 + << "MCD_OPC_FilterValue"
1918 +#else
1919 + << "MCD::OPC_FilterValue"
1920 +#endif
1921 + << ", ";
1922 // The filter value is ULEB128 encoded.
1923 while (*I >= 128)
1924 OS << (unsigned)*I++ << ", ";
1925 @@ -773,7 +785,13 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
1926 ++I;
1927 unsigned Start = *I++;
1928 unsigned Len = *I++;
1929 - OS.indent(Indentation) << "MCD::OPC_CheckField, " << Start << ", "
1930 + OS.indent(Indentation)
1931 +#ifdef CAPSTONE
1932 + << "MCD_OPC_CheckField"
1933 +#else
1934 + << "MCD::OPC_CheckField"
1935 +#endif
1936 + << ", " << Start << ", "
1937 << Len << ", ";// << Val << ", " << NumToSkip << ",\n";
1938 // ULEB128 encoded field value.
1939 for (; *I >= 128; ++I)
1940 @@ -794,7 +812,13 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
1941 }
1942 case MCD::OPC_CheckPredicate: {
1943 ++I;
1944 - OS.indent(Indentation) << "MCD::OPC_CheckPredicate, ";
1945 + OS.indent(Indentation)
1946 +#ifdef CAPSTONE
1947 + << "MCD_OPC_CheckPredicate"
1948 +#else
1949 + << "MCD::OPC_CheckPredicate"
1950 +#endif
1951 + << ", ";
1952 for (; *I >= 128; ++I)
1953 OS << (unsigned)*I << ", ";
1954 OS << (unsigned)*I++ << ", ";
1955 @@ -823,7 +847,13 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
1956 && "ULEB128 value too large!");
1957 // Decode the Opcode value.
1958 unsigned Opc = decodeULEB128(Buffer);
1959 - OS.indent(Indentation) << "MCD::OPC_" << (IsTry ? "Try" : "")
1960 + OS.indent(Indentation)
1961 +#ifdef CAPSTONE
1962 + << "MCD_OPC_"
1963 +#else
1964 + << "MCD::OPC_"
1965 +#endif
1966 + << (IsTry ? "Try" : "")
1967 << "Decode, ";
1968 for (p = Buffer; *p >= 128; ++p)
1969 OS << (unsigned)*p << ", ";
1970 @@ -858,7 +888,12 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
1971 }
1972 case MCD::OPC_SoftFail: {
1973 ++I;
1974 - OS.indent(Indentation) << "MCD::OPC_SoftFail";
1975 + OS.indent(Indentation)
1976 +#ifdef CAPSTONE
1977 + << "MCD_OPC_SoftFail";
1978 +#else
1979 + << "MCD::OPC_SoftFail";
1980 +#endif
1981 // Positive mask
1982 uint64_t Value = 0;
1983 unsigned Shift = 0;
1984 @@ -890,7 +925,13 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
1985 }
1986 case MCD::OPC_Fail: {
1987 ++I;
1988 - OS.indent(Indentation) << "MCD::OPC_Fail,\n";
1989 + OS.indent(Indentation)
1990 +#ifdef CAPSTONE
1991 + << "MCD_OPC_Fail"
1992 +#else
1993 + << "MCD::OPC_Fail"
1994 +#endif
1995 + << ",\n";
1996 break;
1997 }
1998 }
1999 @@ -905,23 +946,46 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
2000 void FixedLenDecoderEmitter::
2001 emitPredicateFunction(formatted_raw_ostream &OS, PredicateSet &Predicates,
2002 unsigned Indentation) const {
2003 +#ifdef CAPSTONE
2004 + OS.indent(Indentation) << "static bool getbool(uint64_t b)\n";
2005 + OS.indent(Indentation) << "{\n";
2006 + OS.indent(Indentation) << "\treturn b != 0;\n";
2007 + OS.indent(Indentation) << "}\n\n";
2008 +#endif
2009 +
2010 // The predicate function is just a big switch statement based on the
2011 // input predicate index.
2012 OS.indent(Indentation) << "static bool checkDecoderPredicate(unsigned Idx, "
2013 +#ifdef CAPSTONE
2014 + << "uint64_t Bits)\n{\n";
2015 +#else
2016 << "const FeatureBitset& Bits) {\n";
2017 +#endif
2018 Indentation += 2;
2019 if (!Predicates.empty()) {
2020 OS.indent(Indentation) << "switch (Idx) {\n";
2021 - OS.indent(Indentation) << "default: llvm_unreachable(\"Invalid index!\");\n";
2022 + OS.indent(Indentation) << "default: "
2023 +#ifdef CAPSTONE
2024 + << "assert(0);\n"
2025 +#endif
2026 + << "llvm_unreachable(\"Invalid index!\");\n";
2027 unsigned Index = 0;
2028 for (const auto &Predicate : Predicates) {
2029 OS.indent(Indentation) << "case " << Index++ << ":\n";
2030 - OS.indent(Indentation+2) << "return (" << Predicate << ");\n";
2031 + OS.indent(Indentation+2) << "return "
2032 +#ifdef CAPSTONE
2033 + << "getbool"
2034 +#endif
2035 + << "(" << Predicate << ");\n";
2036 }
2037 OS.indent(Indentation) << "}\n";
2038 } else {
2039 // No case statement to emit
2040 - OS.indent(Indentation) << "llvm_unreachable(\"Invalid index!\");\n";
2041 + OS.indent(Indentation)
2042 +#ifdef CAPSTONE
2043 + << "assert(0);\n"
2044 +#endif
2045 + << "llvm_unreachable(\"Invalid index!\");\n";
2046 }
2047 Indentation -= 2;
2048 OS.indent(Indentation) << "}\n\n";
2049 @@ -932,23 +996,39 @@ emitDecoderFunction(formatted_raw_ostream &OS, DecoderSet &Decoders,
2050 unsigned Indentation) const {
2051 // The decoder function is just a big switch statement based on the
2052 // input decoder index.
2053 +#ifdef CAPSTONE
2054 +#define EDF_EOL " \\\n"
2055 + OS.indent(Indentation) << "#define DecodeToMCInst(fname,fieldname, InsnType) \\\n";
2056 + OS.indent(Indentation) << "static DecodeStatus fname(DecodeStatus S, unsigned Idx, InsnType insn, MCInst *MI, \\\n";
2057 + OS.indent(Indentation) << " uint64_t Address, const void *Decoder) \\\n";
2058 + OS.indent(Indentation) << "{ \\\n";
2059 +#else
2060 +#define EDF_EOL "\n"
2061 OS.indent(Indentation) << "template<typename InsnType>\n";
2062 OS.indent(Indentation) << "static DecodeStatus decodeToMCInst(DecodeStatus S,"
2063 << " unsigned Idx, InsnType insn, MCInst &MI,\n";
2064 OS.indent(Indentation) << " uint64_t "
2065 << "Address, const void *Decoder, bool &DecodeComplete) {\n";
2066 +#endif
2067 Indentation += 2;
2068 +#ifndef CAPSTONE
2069 OS.indent(Indentation) << "DecodeComplete = true;\n";
2070 - OS.indent(Indentation) << "InsnType tmp;\n";
2071 - OS.indent(Indentation) << "switch (Idx) {\n";
2072 - OS.indent(Indentation) << "default: llvm_unreachable(\"Invalid index!\");\n";
2073 +#endif
2074 + OS.indent(Indentation) << "InsnType tmp;" EDF_EOL;
2075 + OS.indent(Indentation) << "switch (Idx) {" EDF_EOL;
2076 + OS.indent(Indentation) << "default:"
2077 +#ifndef CAPSTONE
2078 + << " llvm_unreachable(\"Invalid index!\");\n";
2079 +#else
2080 + << " assert(0);\\\n";
2081 +#endif
2082 unsigned Index = 0;
2083 for (const auto &Decoder : Decoders) {
2084 - OS.indent(Indentation) << "case " << Index++ << ":\n";
2085 + OS.indent(Indentation) << "case " << Index++ << ":" EDF_EOL;
2086 OS << Decoder;
2087 - OS.indent(Indentation+2) << "return S;\n";
2088 + OS.indent(Indentation+2) << "return S;" EDF_EOL;
2089 }
2090 - OS.indent(Indentation) << "}\n";
2091 + OS.indent(Indentation) << "}" EDF_EOL;
2092 Indentation -= 2;
2093 OS.indent(Indentation) << "}\n\n";
2094 }
2095 @@ -1075,16 +1155,21 @@ void FilterChooser::emitBinaryParser(raw_ostream &o, unsigned &Indentation,
2096 const std::string &Decoder = OpInfo.Decoder;
2097
2098 if (OpInfo.numFields() != 1)
2099 - o.indent(Indentation) << "tmp = 0;\n";
2100 + o.indent(Indentation) << "tmp = 0;" EDF_EOL;
2101
2102 for (const EncodingField &EF : OpInfo) {
2103 o.indent(Indentation) << "tmp ";
2104 if (OpInfo.numFields() != 1) o << '|';
2105 - o << "= fieldFromInstruction"
2106 + o << "= "
2107 +#ifdef CAPSTONE
2108 + << "fieldname"
2109 +#else
2110 + << "fieldFromInstruction"
2111 +#endif
2112 << "(insn, " << EF.Base << ", " << EF.Width << ')';
2113 if (OpInfo.numFields() != 1 || EF.Offset != 0)
2114 o << " << " << EF.Offset;
2115 - o << ";\n";
2116 + o << ";" EDF_EOL;
2117 }
2118
2119 if (Decoder != "") {
2120 @@ -1092,8 +1177,12 @@ void FilterChooser::emitBinaryParser(raw_ostream &o, unsigned &Indentation,
2121 o.indent(Indentation) << Emitter->GuardPrefix << Decoder
2122 << "(MI, tmp, Address, Decoder)"
2123 << Emitter->GuardPostfix
2124 +#ifdef CAPSTONE
2125 + << " return MCDisassembler_Fail; \\\n";
2126 +#else
2127 << " { " << (OpHasCompleteDecoder ? "" : "DecodeComplete = false; ")
2128 << "return MCDisassembler::Fail; }\n";
2129 +#endif
2130 } else {
2131 OpHasCompleteDecoder = true;
2132 o.indent(Indentation) << "MI.addOperand(MCOperand::createImm(tmp));\n";
2133 @@ -1112,7 +1201,13 @@ void FilterChooser::emitDecoder(raw_ostream &OS, unsigned Indentation,
2134 << "(MI, insn, Address, Decoder)"
2135 << Emitter->GuardPostfix
2136 << " { " << (HasCompleteDecoder ? "" : "DecodeComplete = false; ")
2137 - << "return MCDisassembler::Fail; }\n";
2138 + << "return "
2139 +#ifdef CAPSTONE
2140 + << "MCDisassembler_Fail"
2141 +#else
2142 + << "MCDisassembler::Fail"
2143 +#endif
2144 + << "; }\n";
2145 break;
2146 }
2147
2148 @@ -1150,10 +1245,19 @@ unsigned FilterChooser::getDecoderIndex(DecoderSet &Decoders,
2149 static void emitSinglePredicateMatch(raw_ostream &o, StringRef str,
2150 const std::string &PredicateNamespace) {
2151 if (str[0] == '!')
2152 +#ifdef CAPSTONE
2153 + o << "~(Bits & " << PredicateNamespace << "_"
2154 + << str.slice(1,str.size()) << ")";
2155 +#else
2156 o << "!Bits[" << PredicateNamespace << "::"
2157 << str.slice(1,str.size()) << "]";
2158 +#endif
2159 else
2160 +#ifdef CAPSTONE
2161 + o << "(Bits & " << PredicateNamespace << "_" << str << ")";
2162 +#else
2163 o << "Bits[" << PredicateNamespace << "::" << str << "]";
2164 +#endif
2165 }
2166
2167 bool FilterChooser::emitPredicateMatch(raw_ostream &o, unsigned &Indentation,
2168 @@ -2090,7 +2194,18 @@ static void emitFieldFromInstruction(formatted_raw_ostream &OS) {
2169 << "// * Support shift (<<, >>) with signed and unsigned integers on the "
2170 "RHS\n"
2171 << "// * Support put (<<) to raw_ostream&\n"
2172 - << "template<typename InsnType>\n"
2173 +#ifdef CAPSTONE
2174 + << "#define FieldFromInstruction(fname, InsnType) \\\n"
2175 + << "static InsnType fname(InsnType insn, unsigned startBit, unsigned numBits) \\\n"
2176 + << "{ \\\n"
2177 + << " InsnType fieldMask; \\\n"
2178 + << " if (numBits == sizeof(InsnType)*8) \\\n"
2179 + << " fieldMask = (InsnType)(-1LL); \\\n"
2180 + << " else \\\n"
2181 + << " fieldMask = (((InsnType)1 << numBits) - 1) << startBit; \\\n"
2182 + << " return (insn & fieldMask) >> startBit; \\\n"
2183 +#else
2184 + << "template<typename InsnType>\n"
2185 << "#if defined(_MSC_VER) && !defined(__clang__)\n"
2186 << "__declspec(noinline)\n"
2187 << "#endif\n"
2188 @@ -2127,12 +2242,239 @@ static void emitFieldFromInstruction(formatted_raw_ostream &OS) {
2189 << " unsigned numBits) {\n"
2190 << " return fieldFromInstruction(insn, startBit, numBits, "
2191 "std::is_integral<InsnType>());\n"
2192 +#endif
2193 << "}\n\n";
2194 }
2195
2196 // emitDecodeInstruction - Emit the templated helper function
2197 // decodeInstruction().
2198 static void emitDecodeInstruction(formatted_raw_ostream &OS) {
2199 +
2200 +#if 0
2201 + OS << "#define DecodeInstruction(fname, fieldname, decoder, InsnType) \\\n"
2202 + << "static DecodeStatus fname(const uint8_t DecodeTable[], MCInst *MI, \\\n"
2203 + << " InsnType insn, uint64_t Address, const MCRegisterInfo *MRI, int feature) \\\n"
2204 + << "{ \\\n"
2205 + << " uint64_t Bits = getFeatureBits(feature); \\\n"
2206 + << " const uint8_t *Ptr = DecodeTable; \\\n"
2207 + << " uint32_t CurFieldValue = 0, ExpectedValue; \\\n"
2208 + << " DecodeStatus S = MCDisassembler_Success; \\\n"
2209 + << " unsigned Start, Len, NumToSkip, PIdx, Opc, DecodeIdx; \\\n"
2210 + << " InsnType Val, FieldValue, PositiveMask, NegativeMask; \\\n"
2211 + << " bool Pred, Fail; \\\n"
2212 + << " for (;;) { \\\n"
2213 + << " switch (*Ptr) { \\\n"
2214 + << " default: \\\n"
2215 + << " return MCDisassembler_Fail; \\\n"
2216 + << " case MCD_OPC_ExtractField: { \\\n"
2217 + << " Start = *++Ptr; \\\n"
2218 + << " Len = *++Ptr; \\\n"
2219 + << " ++Ptr; \\\n"
2220 + << " CurFieldValue = (uint32_t)fieldname(insn, Start, Len); \\\n"
2221 + << " break; \\\n"
2222 + << " } \\\n"
2223 + << " case MCD_OPC_FilterValue: { \\\n"
2224 + << " Val = (InsnType)decodeULEB128(++Ptr, &Len); \\\n"
2225 + << " Ptr += Len; \\\n"
2226 + << " NumToSkip = *Ptr++; \\\n"
2227 + << " NumToSkip |= (*Ptr++) << 8; \\\n"
2228 + << " if (Val != CurFieldValue) \\\n"
2229 + << " Ptr += NumToSkip; \\\n"
2230 + << " break; \\\n"
2231 + << " } \\\n"
2232 + << " case MCD_OPC_CheckField: { \\\n"
2233 + << " Start = *++Ptr; \\\n"
2234 + << " Len = *++Ptr; \\\n"
2235 + << " FieldValue = fieldname(insn, Start, Len); \\\n"
2236 + << " ExpectedValue = (uint32_t)decodeULEB128(++Ptr, &Len); \\\n"
2237 + << " Ptr += Len; \\\n"
2238 + << " NumToSkip = *Ptr++; \\\n"
2239 + << " NumToSkip |= (*Ptr++) << 8; \\\n"
2240 + << " if (ExpectedValue != FieldValue) \\\n"
2241 + << " Ptr += NumToSkip; \\\n"
2242 + << " break; \\\n"
2243 + << " } \\\n"
2244 + << " case MCD_OPC_CheckPredicate: { \\\n"
2245 + << " PIdx = (uint32_t)decodeULEB128(++Ptr, &Len); \\\n"
2246 + << " Ptr += Len; \\\n"
2247 + << " NumToSkip = *Ptr++; \\\n"
2248 + << " NumToSkip |= (*Ptr++) << 8; \\\n"
2249 + << " Pred = checkDecoderPredicate(PIdx, Bits); \\\n"
2250 + << " if (!Pred) \\\n"
2251 + << " Ptr += NumToSkip; \\\n"
2252 + << " (void)Pred; \\\n"
2253 + << " break; \\\n"
2254 + << " } \\\n"
2255 + << " case MCD_OPC_Decode: { \\\n"
2256 + << " Opc = (unsigned)decodeULEB128(++Ptr, &Len); \\\n"
2257 + << " Ptr += Len; \\\n"
2258 + << " DecodeIdx = (unsigned)decodeULEB128(Ptr, &Len); \\\n"
2259 + << " Ptr += Len; \\\n"
2260 + << " MCInst_setOpcode(MI, Opc); \\\n"
2261 + << " return decoder(S, DecodeIdx, insn, MI, Address, MRI); \\\n"
2262 + << " } \\\n"
2263 + << " case MCD_OPC_SoftFail: { \\\n"
2264 + << " PositiveMask = (InsnType)decodeULEB128(++Ptr, &Len); \\\n"
2265 + << " Ptr += Len; \\\n"
2266 + << " NegativeMask = (InsnType)decodeULEB128(Ptr, &Len); \\\n"
2267 + << " Ptr += Len; \\\n"
2268 + << " Fail = (insn & PositiveMask) || (~insn & NegativeMask); \\\n"
2269 + << " if (Fail) \\\n"
2270 + << " S = MCDisassembler_SoftFail; \\\n"
2271 + << " break; \\\n"
2272 + << " } \\\n"
2273 + << " case MCD_OPC_Fail: { \\\n"
2274 + << " return MCDisassembler_Fail; \\\n"
2275 + << " } \\\n"
2276 + << " } \\\n"
2277 + << " } \\\n"
2278 +#endif
2279 +
2280 +#ifdef CAPSTONE
2281 + OS << "static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], "
2282 +
2283 + "MCInst *MI,\\\n"
2284 + << " InsnType insn, uint64_t "
2285 + "Address,\\\n"
2286 + << " const void *DisAsm,\\\n"
2287 + << " int feature) {\\\n"
2288 + << " uint64_t Bits = getFeatureBits(feature); \\\n"
2289 + //<< " const FeatureBitset& Bits = STI.getFeatureBits();\n"
2290 + << "\\\n"
2291 + << " const uint8_t *Ptr = DecodeTable;\\\n"
2292 + << " uint32_t CurFieldValue = 0;\\\n"
2293 + << " DecodeStatus S = MCDisassembler_Success;\\\n"
2294 + << " while (true) {\\\n"
2295 + << " ptrdiff_t Loc = Ptr - DecodeTable;\\\n"
2296 + << " switch (*Ptr) {\\\n"
2297 + << " default:\\\n"
2298 + << " return MCDisassembler_Fail;\\\n"
2299 + << " case MCD_OPC_ExtractField: {\\\n"
2300 + << " unsigned Start = *++Ptr;\\\n"
2301 + << " unsigned Len = *++Ptr;\\\n"
2302 + << " ++Ptr;\\\n"
2303 + << " CurFieldValue = fieldFromInstruction(insn, Start, Len);\\\n"
2304 + << " break;\\\n"
2305 + << " }\\\n"
2306 + << " case MCD_OPC_FilterValue: {\\\n"
2307 + << " // Decode the field value.\\\n"
2308 + << " unsigned Len;\\\n"
2309 + << " InsnType Val = decodeULEB128(++Ptr, &Len);\\\n"
2310 + << " Ptr += Len;\\\n"
2311 + << " // NumToSkip is a plain 24-bit integer.\\\n"
2312 + << " unsigned NumToSkip = *Ptr++;\\\n"
2313 + << " NumToSkip |= (*Ptr++) << 8;\\\n"
2314 + << " NumToSkip |= (*Ptr++) << 16;\\\n"
2315 + << "\\\n"
2316 + << " // Perform the filter operation.\\\n"
2317 + << " if (Val != CurFieldValue)\\\n"
2318 + << " Ptr += NumToSkip;\\\n"
2319 + << "\\\n"
2320 + << " break;\\\n"
2321 + << " }\\\n"
2322 + << " case MCD_OPC_CheckField: {\\\n"
2323 + << " unsigned Start = *++Ptr;\\\n"
2324 + << " unsigned Len = *++Ptr;\\\n"
2325 + << " InsnType FieldValue = fieldFromInstruction(insn, Start, Len);\\\n"
2326 + << " // Decode the field value.\\\n"
2327 + << " uint32_t ExpectedValue = decodeULEB128(++Ptr, &Len);\\\n"
2328 + << " Ptr += Len;\\\n"
2329 + << " // NumToSkip is a plain 24-bit integer.\\\n"
2330 + << " unsigned NumToSkip = *Ptr++;\\\n"
2331 + << " NumToSkip |= (*Ptr++) << 8;\\\n"
2332 + << " NumToSkip |= (*Ptr++) << 16;\\\n"
2333 + << "\\\n"
2334 + << " // If the actual and expected values don't match, skip.\\\n"
2335 + << " if (ExpectedValue != FieldValue)\\\n"
2336 + << " Ptr += NumToSkip;\\\n"
2337 + << " break;\\\n"
2338 + << " }\\\n"
2339 + << " case MCD_OPC_CheckPredicate: {\\\n"
2340 + << " unsigned Len;\\\n"
2341 + << " // Decode the Predicate Index value.\\\n"
2342 + << " unsigned PIdx = decodeULEB128(++Ptr, &Len);\\\n"
2343 + << " Ptr += Len;\\\n"
2344 + << " // NumToSkip is a plain 24-bit integer.\\\n"
2345 + << " unsigned NumToSkip = *Ptr++;\\\n"
2346 + << " NumToSkip |= (*Ptr++) << 8;\\\n"
2347 + << " NumToSkip |= (*Ptr++) << 16;\\\n"
2348 + << " // Check the predicate.\\\n"
2349 + << " bool Pred;\\\n"
2350 + << " if (!(Pred = checkDecoderPredicate(PIdx, Bits)))\\\n"
2351 + << " Ptr += NumToSkip;\\\n"
2352 + << " (void)Pred;\\\n"
2353 + << " break;\\\n"
2354 + << " }\\\n"
2355 + << " case MCD_OPC_Decode: {\\\n"
2356 + << " unsigned Len;\\\n"
2357 + << " // Decode the Opcode value.\\\n"
2358 + << " unsigned Opc = decodeULEB128(++Ptr, &Len);\\\n"
2359 + << " Ptr += Len;\\\n"
2360 + << " unsigned DecodeIdx = decodeULEB128(Ptr, &Len);\\\n"
2361 + << " Ptr += Len;\\\n"
2362 + << "\\\n"
2363 + << " MCInst_clear(MI);\\\n"
2364 + << " MCInst_setOpcode(MI, Opc);\\\n"
2365 + << " bool DecodeComplete;\\\n"
2366 + << " S = decodeToMCInst(S, DecodeIdx, insn, MI, Address, DisAsm, "
2367 + "DecodeComplete);\\\n"
2368 + << " assert(DecodeComplete);\\\n"
2369 + << "\\\n"
2370 + << " return S;\\\n"
2371 + << " }\\\n"
2372 + << " case MCD_OPC_TryDecode: {\\\n"
2373 + << " unsigned Len;\\\n"
2374 + << " // Decode the Opcode value.\\\n"
2375 + << " unsigned Opc = decodeULEB128(++Ptr, &Len);\\\n"
2376 + << " Ptr += Len;\\\n"
2377 + << " unsigned DecodeIdx = decodeULEB128(Ptr, &Len);\\\n"
2378 + << " Ptr += Len;\\\n"
2379 + << " // NumToSkip is a plain 24-bit integer.\\\n"
2380 + << " unsigned NumToSkip = *Ptr++;\\\n"
2381 + << " NumToSkip |= (*Ptr++) << 8;\\\n"
2382 + << " NumToSkip |= (*Ptr++) << 16;\\\n"
2383 + << "\\\n"
2384 + << " // Perform the decode operation.\\\n"
2385 + << " MCInst TmpMI;\\\n"
2386 + << " MCInst_setOpcode(&TmpMI, Opc);\\\n"
2387 + << " bool DecodeComplete;\n"
2388 + << " S = decodeToMCInst(S, DecodeIdx, insn, &TmpMI, Address, DisAsm, "
2389 + "DecodeComplete);\\\n"
2390 + << " if (DecodeComplete) {\\\n"
2391 + << " // Decoding complete.\\\n"
2392 + << " MI = &TmpMI;\\\n"
2393 + << " return S;\\\n"
2394 + << " } else {\\\n"
2395 + << " assert(S == MCDisassembler_Fail);\\\n"
2396 + << " // If the decoding was incomplete, skip.\\\n"
2397 + << " Ptr += NumToSkip;\\\n"
2398 + << " // Reset decode status. This also drops a SoftFail status "
2399 + "that could be\\\n"
2400 + << " // set before the decode attempt.\\\n"
2401 + << " S = MCDisassembler_Success;\\\n"
2402 + << " }\\\n"
2403 + << " break;\\\n"
2404 + << " }\\\n"
2405 + << " case MCD_OPC_SoftFail: {\\\n"
2406 + << " // Decode the mask values.\\\n"
2407 + << " unsigned Len;\\\n"
2408 + << " InsnType PositiveMask = decodeULEB128(++Ptr, &Len);\\\n"
2409 + << " Ptr += Len;\\\n"
2410 + << " InsnType NegativeMask = decodeULEB128(Ptr, &Len);\\\n"
2411 + << " Ptr += Len;\\\n"
2412 + << " bool Fail = (insn & PositiveMask) || (~insn & NegativeMask);\\\n"
2413 + << " if (Fail)\\\n"
2414 + << " S = MCDisassembler_SoftFail;\\\n"
2415 + << " break;\\\n"
2416 + << " }\\\n"
2417 + << " case MCD_OPC_Fail: {\\\n"
2418 + << " return MCDisassembler_Fail;\\\n"
2419 + << " }\\\n"
2420 + << " }\\\n"
2421 + << " }\\\n"
2422 + << " assert(0);\\\n"
2423 +
2424 +#else
2425 OS << "template<typename InsnType>\n"
2426 << "static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], "
2427 "MCInst &MI,\n"
2428 @@ -2313,12 +2655,19 @@ static void emitDecodeInstruction(formatted_raw_ostream &OS) {
2429 << " }\n"
2430 << " llvm_unreachable(\"bogosity detected in disassembler state "
2431 "machine!\");\n"
2432 +#endif
2433 << "}\n\n";
2434 }
2435
2436 // Emits disassembler code for instruction decoding.
2437 void FixedLenDecoderEmitter::run(raw_ostream &o) {
2438 formatted_raw_ostream OS(o);
2439 +#ifdef CAPSTONE
2440 + OS << "#include \"../../MCInst.h\"\n";
2441 + OS << "#include \"../../LEB128.h\"\n";
2442 + OS << "#include <assert.h>\n";
2443 + OS << "\n";
2444 +#else
2445 OS << "#include \"llvm/MC/MCInst.h\"\n";
2446 OS << "#include \"llvm/Support/Debug.h\"\n";
2447 OS << "#include \"llvm/Support/DataTypes.h\"\n";
2448 @@ -2327,6 +2676,7 @@ void FixedLenDecoderEmitter::run(raw_ostream &o) {
2449 OS << "#include <assert.h>\n";
2450 OS << '\n';
2451 OS << "namespace llvm {\n\n";
2452 +#endif
2453
2454 emitFieldFromInstruction(OS);
2455
2456 @@ -2401,7 +2751,13 @@ void FixedLenDecoderEmitter::run(raw_ostream &o) {
2457 // Emit the main entry point for the decoder, decodeInstruction().
2458 emitDecodeInstruction(OS);
2459
2460 +#ifdef CAPSTONE
2461 + OS << "FieldFromInstruction(fieldFromInstruction, uint64_t)\n";
2462 + OS << "DecodeToMCInst(decodeToMCInst, fieldFromInstruction, uint64_t)\n";
2463 + OS << "DecodeInstruction(decodeInstruction, fieldFromInstruction, decodeToMCInst, uint64_t)\n";
2464 +#else
2465 OS << "\n} // End llvm namespace\n";
2466 +#endif
2467 }
2468
2469 namespace llvm {
2470 diff --git a/llvm/utils/TableGen/InstrInfoEmitter.cpp b/llvm/utils/TableGen/InstrInfoEmitter.cpp
2471 index fd8775023..797f42a50 100644
2472 --- a/llvm/utils/TableGen/InstrInfoEmitter.cpp
2473 +++ b/llvm/utils/TableGen/InstrInfoEmitter.cpp
2474 @@ -92,6 +92,7 @@ private:
2475
2476 } // end anonymous namespace
2477
2478 +#ifndef CAPSTONE
2479 static void PrintDefList(const std::vector<Record*> &Uses,
2480 unsigned Num, raw_ostream &OS) {
2481 OS << "static const MCPhysReg ImplicitList" << Num << "[] = { ";
2482 @@ -99,6 +100,7 @@ static void PrintDefList(const std::vector<Record*> &Uses,
2483 OS << getQualifiedName(U) << ", ";
2484 OS << "0 };\n";
2485 }
2486 +#endif
2487
2488 //===----------------------------------------------------------------------===//
2489 // Operand Info Emission.
2490 @@ -434,8 +436,17 @@ void InstrInfoEmitter::emitTIIHelperMethods(raw_ostream &OS,
2491 // run - Emit the main instruction description records for the target...
2492 void InstrInfoEmitter::run(raw_ostream &OS) {
2493 emitSourceFileHeader("Target Instruction Enum Values and Descriptors", OS);
2494 +
2495 +#ifdef CAPSTONE
2496 + OS << "/* Capstone Disassembly Engine */\n"
2497 + "/* By Nguyen Anh Quynh <[email protected]>, 2013-2015 */\n"
2498 + "\n"
2499 + "\n";
2500 +#endif
2501 +
2502 emitEnums(OS);
2503
2504 +#ifndef CAPSTONE
2505 OS << "#ifdef GET_INSTRINFO_MC_DESC\n";
2506 OS << "#undef GET_INSTRINFO_MC_DESC\n";
2507
2508 @@ -563,6 +574,7 @@ void InstrInfoEmitter::run(raw_ostream &OS) {
2509 emitOperandTypesEnum(OS, Target);
2510
2511 emitMCIIHelperMethods(OS, TargetName);
2512 +#endif
2513 }
2514
2515 void InstrInfoEmitter::emitRecord(const CodeGenInstruction &Inst, unsigned Num,
2516 @@ -680,7 +692,9 @@ void InstrInfoEmitter::emitEnums(raw_ostream &OS) {
2517 OS << "#ifdef GET_INSTRINFO_ENUM\n";
2518 OS << "#undef GET_INSTRINFO_ENUM\n";
2519
2520 +#ifndef CAPSTONE
2521 OS << "namespace llvm {\n\n";
2522 +#endif
2523
2524 CodeGenTarget Target(Records);
2525
2526 @@ -690,17 +704,39 @@ void InstrInfoEmitter::emitEnums(raw_ostream &OS) {
2527 if (Namespace.empty())
2528 PrintFatalError("No instructions defined!");
2529
2530 +#ifndef CAPSTONE
2531 OS << "namespace " << Namespace << " {\n";
2532 - OS << " enum {\n";
2533 +#endif
2534 +#ifdef CAPSTONE
2535 + OS << "\n"
2536 +#else
2537 + OS << " "
2538 +#endif
2539 + << "enum {\n";
2540 unsigned Num = 0;
2541 for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue())
2542 - OS << " " << Inst->TheDef->getName() << "\t= " << Num++ << ",\n";
2543 - OS << " INSTRUCTION_LIST_END = " << Num << "\n";
2544 + OS << " "
2545 +#ifdef CAPSTONE
2546 + << Target.getName() << "_"
2547 +#endif
2548 + << Inst->TheDef->getName() << "\t= " << Num++ << ",\n";
2549 + OS << " "
2550 +#ifdef CAPSTONE
2551 + << Target.getName() << "_"
2552 +#endif
2553 + << "INSTRUCTION_LIST_END = " << Num << "\n";
2554 OS << " };\n\n";
2555 +#ifndef CAPSTONE
2556 OS << "} // end " << Namespace << " namespace\n";
2557 OS << "} // end llvm namespace\n";
2558 - OS << "#endif // GET_INSTRINFO_ENUM\n\n";
2559 -
2560 +#endif
2561 + OS << "#endif // GET_INSTRINFO_ENUM\n"
2562 +#ifndef CAPSTONE
2563 + << "\n"
2564 +#endif
2565 + ;
2566 +
2567 +#ifndef CAPSTONE
2568 OS << "#ifdef GET_INSTRINFO_SCHED_ENUM\n";
2569 OS << "#undef GET_INSTRINFO_SCHED_ENUM\n";
2570 OS << "namespace llvm {\n\n";
2571 @@ -717,13 +753,140 @@ void InstrInfoEmitter::emitEnums(raw_ostream &OS) {
2572 OS << "} // end llvm namespace\n";
2573
2574 OS << "#endif // GET_INSTRINFO_SCHED_ENUM\n\n";
2575 +#endif
2576 }
2577
2578 namespace llvm {
2579
2580 void EmitInstrInfo(RecordKeeper &RK, raw_ostream &OS) {
2581 InstrInfoEmitter(RK).run(OS);
2582 +#ifndef CAPSTONE
2583 EmitMapTable(RK, OS);
2584 +#endif
2585 +}
2586 +
2587 +#ifdef CAPSTONE
2588 +std::string GetPublicName(const CodeGenInstruction *Inst) {
2589 + std::string Name = Inst->TheDef->getName();
2590 + // Apply backward compatibility fixups.
2591 + // BRNLE -> BNLER.
2592 + if (Name.length() >= 5 && Name.substr(0, 5) == "BRAsm") {
2593 + Name = "B" + Name.substr(5, Name.length() - 5) + "R";
2594 + }
2595 + // SSKEOpt -> SSKE.
2596 + while (Name.length() >= 3 && Name.substr(Name.length() - 3, 3) == "Opt") {
2597 + Name = Name.substr(0, Name.length() - 3);
2598 + }
2599 + // BRCLAsm -> BRCL.
2600 + while (true) {
2601 + size_t pos = Name.find("Asm");
2602 + if (pos == std::string::npos) {
2603 + break;
2604 + }
2605 + Name = Name.substr(0, pos) + Name.substr(pos + 3);
2606 + }
2607 + // CPSDRxx -> CPSDR.
2608 + if (Name.length() >= 2) {
2609 + std::string Suffix2 = Name.substr(Name.length() - 2, 2);
2610 + if (Suffix2 == "dd" || Suffix2 == "ds" ||
2611 + Suffix2 == "sd" || Suffix2 == "ss") {
2612 + Name = Name.substr(0, Name.length() - 2);
2613 + }
2614 + }
2615 + return "SYSZ_INS_" + Name;
2616 +}
2617 +
2618 +std::string GetRegisterName(Record *Reg) {
2619 + std::string Name = Reg->getName();
2620 + for (char& c : Name) {
2621 + c = toupper(c);
2622 + }
2623 + // R0L, R0D -> R0.
2624 + if (Name.length() >= 3 &&
2625 + Name[Name.length() - 3] == 'R' &&
2626 + (Name[Name.length() - 1] == 'L' ||
2627 + Name[Name.length() - 1] == 'D')) {
2628 + Name = Name.substr(0, Name.length() - 3) + Name[Name.length() - 2];
2629 + }
2630 + return "SYSZ_REG_" + Name;
2631 +}
2632 +
2633 +std::string GetGroupName(Record *Pred) {
2634 + std::string Name = Pred->getName();
2635 + for (char& c : Name) {
2636 + c = toupper(c);
2637 + }
2638 + if (Name.length() >= 7 && Name.substr(0, 7) == "FEATURE") {
2639 + Name = Name.substr(7);
2640 + }
2641 + return "SYSZ_GRP_" + Name;
2642 +}
2643 +
2644 +void EmitMappingInsn(RecordKeeper &RK, raw_ostream &OS) {
2645 + OS << "// This is auto-gen data for Capstone engine (www.capstone-engine.org)\n"
2646 + "// By Nguyen Anh Quynh <[email protected]>\n"
2647 + "\n";
2648 + CodeGenTarget Target(RK);
2649 + for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
2650 + if (Inst->TheDef->getValueAsBit("isPseudo") ||
2651 + Inst->TheDef->getValueAsBit("isCodeGenOnly")) {
2652 + continue;
2653 + }
2654 + OS << "{\n"
2655 + << "\t" << Target.getName() << "_" << Inst->TheDef->getName() << ", "
2656 + << GetPublicName(Inst) << ",\n"
2657 + << "#ifndef CAPSTONE_DIET\n"
2658 + << "\t{ ";
2659 + for (Record *Use : Inst->TheDef->getValueAsListOfDefs("Uses")) {
2660 + OS << GetRegisterName(Use) << ", ";
2661 + }
2662 + OS << "0 }, { ";
2663 + for (Record *Def : Inst->TheDef->getValueAsListOfDefs("Defs")) {
2664 + OS << GetRegisterName(Def) << ", ";
2665 + }
2666 + OS << "0 }, { ";
2667 + ListInit *Predicates = Inst->TheDef->getValueAsListInit("Predicates");
2668 + for (unsigned i = 0; i < Predicates->size(); ++i) {
2669 + OS << GetGroupName(Predicates->getElementAsRecord(i)) << ", ";
2670 + }
2671 + OS << "0 }, "
2672 + << Inst->TheDef->getValueAsBit("isBranch")
2673 + << ", "
2674 + << Inst->TheDef->getValueAsBit("isIndirectBranch")
2675 + << "\n"
2676 + << "#endif\n"
2677 + << "},\n";
2678 + }
2679 +}
2680 +
2681 +std::string GetMnemonic(const CodeGenInstruction *Inst) {
2682 + std::string Mnemonic = Inst->AsmString;
2683 +
2684 + for (size_t i = 0; i < Mnemonic.length(); i++) {
2685 + if (Mnemonic[i] == '\t') {
2686 + return Mnemonic.substr(0, i);
2687 + }
2688 + }
2689 + return Mnemonic;
2690 +}
2691 +
2692 +void EmitInsnNameMaps(RecordKeeper &RK, raw_ostream &OS) {
2693 + OS << "// This is auto-gen data for Capstone engine (www.capstone-engine.org)\n"
2694 + "// By Nguyen Anh Quynh <[email protected]>\n"
2695 + "\n";
2696 + CodeGenTarget Target(RK);
2697 + std::map<std::string, std::string> M;
2698 + for (const CodeGenInstruction *Inst : Target.getInstructionsByEnumValue()) {
2699 + if (Inst->TheDef->getValueAsBit("isPseudo") ||
2700 + Inst->TheDef->getValueAsBit("isCodeGenOnly")) {
2701 + continue;
2702 + }
2703 + M[GetPublicName(Inst)] = GetMnemonic(Inst);
2704 + }
2705 + for (auto &P : M) {
2706 + OS << "\t{ " << P.first << ", \"" << P.second << "\" },\n";
2707 + }
2708 }
2709 +#endif
2710
2711 } // end llvm namespace
2712 diff --git a/llvm/utils/TableGen/RegisterInfoEmitter.cpp b/llvm/utils/TableGen/RegisterInfoEmitter.cpp
2713 index 1b619072c..0df306680 100644
2714 --- a/llvm/utils/TableGen/RegisterInfoEmitter.cpp
2715 +++ b/llvm/utils/TableGen/RegisterInfoEmitter.cpp
2716 @@ -98,6 +98,12 @@ private:
2717
2718 } // end anonymous namespace
2719
2720 +#ifdef CAPSTONE
2721 +#define NAME_PREFIX Target.getName() << "_" <<
2722 +#else
2723 +#define NAME_PREFIX
2724 +#endif
2725 +
2726 // runEnums - Print out enum values for all of the registers.
2727 void RegisterInfoEmitter::runEnums(raw_ostream &OS,
2728 CodeGenTarget &Target, CodeGenRegBank &Bank) {
2729 @@ -106,13 +112,22 @@ void RegisterInfoEmitter::runEnums(raw_ostream &OS,
2730 // Register enums are stored as uint16_t in the tables. Make sure we'll fit.
2731 assert(Registers.size() <= 0xffff && "Too many regs to fit in tables");
2732
2733 +#ifndef CAPSTONE
2734 StringRef Namespace = Registers.front().TheDef->getValueAsString("Namespace");
2735 +#endif
2736
2737 emitSourceFileHeader("Target Register Enum Values", OS);
2738
2739 +#ifdef CAPSTONE
2740 + OS << "/* Capstone Disassembly Engine */\n"
2741 + "/* By Nguyen Anh Quynh <[email protected]>, 2013-2015 */\n"
2742 + "\n";
2743 +#endif
2744 +
2745 OS << "\n#ifdef GET_REGINFO_ENUM\n";
2746 OS << "#undef GET_REGINFO_ENUM\n\n";
2747
2748 +#ifndef CAPSTONE
2749 OS << "namespace llvm {\n\n";
2750
2751 OS << "class MCRegisterClass;\n"
2752 @@ -121,16 +136,20 @@ void RegisterInfoEmitter::runEnums(raw_ostream &OS,
2753
2754 if (!Namespace.empty())
2755 OS << "namespace " << Namespace << " {\n";
2756 - OS << "enum {\n NoRegister,\n";
2757 +#endif
2758 +
2759 + OS << "enum {\n " << NAME_PREFIX "NoRegister,\n";
2760
2761 for (const auto &Reg : Registers)
2762 - OS << " " << Reg.getName() << " = " << Reg.EnumValue << ",\n";
2763 + OS << " " << NAME_PREFIX Reg.getName() << " = " << Reg.EnumValue << ",\n";
2764 assert(Registers.size() == Registers.back().EnumValue &&
2765 "Register enum value mismatch!");
2766 - OS << " NUM_TARGET_REGS \t// " << Registers.size()+1 << "\n";
2767 + OS << " " << NAME_PREFIX "NUM_TARGET_REGS \t// " << Registers.size()+1 << "\n";
2768 OS << "};\n";
2769 +#ifndef CAPSTONE
2770 if (!Namespace.empty())
2771 OS << "} // end namespace " << Namespace << "\n";
2772 +#endif
2773
2774 const auto &RegisterClasses = Bank.getRegClasses();
2775 if (!RegisterClasses.empty()) {
2776 @@ -139,18 +158,29 @@ void RegisterInfoEmitter::runEnums(raw_ostream &OS,
2777 assert(RegisterClasses.size() <= 0xffff &&
2778 "Too many register classes to fit in tables");
2779
2780 - OS << "\n// Register classes\n\n";
2781 + OS << "\n// Register classes\n";
2782 +#ifndef CAPSTONE
2783 + OS << "\n";
2784 if (!Namespace.empty())
2785 OS << "namespace " << Namespace << " {\n";
2786 +#endif
2787 OS << "enum {\n";
2788 for (const auto &RC : RegisterClasses)
2789 - OS << " " << RC.getName() << "RegClassID"
2790 + OS << " " << NAME_PREFIX RC.getName() << "RegClassID"
2791 << " = " << RC.EnumValue << ",\n";
2792 - OS << "\n };\n";
2793 +#ifdef CAPSTONE
2794 + OS
2795 +#else
2796 + OS << "\n "
2797 +#endif
2798 + << "};\n";
2799 +#ifndef CAPSTONE
2800 if (!Namespace.empty())
2801 OS << "} // end namespace " << Namespace << "\n\n";
2802 +#endif
2803 }
2804
2805 +#ifndef CAPSTONE
2806 const std::vector<Record*> &RegAltNameIndices = Target.getRegAltNameIndices();
2807 // If the only definition is the default NoRegAltName, we don't need to
2808 // emit anything.
2809 @@ -181,8 +211,11 @@ void RegisterInfoEmitter::runEnums(raw_ostream &OS,
2810 if (!Namespace.empty())
2811 OS << "} // end namespace " << Namespace << "\n\n";
2812 }
2813 +#endif
2814
2815 +#ifndef CAPSTONE
2816 OS << "} // end namespace llvm\n\n";
2817 +#endif
2818 OS << "#endif // GET_REGINFO_ENUM\n\n";
2819 }
2820
2821 @@ -869,7 +902,9 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
2822
2823 const auto &Regs = RegBank.getRegisters();
2824
2825 +#ifndef CAPSTONE
2826 auto &SubRegIndices = RegBank.getSubRegIndices();
2827 +#endif
2828 // The lists of sub-registers and super-registers go in the same array. That
2829 // allows us to share suffixes.
2830 typedef std::vector<const CodeGenRegister*> RegVec;
2831 @@ -961,25 +996,40 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
2832 LaneMaskSeqs.layout();
2833 SubRegIdxSeqs.layout();
2834
2835 +#ifndef CAPSTONE
2836 OS << "namespace llvm {\n\n";
2837 +#endif
2838
2839 const std::string &TargetName = Target.getName();
2840
2841 // Emit the shared table of differential lists.
2842 - OS << "extern const MCPhysReg " << TargetName << "RegDiffLists[] = {\n";
2843 +#ifdef CAPSTONE
2844 + OS << "static"
2845 +#else
2846 + OS << "extern"
2847 +#endif
2848 + << " const MCPhysReg " << TargetName << "RegDiffLists[] = {\n";
2849 DiffSeqs.emit(OS, printDiff16);
2850 OS << "};\n\n";
2851
2852 +#ifndef CAPSTONE
2853 // Emit the shared table of regunit lane mask sequences.
2854 OS << "extern const LaneBitmask " << TargetName << "LaneMaskLists[] = {\n";
2855 LaneMaskSeqs.emit(OS, printMask, "LaneBitmask::getAll()");
2856 OS << "};\n\n";
2857 +#endif
2858
2859 // Emit the table of sub-register indexes.
2860 - OS << "extern const uint16_t " << TargetName << "SubRegIdxLists[] = {\n";
2861 +#ifdef CAPSTONE
2862 + OS << "static"
2863 +#else
2864 + OS << "extern"
2865 +#endif
2866 + << " const uint16_t " << TargetName << "SubRegIdxLists[] = {\n";
2867 SubRegIdxSeqs.emit(OS, printSubRegIndex);
2868 OS << "};\n\n";
2869
2870 +#ifndef CAPSTONE
2871 // Emit the table of sub-register index sizes.
2872 OS << "extern const MCRegisterInfo::SubRegCoveredBits "
2873 << TargetName << "SubRegIdxRanges[] = {\n";
2874 @@ -989,14 +1039,22 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
2875 << Idx.getName() << "\n";
2876 }
2877 OS << "};\n\n";
2878 +#endif
2879
2880 // Emit the string table.
2881 RegStrings.layout();
2882 +#ifndef CAPSTONE
2883 OS << "extern const char " << TargetName << "RegStrings[] = {\n";
2884 RegStrings.emit(OS, printChar);
2885 OS << "};\n\n";
2886 +#endif
2887
2888 - OS << "extern const MCRegisterDesc " << TargetName
2889 +#ifdef CAPSTONE
2890 + OS << "static"
2891 +#else
2892 + OS << "extern"
2893 +#endif
2894 + << " const MCRegisterDesc " << TargetName
2895 << "RegDesc[] = { // Descriptors\n";
2896 OS << " { " << RegStrings.get("") << ", 0, 0, 0, 0, 0 },\n";
2897
2898 @@ -1012,6 +1070,7 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
2899 }
2900 OS << "};\n\n"; // End of register descriptors...
2901
2902 +#ifndef CAPSTONE
2903 // Emit the table of register unit roots. Each regunit has one or two root
2904 // registers.
2905 OS << "extern const MCPhysReg " << TargetName << "RegUnitRoots[][2] = {\n";
2906 @@ -1025,11 +1084,14 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
2907 OS << " },\n";
2908 }
2909 OS << "};\n\n";
2910 +#endif
2911
2912 const auto &RegisterClasses = RegBank.getRegClasses();
2913
2914 // Loop over all of the register classes... emitting each one.
2915 +#ifndef CAPSTONE
2916 OS << "namespace { // Register classes...\n";
2917 +#endif
2918
2919 SequenceToOffsetTable<std::string> RegClassStrings;
2920
2921 @@ -1044,15 +1106,28 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
2922
2923 // Emit the register list now.
2924 OS << " // " << Name << " Register Class...\n"
2925 - << " const MCPhysReg " << Name
2926 + << " "
2927 +#ifdef CAPSTONE
2928 + << "static "
2929 +#endif
2930 + << "const MCPhysReg " << Name
2931 << "[] = {\n ";
2932 for (Record *Reg : Order) {
2933 - OS << getQualifiedName(Reg) << ", ";
2934 +#ifdef CAPSTONE
2935 + OS << NAME_PREFIX Reg->getName()
2936 +#else
2937 + OS << getQualifiedName(Reg)
2938 +#endif
2939 + << ", ";
2940 }
2941 OS << "\n };\n\n";
2942
2943 OS << " // " << Name << " Bit set.\n"
2944 - << " const uint8_t " << Name
2945 + << " "
2946 +#ifdef CAPSTONE
2947 + << "static "
2948 +#endif
2949 + << "const uint8_t " << Name
2950 << "Bits[] = {\n ";
2951 BitVectorEmitter BVE;
2952 for (Record *Reg : Order) {
2953 @@ -1062,14 +1137,23 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
2954 OS << "\n };\n\n";
2955
2956 }
2957 +#ifndef CAPSTONE
2958 OS << "} // end anonymous namespace\n\n";
2959 +#endif
2960
2961 RegClassStrings.layout();
2962 +#ifndef CAPSTONE
2963 OS << "extern const char " << TargetName << "RegClassStrings[] = {\n";
2964 RegClassStrings.emit(OS, printChar);
2965 OS << "};\n\n";
2966 +#endif
2967
2968 - OS << "extern const MCRegisterClass " << TargetName
2969 +#ifdef CAPSTONE
2970 + OS << "static"
2971 +#else
2972 + OS << "extern"
2973 +#endif
2974 + << " const MCRegisterClass " << TargetName
2975 << "MCRegisterClasses[] = {\n";
2976
2977 for (const auto &RC : RegisterClasses) {
2978 @@ -1077,13 +1161,20 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
2979 OS << " { " << RC.getName() << ", " << RC.getName() << "Bits, "
2980 << RegClassStrings.get(RC.getName()) << ", "
2981 << RC.getOrder().size() << ", sizeof(" << RC.getName() << "Bits), "
2982 - << RC.getQualifiedName() + "RegClassID" << ", "
2983 +#ifdef CAPSTONE
2984 + << NAME_PREFIX RC.getName()
2985 +#else
2986 + << RC.getQualifiedName()
2987 +#endif
2988 + << "RegClassID" << ", "
2989 + //<< RegSize/8 << ", "
2990 << RC.CopyCost << ", "
2991 << ( RC.Allocatable ? "true" : "false" ) << " },\n";
2992 }
2993
2994 OS << "};\n\n";
2995
2996 +#ifndef CAPSTONE
2997 EmitRegMappingTables(OS, Regs, false);
2998
2999 // Emit Reg encoding table
3000 @@ -1102,7 +1193,9 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
3001 OS << " " << Value << ",\n";
3002 }
3003 OS << "};\n"; // End of HW encoding table
3004 +#endif
3005
3006 +#ifndef CAPSTONE
3007 // MCRegisterInfo initialization routine.
3008 OS << "static inline void Init" << TargetName
3009 << "MCRegisterInfo(MCRegisterInfo *RI, unsigned RA, "
3010 @@ -1123,7 +1216,12 @@ RegisterInfoEmitter::runMCDesc(raw_ostream &OS, CodeGenTarget &Target,
3011 OS << "}\n\n";
3012
3013 OS << "} // end namespace llvm\n\n";
3014 - OS << "#endif // GET_REGINFO_MC_DESC\n\n";
3015 +#endif
3016 + OS << "#endif // GET_REGINFO_MC_DESC\n"
3017 +#ifndef CAPSTONE
3018 + << "\n"
3019 +#endif
3020 + ;
3021 }
3022
3023 void
3024 @@ -1605,8 +1703,10 @@ void RegisterInfoEmitter::run(raw_ostream &OS) {
3025 CodeGenRegBank &RegBank = Target.getRegBank();
3026 runEnums(OS, Target, RegBank);
3027 runMCDesc(OS, Target, RegBank);
3028 +#ifndef CAPSTONE
3029 runTargetHeader(OS, Target, RegBank);
3030 runTargetDesc(OS, Target, RegBank);
3031 +#endif
3032
3033 if (RegisterInfoDebug)
3034 debugDump(errs());
3035 diff --git a/llvm/utils/TableGen/SubtargetEmitter.cpp b/llvm/utils/TableGen/SubtargetEmitter.cpp
3036 index 792c957ea..3ddfd1371 100644
3037 --- a/llvm/utils/TableGen/SubtargetEmitter.cpp
3038 +++ b/llvm/utils/TableGen/SubtargetEmitter.cpp
3039 @@ -149,7 +149,9 @@ void SubtargetEmitter::Enumeration(raw_ostream &OS) {
3040 if (N > MAX_SUBTARGET_FEATURES)
3041 PrintFatalError("Too many subtarget features! Bump MAX_SUBTARGET_FEATURES.");
3042
3043 +#ifndef CAPSTONE
3044 OS << "namespace " << Target << " {\n";
3045 +#endif
3046
3047 // Open enumeration.
3048 OS << "enum {\n";
3049 @@ -160,12 +162,22 @@ void SubtargetEmitter::Enumeration(raw_ostream &OS) {
3050 Record *Def = DefList[i];
3051
3052 // Get and emit name
3053 - OS << " " << Def->getName() << " = " << i << ",\n";
3054 + OS << " "
3055 +#ifdef CAPSTONE
3056 + << Target << "_"
3057 +#endif
3058 + << Def->getName() << " = "
3059 +#ifdef CAPSTONE
3060 + << "1ULL << "
3061 +#endif
3062 + << i << ",\n";
3063 }
3064
3065 // Close enumeration and namespace
3066 OS << "};\n";
3067 +#ifndef CAPSTONE
3068 OS << "} // end namespace " << Target << "\n";
3069 +#endif
3070 }
3071
3072 //
3073 @@ -1786,14 +1798,27 @@ void SubtargetEmitter::EmitMCInstrAnalysisPredicateFunctions(raw_ostream &OS) {
3074 void SubtargetEmitter::run(raw_ostream &OS) {
3075 emitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
3076
3077 +#ifdef CAPSTONE
3078 + OS << "/* Capstone Disassembly Engine, http://www.capstone-engine.org */\n"
3079 + "/* By Nguyen Anh Quynh <[email protected]>, 2013-2015 */\n"
3080 + "\n";
3081 +#endif
3082 +
3083 OS << "\n#ifdef GET_SUBTARGETINFO_ENUM\n";
3084 OS << "#undef GET_SUBTARGETINFO_ENUM\n\n";
3085
3086 +#ifndef CAPSTONE
3087 OS << "namespace llvm {\n";
3088 +#endif
3089 Enumeration(OS);
3090 +#ifdef CAPSTONE
3091 + OS << "\n";
3092 +#else
3093 OS << "} // end namespace llvm\n\n";
3094 +#endif
3095 OS << "#endif // GET_SUBTARGETINFO_ENUM\n\n";
3096
3097 +#ifndef CAPSTONE
3098 OS << "\n#ifdef GET_SUBTARGETINFO_MC_DESC\n";
3099 OS << "#undef GET_SUBTARGETINFO_MC_DESC\n\n";
3100
3101 @@ -1942,6 +1967,7 @@ void SubtargetEmitter::run(raw_ostream &OS) {
3102 OS << "#endif // GET_SUBTARGETINFO_CTOR\n\n";
3103
3104 EmitMCInstrAnalysisPredicateFunctions(OS);
3105 +#endif
3106 }
3107
3108 namespace llvm {
3109 diff --git a/llvm/utils/TableGen/TableGen.cpp b/llvm/utils/TableGen/TableGen.cpp
3110 index 38f81dc39..abe172be2 100644
3111 --- a/llvm/utils/TableGen/TableGen.cpp
3112 +++ b/llvm/utils/TableGen/TableGen.cpp
3113 @@ -27,6 +27,8 @@ enum ActionType {
3114 GenEmitter,
3115 GenRegisterInfo,
3116 GenInstrInfo,
3117 + GenMappingInsn,
3118 + GenInsnNameMaps,
3119 GenInstrDocs,
3120 GenAsmWriter,
3121 GenAsmMatcher,
3122 @@ -68,6 +70,10 @@ namespace {
3123 "Generate registers and register classes info"),
3124 clEnumValN(GenInstrInfo, "gen-instr-info",
3125 "Generate instruction descriptions"),
3126 + clEnumValN(GenMappingInsn, "gen-mapping-insn",
3127 + ""),
3128 + clEnumValN(GenInsnNameMaps, "gen-insn-name-maps",
3129 + ""),
3130 clEnumValN(GenInstrDocs, "gen-instr-docs",
3131 "Generate instruction documentation"),
3132 clEnumValN(GenCallingConv, "gen-callingconv",
3133 @@ -143,6 +149,12 @@ bool LLVMTableGenMain(raw_ostream &OS, RecordKeeper &Records) {
3134 case GenInstrInfo:
3135 EmitInstrInfo(Records, OS);
3136 break;
3137 + case GenMappingInsn:
3138 + EmitMappingInsn(Records, OS);
3139 + break;
3140 + case GenInsnNameMaps:
3141 + EmitInsnNameMaps(Records, OS);
3142 + break;
3143 case GenInstrDocs:
3144 EmitInstrDocs(Records, OS);
3145 break;
3146 diff --git a/llvm/utils/TableGen/TableGenBackends.h b/llvm/utils/TableGen/TableGenBackends.h
3147 index 135ec65c0..82f787dba 100644
3148 --- a/llvm/utils/TableGen/TableGenBackends.h
3149 +++ b/llvm/utils/TableGen/TableGenBackends.h
3150 @@ -74,6 +74,8 @@ void EmitDFAPacketizer(RecordKeeper &RK, raw_ostream &OS);
3151 void EmitDisassembler(RecordKeeper &RK, raw_ostream &OS);
3152 void EmitFastISel(RecordKeeper &RK, raw_ostream &OS);
3153 void EmitInstrInfo(RecordKeeper &RK, raw_ostream &OS);
3154 +void EmitMappingInsn(RecordKeeper &RK, raw_ostream &OS);
3155 +void EmitInsnNameMaps(RecordKeeper &RK, raw_ostream &OS);
3156 void EmitInstrDocs(RecordKeeper &RK, raw_ostream &OS);
3157 void EmitPseudoLowering(RecordKeeper &RK, raw_ostream &OS);
3158 void EmitCompressInst(RecordKeeper &RK, raw_ostream &OS);
3159 --
3160 2.20.1
3161