@@ -188,9 +188,12 @@ class MapTableEmitter {
188
188
std::vector<Record*> KeyInstrVec;
189
189
DenseMap<Record*, std::vector<Record*> > MapTable;
190
190
191
+ // Simple flag for Capstone C files.
192
+ bool EmitC;
193
+
191
194
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) {
194
197
const std::string &FilterClass = InstrMapDesc.getFilterClass ();
195
198
InstrDefs = Records.getAllDerivedDefinitions (FilterClass);
196
199
}
@@ -210,8 +213,8 @@ class MapTableEmitter {
210
213
void buildMapTable ();
211
214
212
215
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 );
215
218
216
219
// Lookup functions to query binary search tables.
217
220
void emitMapFuncBody (raw_ostream &OS, unsigned TableSize);
@@ -358,7 +361,7 @@ Record *MapTableEmitter::getInstrForColumn(Record *KeyInstr,
358
361
// Binary search is used for locating instructions in the table.
359
362
// ===----------------------------------------------------------------------===//
360
363
361
- unsigned MapTableEmitter::emitBinSearchTable (raw_ostream &OS) {
364
+ unsigned MapTableEmitter::emitBinSearchTable (raw_ostream &OS, bool EmitC ) {
362
365
363
366
ArrayRef<const CodeGenInstruction*> NumberedInstructions =
364
367
Target.getInstructionsByEnumValue ();
@@ -367,6 +370,7 @@ unsigned MapTableEmitter::emitBinSearchTable(raw_ostream &OS) {
367
370
unsigned NumCol = ValueCols.size ();
368
371
unsigned TotalNumInstr = NumberedInstructions.size ();
369
372
unsigned TableSize = 0 ;
373
+ std::string NSSep = (EmitC ? " _" : " ::" );
370
374
371
375
OS << " static const uint16_t " <<InstrMapDesc.getName ();
372
376
// Number of columns in the table are NumCol+1 because key instructions are
@@ -383,21 +387,21 @@ unsigned MapTableEmitter::emitBinSearchTable(raw_ostream &OS) {
383
387
RelExists = 1 ;
384
388
OutStr += " , " ;
385
389
OutStr += Namespace;
386
- OutStr += " :: " ;
390
+ OutStr += NSSep ;
387
391
OutStr += ColInstrs[j]->getName ();
388
392
} else { OutStr += " , (uint16_t)-1U" ;}
389
393
}
390
394
391
395
if (RelExists) {
392
- OS << " { " << Namespace << " :: " << CurInstr->getName ();
396
+ OS << " { " << Namespace << NSSep << CurInstr->getName ();
393
397
OS << OutStr <<" },\n " ;
394
398
TableSize++;
395
399
}
396
400
}
397
401
}
398
402
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 }" ;
401
405
}
402
406
OS << " }; // End of " << InstrMapDesc.getName () << " Table\n\n " ;
403
407
return TableSize;
@@ -469,7 +473,7 @@ void MapTableEmitter::emitMapFuncBody(raw_ostream &OS,
469
473
// Emit relation tables and the functions to query them.
470
474
// ===----------------------------------------------------------------------===//
471
475
472
- void MapTableEmitter::emitTablesWithFunc (raw_ostream &OS) {
476
+ void MapTableEmitter::emitTablesWithFunc (raw_ostream &OS, bool EmitC ) {
473
477
474
478
// Emit function name and the input parameters : mostly opcode value of the
475
479
// current instruction. However, if a table has multiple columns (more than 2
@@ -478,7 +482,8 @@ void MapTableEmitter::emitTablesWithFunc(raw_ostream &OS) {
478
482
479
483
ListInit *ColFields = InstrMapDesc.getColFields ();
480
484
const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols ();
481
- OS << " // " << InstrMapDesc.getName () << " \n LLVM_READONLY\n " ;
485
+ OS << " // " << InstrMapDesc.getName () << " \n " ;
486
+ OS << (EmitC ? " " : " LLVM_READONLY\n " );
482
487
OS << " int " << InstrMapDesc.getName () << " (uint16_t Opcode" ;
483
488
if (ValueCols.size () > 1 ) {
484
489
for (Init *CF : ColFields->getValues ()) {
@@ -489,7 +494,7 @@ void MapTableEmitter::emitTablesWithFunc(raw_ostream &OS) {
489
494
OS << " ) {\n " ;
490
495
491
496
// Emit map table.
492
- unsigned TableSize = emitBinSearchTable (OS);
497
+ unsigned TableSize = emitBinSearchTable (OS, EmitC );
493
498
494
499
// Emit rest of the function body.
495
500
emitMapFuncBody (OS, TableSize);
@@ -563,7 +568,7 @@ namespace llvm {
563
568
// between instructions. These relations are emitted as a tables along with the
564
569
// functions to query them.
565
570
// ===----------------------------------------------------------------------===//
566
- void EmitMapTable (RecordKeeper &Records, raw_ostream &OS) {
571
+ void EmitMapTable (RecordKeeper &Records, raw_ostream &OS, bool EmitC = false ) {
567
572
CodeGenTarget Target (Records);
568
573
StringRef NameSpace = Target.getInstNamespace ();
569
574
std::vector<Record*> InstrMapVec;
@@ -574,8 +579,10 @@ void EmitMapTable(RecordKeeper &Records, raw_ostream &OS) {
574
579
575
580
OS << " #ifdef GET_INSTRMAP_INFO\n " ;
576
581
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
+ }
579
586
580
587
// Emit coulumn field names and their values as enums.
581
588
emitEnums (OS, Records);
@@ -596,10 +603,12 @@ void EmitMapTable(RecordKeeper &Records, raw_ostream &OS) {
596
603
IMap.buildMapTable ();
597
604
598
605
// 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 " ;
600
611
}
601
- OS << " } // end namespace " << NameSpace << " \n " ;
602
- OS << " } // end namespace llvm\n " ;
603
612
OS << " #endif // GET_INSTRMAP_INFO\n\n " ;
604
613
}
605
614
0 commit comments