Refactoring: split CollectProperties into two separate function objects.
[oota-llvm.git] / utils / TableGen / InstrEnumEmitter.cpp
1 //===- InstrEnumEmitter.cpp - Generate Instruction Set Enums --------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This tablegen backend is responsible for emitting enums for each machine
11 // instruction.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "InstrEnumEmitter.h"
16 #include "CodeGenTarget.h"
17 #include "Record.h"
18 using namespace llvm;
19
20 // runEnums - Print out enum values for all of the instructions.
21 void InstrEnumEmitter::run(std::ostream &OS) {
22   EmitSourceFileHeader("Target Instruction Enum Values", OS);
23   OS << "namespace llvm {\n\n";
24
25   CodeGenTarget Target;
26
27   // We must emit the PHI opcode first...
28   std::string Namespace;
29   for (CodeGenTarget::inst_iterator II = Target.inst_begin(), 
30        E = Target.inst_end(); II != E; ++II) {
31     if (II->second.Namespace != "TargetInstrInfo") {
32       Namespace = II->second.Namespace;
33       break;
34     }
35   }
36   
37   if (Namespace.empty()) {
38     fprintf(stderr, "No instructions defined!\n");
39     exit(1);
40   }
41
42   std::vector<const CodeGenInstruction*> NumberedInstructions;
43   Target.getInstructionsByEnumValue(NumberedInstructions);
44
45   OS << "namespace " << Namespace << " {\n";
46   OS << "  enum {\n";
47   for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
48     OS << "    " << NumberedInstructions[i]->TheDef->getName()
49        << "\t= " << i << ",\n";
50   }
51   OS << "    INSTRUCTION_LIST_END = " << NumberedInstructions.size() << "\n";
52   OS << "  };\n}\n";
53   OS << "} // End llvm namespace \n";
54 }