Debian Codegraph @

contrib/riscv_update/0002-update-TableGen-for-generate-RISCV-port-inc-for-CAPS.patch

raw
1 From 40ac7444e7f3679fad852564acca4f30f47fb52d Mon Sep 17 00:00:00 2001
2 From: fanfuqiang <[email protected]>
3 Date: Thu, 28 Feb 2019 01:37:55 +0800
4 Subject: [PATCH] update TableGen for generate RISCV port inc for CAPSTONE
5
6 ---
7 llvm/lib/Target/RISCV/CMakeLists.txt | 2 +-
8 llvm/utils/TableGen/AsmWriterEmitter.cpp | 175 ++++++++++++++++--
9 .../utils/TableGen/FixedLenDecoderEmitter.cpp | 122 ++++++------
10 llvm/utils/TableGen/InstrInfoEmitter.cpp | 14 +-
11 llvm/utils/TableGen/RegisterInfoEmitter.cpp | 20 +-
12 llvm/utils/TableGen/TableGen.cpp | 6 +
13 6 files changed, 249 insertions(+), 90 deletions(-)
14
15 diff --git a/llvm/lib/Target/RISCV/CMakeLists.txt b/llvm/lib/Target/RISCV/CMakeLists.txt
16 index 1821f4b01..603aa3f54 100644
17 --- a/llvm/lib/Target/RISCV/CMakeLists.txt
18 +++ b/llvm/lib/Target/RISCV/CMakeLists.txt
19 @@ -6,7 +6,7 @@ tablegen(LLVM RISCVGenCompressInstEmitter.inc -gen-compress-inst-emitter)
20 tablegen(LLVM RISCVGenDAGISel.inc -gen-dag-isel)
21 tablegen(LLVM RISCVGenDisassemblerTables.inc -gen-disassembler)
22 tablegen(LLVM RISCVGenInstrInfo.inc -gen-instr-info)
23 -tablegen(LLVM RISCVGenMappingInsn.inc -gen-mapping-insn)
24 +tablegen(LLVM RISCVMappingInsn.inc -gen-mapping-insn)
25 tablegen(LLVM RISCVGenInsnNameMaps.inc -gen-insn-name-maps)
26 tablegen(LLVM RISCVGenMCCodeEmitter.inc -gen-emitter)
27 tablegen(LLVM RISCVGenMCPseudoLowering.inc -gen-pseudo-lowering)
28 diff --git a/llvm/utils/TableGen/AsmWriterEmitter.cpp b/llvm/utils/TableGen/AsmWriterEmitter.cpp
29 index c24dc6052..ac82573fe 100644
30 --- a/llvm/utils/TableGen/AsmWriterEmitter.cpp
31 +++ b/llvm/utils/TableGen/AsmWriterEmitter.cpp
32 @@ -270,12 +270,13 @@ static void UnescapeString(std::string &Str) {
33 /// implementation. Destroys all instances of AsmWriterInst information, by
34 /// clearing the Instructions vector.
35 void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
36 +#ifdef CAPSTONE
37 + bool PassSubtarget = false;
38 +#else
39 Record *AsmWriter = Target.getAsmWriter();
40 -#ifndef CAPSTONE
41 StringRef ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
42 -#endif
43 bool PassSubtarget = AsmWriter->getValueAsInt("PassSubtarget");
44 -
45 +#endif
46 O <<
47 "/// printInstruction - This method is automatically generated by tablegen\n"
48 "/// from the instruction set description.\n"
49 @@ -434,7 +435,11 @@ void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
50 }
51
52 // Emit the initial tab character.
53 -#ifndef CAPSTONE
54 +#ifdef CAPSTONE
55 + O << "#ifndef CAPSTONE_DIET\n"
56 + << " SStream_concat0(O, \"\\t\");\n"
57 + << "#endif\n\n";
58 +#else
59 O << " O << \"\\t\";\n\n";
60 #endif
61
62 @@ -493,10 +498,10 @@ void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
63 << ((1 << NumBits)-1) << ") {\n"
64 << " default: "
65 #ifdef CAPSTONE
66 - << "assert(0);\n"
67 -#endif
68 + << "assert(0 && \"Invalid command number.\");\n";
69 +#else
70 << "llvm_unreachable(\"Invalid command number.\");\n";
71 -
72 +#endif
73 // Print out all the cases.
74 for (unsigned j = 0, e = Commands.size(); j != e; ++j) {
75 O << " case " << j << ":\n";
76 @@ -576,9 +581,7 @@ emitRegisterNameString(raw_ostream &O, StringRef AltName,
77 }
78
79 StringTable.layout();
80 -#ifdef CAPSTONE
81 - O << "#ifndef CAPSTONE_DIET\n";
82 -#endif
83 +
84 O << " static const char AsmStrs" << AltName << "[] = {\n";
85 StringTable.emit(O, printChar);
86 O << " };\n\n";
87 @@ -625,7 +628,8 @@ void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
88 O << " "
89 << "assert(RegNo && RegNo < " << (Registers.size()+1)
90 << " && \"Invalid register number!\");\n"
91 - << "\n";
92 + << "\n"
93 + << "#ifndef CAPSTONE_DIET\n";
94
95 if (hasAltNames) {
96 for (const Record *R : AltNameIndices)
97 @@ -636,7 +640,7 @@ void AsmWriterEmitter::EmitGetRegisterName(raw_ostream &O) {
98 if (hasAltNames) {
99 O << " switch(AltIdx) {\n"
100 #ifdef CAPSTONE
101 - << " default: assert(0);\n";
102 + << " default: assert(0 && \"Invalid register alt name index!\");\n";
103 #else
104 << " default: llvm_unreachable(\"Invalid register alt name index!\");\n";
105 #endif
106 @@ -886,7 +890,9 @@ void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
107
108 IAPrinter IAP(CGA.Result->getAsString(), FlatAliasAsmString);
109
110 +#ifndef CAPSTONE // Silence the compiler waring.
111 StringRef Namespace = Target.getName();
112 +#endif
113 std::vector<Record *> ReqFeatures;
114 if (PassSubtarget) {
115 // We only consider ReqFeatures predicates if PassSubtarget
116 @@ -902,7 +908,11 @@ void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
117 NumMIOps += ResultInstOpnd.MINumOperands;
118
119 std::string Cond;
120 +#ifdef CAPSTONE
121 + Cond = std::string("MCInst_getNumOperands(MI) == ") + utostr(NumMIOps);
122 +#else
123 Cond = std::string("MI->getNumOperands() == ") + utostr(NumMIOps);
124 +#endif
125 IAP.addCond(Cond);
126
127 bool CantHandle = false;
128 @@ -926,9 +936,11 @@ void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
129 }
130 break;
131 }
132 -
133 +#ifdef CAPSTONE
134 + std::string Op = "MCInst_getOperand(MI, " + utostr(MIOpNum) + ")";
135 +#else
136 std::string Op = "MI->getOperand(" + utostr(MIOpNum) + ")";
137 -
138 +#endif
139 const CodeGenInstAlias::ResultOperand &RO = CGA.ResultOperands[i];
140
141 switch (RO.Kind) {
142 @@ -954,19 +966,39 @@ void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
143 if (Rec->isSubClassOf("RegisterOperand"))
144 Rec = Rec->getValueAsDef("RegClass");
145 if (Rec->isSubClassOf("RegisterClass")) {
146 +#ifdef CAPSTONE
147 + IAP.addCond("MCOperand_isReg(" + Op + ")");
148 +#else
149 IAP.addCond(Op + ".isReg()");
150 +#endif
151
152 if (!IAP.isOpMapped(ROName)) {
153 IAP.addOperand(ROName, MIOpNum, PrintMethodIdx);
154 Record *R = CGA.ResultOperands[i].getRecord();
155 if (R->isSubClassOf("RegisterOperand"))
156 R = R->getValueAsDef("RegClass");
157 +
158 +#ifdef CAPSTONE
159 + Cond = std::string("MCRegisterClass_contains(") +
160 + "MCRegisterInfo_getRegClass(" + "MRI, " +
161 + Target.getName().str() + "_" + R->getName().str() + "RegClassID)" +
162 + ", " +
163 + "MCOperand_getReg(" + Op + "))";
164 +#else
165 Cond = std::string("MRI.getRegClass(") + Target.getName().str() +
166 "::" + R->getName().str() + "RegClassID).contains(" + Op +
167 ".getReg())";
168 +#endif
169 +
170 } else {
171 +#ifdef CAPSTONE
172 + Cond = std::string("MCOperand_getReg(") + Op + ") == " +
173 + "MCOperand_getReg(MCInst_getOperand(MI, " +
174 + utostr(IAP.getOpIndex(ROName)) + "))";
175 +#else
176 Cond = Op + ".getReg() == MI->getOperand(" +
177 utostr(IAP.getOpIndex(ROName)) + ").getReg()";
178 +#endif
179 }
180 } else {
181 // Assume all printable operands are desired for now. This can be
182 @@ -984,8 +1016,12 @@ void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
183 break; // No conditions on this operand at all
184 }
185 Cond = (Target.getName() + ClassName + "ValidateMCOperand(" + Op +
186 + #ifdef CAPSTONE
187 + ", " + utostr(Entry) + ")").str();
188 + #else
189 ", STI, " + utostr(Entry) + ")")
190 .str();
191 + #endif
192 }
193 // for all subcases of ResultOperand::K_Record:
194 IAP.addCond(Cond);
195 @@ -994,9 +1030,15 @@ void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
196 case CodeGenInstAlias::ResultOperand::K_Imm: {
197 // Just because the alias has an immediate result, doesn't mean the
198 // MCInst will. An MCExpr could be present, for example.
199 +#ifdef CAPSTONE
200 + IAP.addCond("MCOperand_isImm(" + Op + ")");
201 + Cond = "MCOperand_getImm(" + Op + ") == " +
202 + itostr(CGA.ResultOperands[i].getImm());
203 +#else
204 IAP.addCond(Op + ".isImm()");
205 -
206 Cond = Op + ".getImm() == " + itostr(CGA.ResultOperands[i].getImm());
207 +#endif
208 +
209 IAP.addCond(Cond);
210 break;
211 }
212 @@ -1008,8 +1050,14 @@ void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
213 break;
214 }
215
216 +#ifdef CAPSTONE
217 + Cond = "MCOperand_getReg(" + Op + ") == " + Target.getName().str() +
218 + "_" + CGA.ResultOperands[i].getRegister()->getName().str();
219 +#else
220 Cond = Op + ".getReg() == " + Target.getName().str() + "::" +
221 CGA.ResultOperands[i].getRegister()->getName().str();
222 +#endif
223 +
224 IAP.addCond(Cond);
225 break;
226 }
227 @@ -1019,6 +1067,7 @@ void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
228
229 if (CantHandle) continue;
230
231 +#ifndef CAPSTONE
232 for (auto I = ReqFeatures.cbegin(); I != ReqFeatures.cend(); I++) {
233 Record *R = *I;
234 StringRef AsmCondString = R->getValueAsString("AssemblerCondString");
235 @@ -1040,6 +1089,7 @@ void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
236 IAP.addCond(Cond);
237 }
238 }
239 +#endif
240
241 IAPrinterMap[Aliases.first].push_back(std::move(IAP));
242 }
243 @@ -1052,10 +1102,17 @@ void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
244 std::string Header;
245 raw_string_ostream HeaderO(Header);
246
247 +#ifdef CAPSTONE
248 + HeaderO << "\nstatic bool printAliasInstr(MCInst *MI, SStream * OS, void *info)"
249 + << "\n"
250 + << "{\n"
251 + << " MCRegisterInfo *MRI = (MCRegisterInfo *) info;\n";
252 +#else
253 HeaderO << "bool " << Target.getName() << ClassName
254 << "::printAliasInstr(const MCInst"
255 << " *MI, " << (PassSubtarget ? "const MCSubtargetInfo &STI, " : "")
256 << "raw_ostream &OS) {\n";
257 +#endif
258
259 std::string Cases;
260 raw_string_ostream CasesO(Cases);
261 @@ -1079,7 +1136,16 @@ void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
262
263 if (UniqueIAPs.empty()) continue;
264
265 +#ifdef CAPSTONE
266 + // TODO: tricky.
267 + const char* tmpCase = Entry.first.c_str();
268 + assert (Entry.first.size() > 7);
269 + CasesO.indent(2) << "case "
270 + << "RISCV_" << std::string(tmpCase + 7) // strlen("RISCV::) == 7
271 + << ":\n";
272 +#else
273 CasesO.indent(2) << "case " << Entry.first << ":\n";
274 +#endif
275
276 for (IAPrinter *IAP : UniqueIAPs) {
277 CasesO.indent(4);
278 @@ -1100,13 +1166,21 @@ void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
279
280 if (!MCOpPredicates.empty())
281 O << "static bool " << Target.getName() << ClassName
282 +#ifdef CAPSTONE
283 + << "ValidateMCOperand(MCOperand *MCOp,\n"
284 +#else
285 << "ValidateMCOperand(const MCOperand &MCOp,\n"
286 << " const MCSubtargetInfo &STI,\n"
287 +#endif
288 << " unsigned PredicateIndex);\n";
289
290 O << HeaderO.str();
291 O.indent(2) << "const char *AsmString;\n";
292 +#ifdef CAPSTONE
293 + O.indent(2) << "switch (MCInst_getOpcode(MI)) {\n";
294 +#else
295 O.indent(2) << "switch (MI->getOpcode()) {\n";
296 +#endif
297 O.indent(2) << "default: return false;\n";
298 O << CasesO.str();
299 O.indent(2) << "}\n\n";
300 @@ -1114,14 +1188,27 @@ void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
301 // Code that prints the alias, replacing the operands with the ones from the
302 // MCInst.
303 O << " unsigned I = 0;\n";
304 +#ifdef CAPSTONE
305 + O << " char *tmpString = cs_strdup(AsmString);\n";
306 +#endif
307 O << " while (AsmString[I] != ' ' && AsmString[I] != '\\t' &&\n";
308 O << " AsmString[I] != '$' && AsmString[I] != '\\0')\n";
309 O << " ++I;\n";
310 +#ifdef CAPSTONE
311 + O << " tmpString[I] = 0;\n";
312 + O << " SStream_concat0(OS, \"\\t\");\n";
313 + O << " SStream_concat0(OS, tmpString);\n";
314 + O << " SStream_concat0(OS, \"\\n\");\n";
315 +#else
316 O << " OS << '\\t' << StringRef(AsmString, I);\n";
317 -
318 +#endif
319 O << " if (AsmString[I] != '\\0') {\n";
320 O << " if (AsmString[I] == ' ' || AsmString[I] == '\\t') {\n";
321 +#ifdef CAPSTONE
322 + O << " SStream_concat0(OS, \"\\t\");\n";
323 +#else
324 O << " OS << '\\t';\n";
325 +#endif
326 O << " ++I;\n";
327 O << " }\n";
328 O << " do {\n";
329 @@ -1131,15 +1218,28 @@ void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
330 O << " ++I;\n";
331 O << " int OpIdx = AsmString[I++] - 1;\n";
332 O << " int PrintMethodIdx = AsmString[I++] - 1;\n";
333 +#ifdef CAPSTONE
334 + O << " printCustomAliasOperand(MI, OpIdx, PrintMethodIdx, OS);\n";
335 +#else
336 O << " printCustomAliasOperand(MI, OpIdx, PrintMethodIdx, ";
337 O << (PassSubtarget ? "STI, " : "");
338 O << "OS);\n";
339 +#endif
340 O << " } else\n";
341 +
342 +#ifdef CAPSTONE
343 + O << " printOperand(MI, (unsigned)(AsmString[I++]) - 1, OS);\n";
344 +#else
345 O << " printOperand(MI, unsigned(AsmString[I++]) - 1, ";
346 O << (PassSubtarget ? "STI, " : "");
347 O << "OS);\n";
348 +#endif
349 O << " } else {\n";
350 +#ifdef CAPSTONE
351 + O << " SStream_concat0(OS, &AsmString[I++]);\n";
352 +#else
353 O << " OS << AsmString[I++];\n";
354 +#endif
355 O << " }\n";
356 O << " } while (AsmString[I] != '\\0');\n";
357 O << " }\n\n";
358 @@ -1150,25 +1250,48 @@ void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
359 //////////////////////////////
360 // Write out the printCustomAliasOperand function
361 //////////////////////////////
362 -
363 +#ifdef CAPSTONE
364 + O << "static void "
365 +#else
366 O << "void " << Target.getName() << ClassName << "::"
367 +#endif
368 << "printCustomAliasOperand(\n"
369 +#ifdef CAPSTONE
370 + << " MCInst *MI, unsigned OpIdx,\n"
371 +#else
372 << " const MCInst *MI, unsigned OpIdx,\n"
373 +#endif
374 << " unsigned PrintMethodIdx,\n"
375 +#ifdef CAPSTONE
376 + << " SStream *OS) {\n";
377 +#else
378 << (PassSubtarget ? " const MCSubtargetInfo &STI,\n" : "")
379 << " raw_ostream &OS) {\n";
380 +#endif
381 if (PrintMethods.empty())
382 +#ifdef CAPSTONE
383 + O << " assert(0 && \"Unknown PrintMethod kind\");\n";
384 +#else
385 O << " llvm_unreachable(\"Unknown PrintMethod kind\");\n";
386 +#endif
387 else {
388 O << " switch (PrintMethodIdx) {\n"
389 << " default:\n"
390 +#ifdef CAPSTONE
391 + << " assert(0 && \"Unknown PrintMethod kind\");\n"
392 +#else
393 << " llvm_unreachable(\"Unknown PrintMethod kind\");\n"
394 +#endif
395 << " break;\n";
396
397 for (unsigned i = 0; i < PrintMethods.size(); ++i) {
398 O << " case " << i << ":\n"
399 << " " << PrintMethods[i] << "(MI, OpIdx, "
400 +#ifdef CAPSTONE
401 + << "OS);\n"
402 +#else
403 << (PassSubtarget ? "STI, " : "") << "OS);\n"
404 +#endif
405 << " break;\n";
406 }
407 O << " }\n";
408 @@ -1177,9 +1300,20 @@ void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
409
410 if (!MCOpPredicates.empty()) {
411 O << "static bool " << Target.getName() << ClassName
412 +#ifdef CAPSTONE
413 + << "ValidateMCOperand(MCOperand *MCOp,\n"
414 +#else
415 << "ValidateMCOperand(const MCOperand &MCOp,\n"
416 << " const MCSubtargetInfo &STI,\n"
417 - << " unsigned PredicateIndex) {\n"
418 +#endif
419 + << " unsigned PredicateIndex) {\n"
420 +#ifdef CAPSTONE
421 + << " // TODO: need some constant untils operate the MCOperand,\n"
422 + << " // but current CAPSTONE does't have.\n"
423 + << " // So, We just return true\n"
424 + << " return true;\n\n"
425 + << "#if 0\n"
426 +#endif
427 << " switch (PredicateIndex) {\n"
428 << " default:\n"
429 << " llvm_unreachable(\"Unknown MCOperandPredicate kind\");\n"
430 @@ -1195,6 +1329,9 @@ void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
431 llvm_unreachable("Unexpected MCOperandPredicate field!");
432 }
433 O << " }\n"
434 +#ifdef CAPSTONE
435 + << "#endif\n"
436 +#endif
437 << "}\n\n";
438 }
439
440 @@ -1228,7 +1365,7 @@ void AsmWriterEmitter::run(raw_ostream &O) {
441 #endif
442 EmitPrintInstruction(O);
443 EmitGetRegisterName(O);
444 -#ifndef CAPSTONE
445 +#ifdef CAPSTONE
446 EmitPrintAliasInstruction(O);
447 #endif
448 }
449 diff --git a/llvm/utils/TableGen/FixedLenDecoderEmitter.cpp b/llvm/utils/TableGen/FixedLenDecoderEmitter.cpp
450 index 3db428dfa..e1bfaa934 100644
451 --- a/llvm/utils/TableGen/FixedLenDecoderEmitter.cpp
452 +++ b/llvm/utils/TableGen/FixedLenDecoderEmitter.cpp
453 @@ -946,13 +946,6 @@ void FixedLenDecoderEmitter::emitTable(formatted_raw_ostream &OS,
454 void FixedLenDecoderEmitter::
455 emitPredicateFunction(formatted_raw_ostream &OS, PredicateSet &Predicates,
456 unsigned Indentation) const {
457 -#ifdef CAPSTONE
458 - OS.indent(Indentation) << "static bool getbool(uint64_t b)\n";
459 - OS.indent(Indentation) << "{\n";
460 - OS.indent(Indentation) << "\treturn b != 0;\n";
461 - OS.indent(Indentation) << "}\n\n";
462 -#endif
463 -
464 // The predicate function is just a big switch statement based on the
465 // input predicate index.
466 OS.indent(Indentation) << "static bool checkDecoderPredicate(unsigned Idx, "
467 @@ -966,26 +959,25 @@ emitPredicateFunction(formatted_raw_ostream &OS, PredicateSet &Predicates,
468 OS.indent(Indentation) << "switch (Idx) {\n";
469 OS.indent(Indentation) << "default: "
470 #ifdef CAPSTONE
471 - << "assert(0);\n"
472 -#endif
473 + << "assert(0 && \"Invalid index!\");\n";
474 +#else
475 << "llvm_unreachable(\"Invalid index!\");\n";
476 +#endif
477 unsigned Index = 0;
478 for (const auto &Predicate : Predicates) {
479 OS.indent(Indentation) << "case " << Index++ << ":\n";
480 OS.indent(Indentation+2) << "return "
481 -#ifdef CAPSTONE
482 - << "getbool"
483 -#endif
484 - << "(" << Predicate << ");\n";
485 + << Predicate << ";\n";
486 }
487 OS.indent(Indentation) << "}\n";
488 } else {
489 // No case statement to emit
490 OS.indent(Indentation)
491 #ifdef CAPSTONE
492 - << "assert(0);\n"
493 -#endif
494 + << "assert(0 && \"Invalid index!\");\n";
495 +#else
496 << "llvm_unreachable(\"Invalid index!\");\n";
497 +#endif
498 }
499 Indentation -= 2;
500 OS.indent(Indentation) << "}\n\n";
501 @@ -998,10 +990,11 @@ emitDecoderFunction(formatted_raw_ostream &OS, DecoderSet &Decoders,
502 // input decoder index.
503 #ifdef CAPSTONE
504 #define EDF_EOL " \\\n"
505 - OS.indent(Indentation) << "#define DecodeToMCInst(fname,fieldname, InsnType) \\\n";
506 - OS.indent(Indentation) << "static DecodeStatus fname(DecodeStatus S, unsigned Idx, InsnType insn, MCInst *MI, \\\n";
507 - OS.indent(Indentation) << " uint64_t Address, const void *Decoder) \\\n";
508 - OS.indent(Indentation) << "{ \\\n";
509 + OS.indent(Indentation) << "#define DecodeToMCInst(fname, fieldname, InsnType) \\\n";
510 + OS.indent(Indentation) << "static DecodeStatus fname(DecodeStatus S, unsigned Idx,"
511 + << " InsnType insn, MCInst *MI, \\\n";
512 + OS.indent(Indentation) << " uint64_t Address, const void *Decoder,\\\n";
513 + OS.indent(Indentation) << " bool *DecodeComplete) {\\\n";
514 #else
515 #define EDF_EOL "\n"
516 OS.indent(Indentation) << "template<typename InsnType>\n";
517 @@ -1011,16 +1004,18 @@ emitDecoderFunction(formatted_raw_ostream &OS, DecoderSet &Decoders,
518 << "Address, const void *Decoder, bool &DecodeComplete) {\n";
519 #endif
520 Indentation += 2;
521 -#ifndef CAPSTONE
522 +#ifdef CAPSTONE
523 + OS.indent(Indentation) << "*DecodeComplete = true;\\\n";
524 +#else
525 OS.indent(Indentation) << "DecodeComplete = true;\n";
526 #endif
527 OS.indent(Indentation) << "InsnType tmp;" EDF_EOL;
528 OS.indent(Indentation) << "switch (Idx) {" EDF_EOL;
529 OS.indent(Indentation) << "default:"
530 -#ifndef CAPSTONE
531 - << " llvm_unreachable(\"Invalid index!\");\n";
532 +#ifdef CAPSTONE
533 + << " assert(0 && \"Invalid index!\");\\\n";
534 #else
535 - << " assert(0);\\\n";
536 + << " llvm_unreachable(\"Invalid index!\");\n";
537 #endif
538 unsigned Index = 0;
539 for (const auto &Decoder : Decoders) {
540 @@ -1174,8 +1169,27 @@ void FilterChooser::emitBinaryParser(raw_ostream &o, unsigned &Indentation,
541
542 if (Decoder != "") {
543 OpHasCompleteDecoder = OpInfo.HasCompleteDecoder;
544 +#ifdef CAPSTONE
545 + std::string::size_type posOfLeftAngle = 0, posOfRightAngle = 0;
546 + posOfLeftAngle = Decoder.find("<");
547 + posOfRightAngle = Decoder.find(">");
548 + std::string printDecoder = Decoder;
549 + if (posOfLeftAngle != std::string::npos &&
550 + posOfRightAngle != std::string::npos) {
551 + printDecoder = Decoder.substr(0, posOfLeftAngle);
552 + o.indent(Indentation) << Emitter->GuardPrefix
553 + << printDecoder
554 + << "(MI, tmp, Address, Decoder, "
555 + << Decoder.substr(posOfLeftAngle+1, posOfRightAngle-posOfLeftAngle-1);
556 + } else
557 + o.indent(Indentation) << Emitter->GuardPrefix << Decoder
558 + << "(MI, tmp, Address, Decoder";
559 + // trick.
560 + o << ")"
561 +#else
562 o.indent(Indentation) << Emitter->GuardPrefix << Decoder
563 << "(MI, tmp, Address, Decoder)"
564 +#endif
565 << Emitter->GuardPostfix
566 #ifdef CAPSTONE
567 << " return MCDisassembler_Fail; \\\n";
568 @@ -1246,7 +1260,7 @@ static void emitSinglePredicateMatch(raw_ostream &o, StringRef str,
569 const std::string &PredicateNamespace) {
570 if (str[0] == '!')
571 #ifdef CAPSTONE
572 - o << "~(Bits & " << PredicateNamespace << "_"
573 + o << "!(Bits & " << PredicateNamespace << "_"
574 << str.slice(1,str.size()) << ")";
575 #else
576 o << "!Bits[" << PredicateNamespace << "::"
577 @@ -2331,15 +2345,13 @@ static void emitDecodeInstruction(formatted_raw_ostream &OS) {
578 #endif
579
580 #ifdef CAPSTONE
581 - OS << "static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], "
582 -
583 + OS << "#define DecodeInstruction(fname, fieldname, decoder, InsnType) \\\n"
584 + << "static DecodeStatus fname(const uint8_t DecodeTable[], "
585 "MCInst *MI,\\\n"
586 - << " InsnType insn, uint64_t "
587 + << " InsnType insn, uint64_t "
588 "Address,\\\n"
589 - << " const void *DisAsm,\\\n"
590 - << " int feature) {\\\n"
591 - << " uint64_t Bits = getFeatureBits(feature); \\\n"
592 - //<< " const FeatureBitset& Bits = STI.getFeatureBits();\n"
593 + << " const void *DisAsm, int feature) {\\\n"
594 + << " uint64_t Bits = getFeatureBits(feature);\\\n"
595 << "\\\n"
596 << " const uint8_t *Ptr = DecodeTable;\\\n"
597 << " uint32_t CurFieldValue = 0;\\\n"
598 @@ -2353,52 +2365,42 @@ static void emitDecodeInstruction(formatted_raw_ostream &OS) {
599 << " unsigned Start = *++Ptr;\\\n"
600 << " unsigned Len = *++Ptr;\\\n"
601 << " ++Ptr;\\\n"
602 - << " CurFieldValue = fieldFromInstruction(insn, Start, Len);\\\n"
603 + << " CurFieldValue = fieldname(insn, Start, Len);\\\n"
604 << " break;\\\n"
605 << " }\\\n"
606 << " case MCD_OPC_FilterValue: {\\\n"
607 - << " // Decode the field value.\\\n"
608 << " unsigned Len;\\\n"
609 << " InsnType Val = decodeULEB128(++Ptr, &Len);\\\n"
610 << " Ptr += Len;\\\n"
611 - << " // NumToSkip is a plain 24-bit integer.\\\n"
612 << " unsigned NumToSkip = *Ptr++;\\\n"
613 << " NumToSkip |= (*Ptr++) << 8;\\\n"
614 << " NumToSkip |= (*Ptr++) << 16;\\\n"
615 << "\\\n"
616 - << " // Perform the filter operation.\\\n"
617 << " if (Val != CurFieldValue)\\\n"
618 << " Ptr += NumToSkip;\\\n"
619 - << "\\\n"
620 << " break;\\\n"
621 << " }\\\n"
622 << " case MCD_OPC_CheckField: {\\\n"
623 << " unsigned Start = *++Ptr;\\\n"
624 << " unsigned Len = *++Ptr;\\\n"
625 - << " InsnType FieldValue = fieldFromInstruction(insn, Start, Len);\\\n"
626 - << " // Decode the field value.\\\n"
627 + << " InsnType FieldValue = fieldname(insn, Start, Len);\\\n"
628 << " uint32_t ExpectedValue = decodeULEB128(++Ptr, &Len);\\\n"
629 << " Ptr += Len;\\\n"
630 - << " // NumToSkip is a plain 24-bit integer.\\\n"
631 << " unsigned NumToSkip = *Ptr++;\\\n"
632 << " NumToSkip |= (*Ptr++) << 8;\\\n"
633 << " NumToSkip |= (*Ptr++) << 16;\\\n"
634 << "\\\n"
635 - << " // If the actual and expected values don't match, skip.\\\n"
636 << " if (ExpectedValue != FieldValue)\\\n"
637 << " Ptr += NumToSkip;\\\n"
638 << " break;\\\n"
639 << " }\\\n"
640 << " case MCD_OPC_CheckPredicate: {\\\n"
641 << " unsigned Len;\\\n"
642 - << " // Decode the Predicate Index value.\\\n"
643 << " unsigned PIdx = decodeULEB128(++Ptr, &Len);\\\n"
644 << " Ptr += Len;\\\n"
645 - << " // NumToSkip is a plain 24-bit integer.\\\n"
646 << " unsigned NumToSkip = *Ptr++;\\\n"
647 << " NumToSkip |= (*Ptr++) << 8;\\\n"
648 << " NumToSkip |= (*Ptr++) << 16;\\\n"
649 - << " // Check the predicate.\\\n"
650 << " bool Pred;\\\n"
651 << " if (!(Pred = checkDecoderPredicate(PIdx, Bits)))\\\n"
652 << " Ptr += NumToSkip;\\\n"
653 @@ -2407,7 +2409,6 @@ static void emitDecodeInstruction(formatted_raw_ostream &OS) {
654 << " }\\\n"
655 << " case MCD_OPC_Decode: {\\\n"
656 << " unsigned Len;\\\n"
657 - << " // Decode the Opcode value.\\\n"
658 << " unsigned Opc = decodeULEB128(++Ptr, &Len);\\\n"
659 << " Ptr += Len;\\\n"
660 << " unsigned DecodeIdx = decodeULEB128(Ptr, &Len);\\\n"
661 @@ -2416,47 +2417,39 @@ static void emitDecodeInstruction(formatted_raw_ostream &OS) {
662 << " MCInst_clear(MI);\\\n"
663 << " MCInst_setOpcode(MI, Opc);\\\n"
664 << " bool DecodeComplete;\\\n"
665 - << " S = decodeToMCInst(S, DecodeIdx, insn, MI, Address, DisAsm, "
666 - "DecodeComplete);\\\n"
667 + << " S = decoder(S, DecodeIdx, insn, MI, Address, DisAsm, "
668 + "&DecodeComplete);\\\n"
669 << " assert(DecodeComplete);\\\n"
670 << "\\\n"
671 << " return S;\\\n"
672 << " }\\\n"
673 << " case MCD_OPC_TryDecode: {\\\n"
674 << " unsigned Len;\\\n"
675 - << " // Decode the Opcode value.\\\n"
676 << " unsigned Opc = decodeULEB128(++Ptr, &Len);\\\n"
677 << " Ptr += Len;\\\n"
678 << " unsigned DecodeIdx = decodeULEB128(Ptr, &Len);\\\n"
679 << " Ptr += Len;\\\n"
680 - << " // NumToSkip is a plain 24-bit integer.\\\n"
681 << " unsigned NumToSkip = *Ptr++;\\\n"
682 << " NumToSkip |= (*Ptr++) << 8;\\\n"
683 << " NumToSkip |= (*Ptr++) << 16;\\\n"
684 << "\\\n"
685 - << " // Perform the decode operation.\\\n"
686 << " MCInst TmpMI;\\\n"
687 << " MCInst_setOpcode(&TmpMI, Opc);\\\n"
688 - << " bool DecodeComplete;\n"
689 - << " S = decodeToMCInst(S, DecodeIdx, insn, &TmpMI, Address, DisAsm, "
690 - "DecodeComplete);\\\n"
691 + << " bool DecodeComplete;\\\n"
692 + << " S = decoder(S, DecodeIdx, insn, &TmpMI, Address, DisAsm, "
693 + "&DecodeComplete);\\\n"
694 + << "\\\n"
695 << " if (DecodeComplete) {\\\n"
696 - << " // Decoding complete.\\\n"
697 - << " MI = &TmpMI;\\\n"
698 + << " *MI = TmpMI;\\\n"
699 << " return S;\\\n"
700 << " } else {\\\n"
701 << " assert(S == MCDisassembler_Fail);\\\n"
702 - << " // If the decoding was incomplete, skip.\\\n"
703 << " Ptr += NumToSkip;\\\n"
704 - << " // Reset decode status. This also drops a SoftFail status "
705 - "that could be\\\n"
706 - << " // set before the decode attempt.\\\n"
707 << " S = MCDisassembler_Success;\\\n"
708 << " }\\\n"
709 << " break;\\\n"
710 << " }\\\n"
711 << " case MCD_OPC_SoftFail: {\\\n"
712 - << " // Decode the mask values.\\\n"
713 << " unsigned Len;\\\n"
714 << " InsnType PositiveMask = decodeULEB128(++Ptr, &Len);\\\n"
715 << " Ptr += Len;\\\n"
716 @@ -2472,8 +2465,8 @@ static void emitDecodeInstruction(formatted_raw_ostream &OS) {
717 << " }\\\n"
718 << " }\\\n"
719 << " }\\\n"
720 - << " assert(0);\\\n"
721 -
722 + << " assert(0 && \"bogosity detected in disassembler state "
723 + "machine!\");\\\n"
724 #else
725 OS << "template<typename InsnType>\n"
726 << "static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], "
727 @@ -2752,9 +2745,10 @@ void FixedLenDecoderEmitter::run(raw_ostream &o) {
728 emitDecodeInstruction(OS);
729
730 #ifdef CAPSTONE
731 - OS << "FieldFromInstruction(fieldFromInstruction, uint64_t)\n";
732 - OS << "DecodeToMCInst(decodeToMCInst, fieldFromInstruction, uint64_t)\n";
733 - OS << "DecodeInstruction(decodeInstruction, fieldFromInstruction, decodeToMCInst, uint64_t)\n";
734 + OS << "// For RISCV instruction is 32 bits.\n";
735 + OS << "FieldFromInstruction(fieldFromInstruction, uint32_t)\n";
736 + OS << "DecodeToMCInst(decodeToMCInst, fieldFromInstruction, uint32_t)\n";
737 + OS << "DecodeInstruction(decodeInstruction, fieldFromInstruction, decodeToMCInst, uint32_t)\n";
738 #else
739 OS << "\n} // End llvm namespace\n";
740 #endif
741 diff --git a/llvm/utils/TableGen/InstrInfoEmitter.cpp b/llvm/utils/TableGen/InstrInfoEmitter.cpp
742 index 01605184f..e59dace24 100644
743 --- a/llvm/utils/TableGen/InstrInfoEmitter.cpp
744 +++ b/llvm/utils/TableGen/InstrInfoEmitter.cpp
745 @@ -769,6 +769,7 @@ void EmitInstrInfo(RecordKeeper &RK, raw_ostream &OS) {
746 #ifdef CAPSTONE
747 std::string GetPublicName(const CodeGenInstruction *Inst) {
748 std::string Name = Inst->TheDef->getName();
749 +#if 0
750 // Apply backward compatibility fixups.
751 // BRNLE -> BNLER.
752 if (Name.length() >= 5 && Name.substr(0, 5) == "BRAsm") {
753 @@ -785,7 +786,7 @@ std::string GetPublicName(const CodeGenInstruction *Inst) {
754 break;
755 }
756 Name = Name.substr(0, pos) + Name.substr(pos + 3);
757 - }
758 + }f 0
759 // CPSDRxx -> CPSDR.
760 if (Name.length() >= 2) {
761 std::string Suffix2 = Name.substr(Name.length() - 2, 2);
762 @@ -794,7 +795,8 @@ std::string GetPublicName(const CodeGenInstruction *Inst) {
763 Name = Name.substr(0, Name.length() - 2);
764 }
765 }
766 - return "SYSZ_INS_" + Name;
767 +#endif
768 + return "RISCV_INS_" + Name;
769 }
770
771 std::string GetRegisterName(Record *Reg) {
772 @@ -802,6 +804,7 @@ std::string GetRegisterName(Record *Reg) {
773 for (char& c : Name) {
774 c = toupper(c);
775 }
776 +#if 0
777 // R0L, R0D -> R0.
778 if (Name.length() >= 3 &&
779 Name[Name.length() - 3] == 'R' &&
780 @@ -809,7 +812,8 @@ std::string GetRegisterName(Record *Reg) {
781 Name[Name.length() - 1] == 'D')) {
782 Name = Name.substr(0, Name.length() - 3) + Name[Name.length() - 2];
783 }
784 - return "SYSZ_REG_" + Name;
785 +#endif
786 + return "RISCV_REG_" + Name;
787 }
788
789 std::string GetGroupName(Record *Pred) {
790 @@ -817,10 +821,12 @@ std::string GetGroupName(Record *Pred) {
791 for (char& c : Name) {
792 c = toupper(c);
793 }
794 +#if 0
795 if (Name.length() >= 7 && Name.substr(0, 7) == "FEATURE") {
796 Name = Name.substr(7);
797 }
798 - return "SYSZ_GRP_" + Name;
799 +#endif
800 + return "RISCV_GRP_" + Name;
801 }
802
803 void EmitMappingInsn(RecordKeeper &RK, raw_ostream &OS) {
804 diff --git a/llvm/utils/TableGen/RegisterInfoEmitter.cpp b/llvm/utils/TableGen/RegisterInfoEmitter.cpp
805 index 0df306680..cf9c352d7 100644
806 --- a/llvm/utils/TableGen/RegisterInfoEmitter.cpp
807 +++ b/llvm/utils/TableGen/RegisterInfoEmitter.cpp
808 @@ -180,12 +180,20 @@ void RegisterInfoEmitter::runEnums(raw_ostream &OS,
809 #endif
810 }
811
812 -#ifndef CAPSTONE
813 const std::vector<Record*> &RegAltNameIndices = Target.getRegAltNameIndices();
814 // If the only definition is the default NoRegAltName, we don't need to
815 // emit anything.
816 if (RegAltNameIndices.size() > 1) {
817 OS << "\n// Register alternate name indices\n\n";
818 +#ifdef CAPSTONE
819 + OS << "enum {\n";
820 + for (unsigned i = 0, e = RegAltNameIndices.size(); i != e; ++i)
821 + OS << " " << NAME_PREFIX RegAltNameIndices[i]->getName()
822 + << ",\t// " << i << "\n";
823 + OS << " " << NAME_PREFIX "NUM_TARGET_REG_ALT_NAMES = "
824 + << RegAltNameIndices.size() << "\n";
825 + OS << "};\n";
826 +#else
827 if (!Namespace.empty())
828 OS << "namespace " << Namespace << " {\n";
829 OS << "enum {\n";
830 @@ -195,11 +203,19 @@ void RegisterInfoEmitter::runEnums(raw_ostream &OS,
831 OS << "};\n";
832 if (!Namespace.empty())
833 OS << "} // end namespace " << Namespace << "\n\n";
834 +#endif
835 }
836
837 auto &SubRegIndices = Bank.getSubRegIndices();
838 if (!SubRegIndices.empty()) {
839 OS << "\n// Subregister indices\n\n";
840 +#ifdef CAPSTONE
841 + OS << "enum {\n" << " " << NAME_PREFIX "NoSubRegister,\n";
842 + unsigned i = 0;
843 + for (const auto &Idx : SubRegIndices)
844 + OS << " " << NAME_PREFIX Idx.getName() << ",\t// " << ++i << "\n";
845 + OS << " " << NAME_PREFIX "NUM_TARGET_SUBREGS\n};\n";
846 +#else
847 std::string Namespace = SubRegIndices.front().getNamespace();
848 if (!Namespace.empty())
849 OS << "namespace " << Namespace << " {\n";
850 @@ -210,8 +226,8 @@ void RegisterInfoEmitter::runEnums(raw_ostream &OS,
851 OS << " NUM_TARGET_SUBREGS\n};\n";
852 if (!Namespace.empty())
853 OS << "} // end namespace " << Namespace << "\n\n";
854 - }
855 #endif
856 + }
857
858 #ifndef CAPSTONE
859 OS << "} // end namespace llvm\n\n";
860 diff --git a/llvm/utils/TableGen/TableGen.cpp b/llvm/utils/TableGen/TableGen.cpp
861 index 9e2a868be..7ec93c0e0 100644
862 --- a/llvm/utils/TableGen/TableGen.cpp
863 +++ b/llvm/utils/TableGen/TableGen.cpp
864 @@ -27,8 +27,10 @@ enum ActionType {
865 GenEmitter,
866 GenRegisterInfo,
867 GenInstrInfo,
868 +#ifdef CAPSTONE
869 GenMappingInsn,
870 GenInsnNameMaps,
871 +#endif
872 GenInstrDocs,
873 GenAsmWriter,
874 GenAsmMatcher,
875 @@ -76,10 +78,12 @@ namespace {
876 "Generate registers and register classes info"),
877 clEnumValN(GenInstrInfo, "gen-instr-info",
878 "Generate instruction descriptions"),
879 +#ifdef CAPSTONE
880 clEnumValN(GenMappingInsn, "gen-mapping-insn",
881 ""),
882 clEnumValN(GenInsnNameMaps, "gen-insn-name-maps",
883 ""),
884 +#endif
885 clEnumValN(GenInstrDocs, "gen-instr-docs",
886 "Generate instruction documentation"),
887 clEnumValN(GenCallingConv, "gen-callingconv",
888 @@ -160,12 +164,14 @@ bool LLVMTableGenMain(raw_ostream &OS, RecordKeeper &Records) {
889 case GenInstrInfo:
890 EmitInstrInfo(Records, OS);
891 break;
892 +#ifdef CAPSTONE
893 case GenMappingInsn:
894 EmitMappingInsn(Records, OS);
895 break;
896 case GenInsnNameMaps:
897 EmitInsnNameMaps(Records, OS);
898 break;
899 +#endif
900 case GenInstrDocs:
901 EmitInstrDocs(Records, OS);
902 break;
903 --
904 2.20.1
905