Skip to content

Commit 90224e3

Browse files
committed
Enable EmitMapTable to print C tables.
1 parent 47557b8 commit 90224e3

File tree

3 files changed

+30
-20
lines changed

3 files changed

+30
-20
lines changed

llvm/utils/TableGen/CodeGenMapTable.cpp

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,12 @@ class MapTableEmitter {
188188
std::vector<Record*> KeyInstrVec;
189189
DenseMap<Record*, std::vector<Record*> > MapTable;
190190

191+
// Simple flag for Capstone C files.
192+
bool EmitC;
193+
191194
public:
192-
MapTableEmitter(CodeGenTarget &Target, RecordKeeper &Records, Record *IMRec):
193-
Target(Target), InstrMapDesc(IMRec) {
195+
MapTableEmitter(CodeGenTarget &Target, RecordKeeper &Records, Record *IMRec, bool EmitC = false):
196+
Target(Target), InstrMapDesc(IMRec), EmitC(EmitC) {
194197
const std::string &FilterClass = InstrMapDesc.getFilterClass();
195198
InstrDefs = Records.getAllDerivedDefinitions(FilterClass);
196199
}
@@ -210,8 +213,8 @@ class MapTableEmitter {
210213
void buildMapTable();
211214

212215
void emitBinSearch(raw_ostream &OS, unsigned TableSize);
213-
void emitTablesWithFunc(raw_ostream &OS);
214-
unsigned emitBinSearchTable(raw_ostream &OS);
216+
void emitTablesWithFunc(raw_ostream &OS, bool EmitC);
217+
unsigned emitBinSearchTable(raw_ostream &OS, bool EmitC);
215218

216219
// Lookup functions to query binary search tables.
217220
void emitMapFuncBody(raw_ostream &OS, unsigned TableSize);
@@ -358,7 +361,7 @@ Record *MapTableEmitter::getInstrForColumn(Record *KeyInstr,
358361
// Binary search is used for locating instructions in the table.
359362
//===----------------------------------------------------------------------===//
360363

361-
unsigned MapTableEmitter::emitBinSearchTable(raw_ostream &OS) {
364+
unsigned MapTableEmitter::emitBinSearchTable(raw_ostream &OS, bool EmitC) {
362365

363366
ArrayRef<const CodeGenInstruction*> NumberedInstructions =
364367
Target.getInstructionsByEnumValue();
@@ -367,6 +370,7 @@ unsigned MapTableEmitter::emitBinSearchTable(raw_ostream &OS) {
367370
unsigned NumCol = ValueCols.size();
368371
unsigned TotalNumInstr = NumberedInstructions.size();
369372
unsigned TableSize = 0;
373+
std::string NSSep = (EmitC ? "_" : "::");
370374

371375
OS << "static const uint16_t "<<InstrMapDesc.getName();
372376
// Number of columns in the table are NumCol+1 because key instructions are
@@ -383,21 +387,21 @@ unsigned MapTableEmitter::emitBinSearchTable(raw_ostream &OS) {
383387
RelExists = 1;
384388
OutStr += ", ";
385389
OutStr += Namespace;
386-
OutStr += "::";
390+
OutStr += NSSep;
387391
OutStr += ColInstrs[j]->getName();
388392
} else { OutStr += ", (uint16_t)-1U";}
389393
}
390394

391395
if (RelExists) {
392-
OS << " { " << Namespace << "::" << CurInstr->getName();
396+
OS << " { " << Namespace << NSSep << CurInstr->getName();
393397
OS << OutStr <<" },\n";
394398
TableSize++;
395399
}
396400
}
397401
}
398402
if (!TableSize) {
399-
OS << " { " << Namespace << "::" << "INSTRUCTION_LIST_END, ";
400-
OS << Namespace << "::" << "INSTRUCTION_LIST_END }";
403+
OS << " { " << Namespace << NSSep << "INSTRUCTION_LIST_END, ";
404+
OS << Namespace << NSSep << "INSTRUCTION_LIST_END }";
401405
}
402406
OS << "}; // End of " << InstrMapDesc.getName() << "Table\n\n";
403407
return TableSize;
@@ -469,7 +473,7 @@ void MapTableEmitter::emitMapFuncBody(raw_ostream &OS,
469473
// Emit relation tables and the functions to query them.
470474
//===----------------------------------------------------------------------===//
471475

472-
void MapTableEmitter::emitTablesWithFunc(raw_ostream &OS) {
476+
void MapTableEmitter::emitTablesWithFunc(raw_ostream &OS, bool EmitC) {
473477

474478
// Emit function name and the input parameters : mostly opcode value of the
475479
// current instruction. However, if a table has multiple columns (more than 2
@@ -478,7 +482,8 @@ void MapTableEmitter::emitTablesWithFunc(raw_ostream &OS) {
478482

479483
ListInit *ColFields = InstrMapDesc.getColFields();
480484
const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols();
481-
OS << "// "<< InstrMapDesc.getName() << "\nLLVM_READONLY\n";
485+
OS << "// "<< InstrMapDesc.getName() << "\n";
486+
OS << (EmitC ? "" : "LLVM_READONLY\n");
482487
OS << "int "<< InstrMapDesc.getName() << "(uint16_t Opcode";
483488
if (ValueCols.size() > 1) {
484489
for (Init *CF : ColFields->getValues()) {
@@ -489,7 +494,7 @@ void MapTableEmitter::emitTablesWithFunc(raw_ostream &OS) {
489494
OS << ") {\n";
490495

491496
// Emit map table.
492-
unsigned TableSize = emitBinSearchTable(OS);
497+
unsigned TableSize = emitBinSearchTable(OS, EmitC);
493498

494499
// Emit rest of the function body.
495500
emitMapFuncBody(OS, TableSize);
@@ -563,7 +568,7 @@ namespace llvm {
563568
// between instructions. These relations are emitted as a tables along with the
564569
// functions to query them.
565570
//===----------------------------------------------------------------------===//
566-
void EmitMapTable(RecordKeeper &Records, raw_ostream &OS) {
571+
void EmitMapTable(RecordKeeper &Records, raw_ostream &OS, bool EmitC = false) {
567572
CodeGenTarget Target(Records);
568573
StringRef NameSpace = Target.getInstNamespace();
569574
std::vector<Record*> InstrMapVec;
@@ -574,8 +579,10 @@ void EmitMapTable(RecordKeeper &Records, raw_ostream &OS) {
574579

575580
OS << "#ifdef GET_INSTRMAP_INFO\n";
576581
OS << "#undef GET_INSTRMAP_INFO\n";
577-
OS << "namespace llvm {\n\n";
578-
OS << "namespace " << NameSpace << " {\n\n";
582+
if (!EmitC) {
583+
OS << "namespace llvm {\n\n";
584+
OS << "namespace " << NameSpace << " {\n\n";
585+
}
579586

580587
// Emit coulumn field names and their values as enums.
581588
emitEnums(OS, Records);
@@ -596,10 +603,12 @@ void EmitMapTable(RecordKeeper &Records, raw_ostream &OS) {
596603
IMap.buildMapTable();
597604

598605
// Emit map tables and the functions to query them.
599-
IMap.emitTablesWithFunc(OS);
606+
IMap.emitTablesWithFunc(OS, EmitC);
607+
}
608+
if (!EmitC) {
609+
OS << "} // end namespace " << NameSpace << "\n";
610+
OS << "} // end namespace llvm\n";
600611
}
601-
OS << "} // end namespace " << NameSpace << "\n";
602-
OS << "} // end namespace llvm\n";
603612
OS << "#endif // GET_INSTRMAP_INFO\n\n";
604613
}
605614

llvm/utils/TableGen/InstrInfoEmitter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "InstrInfoEmitterTypes.h"
1515
#include "Printer.h"
16+
#include "PrinterTypes.h"
1617

1718
using namespace llvm;
1819

@@ -792,7 +793,7 @@ void EmitInstrInfo(RecordKeeper &RK, raw_ostream &OS) {
792793
RK.startTimer("Analyze DAG patterns");
793794
InstrInfoEmitter(RK, *PI).run();
794795
RK.startTimer("Emit map table");
795-
EmitMapTable(RK, OS);
796+
EmitMapTable(RK, OS, PL == PRINTER_LANG_CAPSTONE_C);
796797
delete PI;
797798
}
798799

llvm/utils/TableGen/TableGenBackends.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ namespace llvm {
6464
class raw_ostream;
6565
class RecordKeeper;
6666

67-
void EmitMapTable(RecordKeeper &RK, raw_ostream &OS);
67+
void EmitMapTable(RecordKeeper &RK, raw_ostream &OS, bool EmitC);
6868

6969
// Defined in DecoderEmitter.cpp
7070
void EmitDecoder(RecordKeeper &RK, raw_ostream &OS,

0 commit comments

Comments
 (0)