blob: f382d34f3486866d7fdfde2606578cc6f381d357 [file] [log] [blame]
Chris Lattner7b117122008-01-06 00:49:05 +00001//===- 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"
Duncan Sands4520dd22008-10-08 07:23:46 +000018#include <cstdio>
Chris Lattner7b117122008-01-06 00:49:05 +000019using namespace llvm;
20
21// runEnums - Print out enum values for all of the instructions.
Daniel Dunbar1a551802009-07-03 00:10:29 +000022void InstrEnumEmitter::run(raw_ostream &OS) {
Chris Lattner7b117122008-01-06 00:49:05 +000023 EmitSourceFileHeader("Target Instruction Enum Values", OS);
24 OS << "namespace llvm {\n\n";
25
26 CodeGenTarget Target;
27
28 // We must emit the PHI opcode first...
29 std::string Namespace;
30 for (CodeGenTarget::inst_iterator II = Target.inst_begin(),
31 E = Target.inst_end(); II != E; ++II) {
32 if (II->second.Namespace != "TargetInstrInfo") {
33 Namespace = II->second.Namespace;
34 break;
35 }
36 }
37
38 if (Namespace.empty()) {
39 fprintf(stderr, "No instructions defined!\n");
40 exit(1);
41 }
42
43 std::vector<const CodeGenInstruction*> NumberedInstructions;
44 Target.getInstructionsByEnumValue(NumberedInstructions);
45
46 OS << "namespace " << Namespace << " {\n";
47 OS << " enum {\n";
48 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
49 OS << " " << NumberedInstructions[i]->TheDef->getName()
50 << "\t= " << i << ",\n";
51 }
52 OS << " INSTRUCTION_LIST_END = " << NumberedInstructions.size() << "\n";
53 OS << " };\n}\n";
54 OS << "} // End llvm namespace \n";
55}