New TableGen backends for subtarget information. Only command line stuff
[oota-llvm.git] / utils / TableGen / SubtargetEmitter.cpp
1 //===- SubtargetEmitter.cpp - Generate subtarget enumerations -------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by James M. Laskey and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This tablegen backend emits subtarget enumerations.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "SubtargetEmitter.h"
15 #include "CodeGenTarget.h"
16 #include "Record.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/Support/Debug.h"
19 #include <algorithm>
20 #include <set>
21 using namespace llvm;
22
23 // Convenience types
24 typedef std::vector<Record*> RecordList;
25 typedef std::vector<Record*>::iterator RecordListIter;
26
27
28 // SubtargetEmitter::run - Main subtarget enumeration emitter.
29 //
30 void SubtargetEmitter::run(std::ostream &OS) {
31   EmitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
32   RecordList Features = Records.getAllDerivedDefinitions("SubtargetFeature");
33   RecordList Processors = Records.getAllDerivedDefinitions("Processor");
34
35   OS << "namespace llvm {\n\n";
36   
37   { // Feature enumeration
38     int i = 0;
39     
40     OS << "enum {\n";
41     
42     for (RecordListIter RI = Features.begin(), E = Features.end(); RI != E;){
43       Record *R = *RI++;
44       std::string Instance = R->getName();
45       OS << "  "
46          << Instance
47          << " = "
48          << " 1 << " << i++
49          << ((RI != E) ? ",\n" : "\n");
50     }
51     
52     OS << "};\n";
53   }
54   
55   { // Feature key values
56     OS << "\n\n"
57        << "/// Sorted (by key) array of values for CPU features.\n"
58        << "static SubtargetFeatureKV FeatureKV[] = {\n";
59     for (RecordListIter RI = Features.begin(), E = Features.end(); RI != E;) {
60       Record *R = *RI++;
61       std::string Instance = R->getName();
62       std::string Name = R->getValueAsString("Name");
63       std::string Desc = R->getValueAsString("Desc");
64       OS << "  { "
65          << "\"" << Name << "\", "
66          << "\"" << Desc << "\", "
67          << Instance
68          << ((RI != E) ? " },\n" : " }\n");
69     }
70     OS << "};\n";
71   }
72   
73   { // Feature key values
74     OS << "\n\n"
75        << "/// Sorted (by key) array of values for CPU subtype.\n"
76        << "static const SubtargetFeatureKV SubTypeKV[] = {\n";
77     for (RecordListIter RI = Processors.begin(), E = Processors.end();
78          RI != E;) {
79       Record *R = *RI++;
80       std::string Name = R->getValueAsString("Name");
81       Record *ProcItin = R->getValueAsDef("ProcItin");
82       ListInit *Features = R->getValueAsListInit("Features");
83       unsigned N = Features->getSize();
84       OS << "  { "
85          << "\"" << Name << "\", "
86          << "\"Select the " << Name << " processor\", ";
87          
88       
89       if (N == 0) {
90         OS << "0";
91       } else {
92         for (unsigned i = 0; i < N; ) {
93           if (DefInit *DI = dynamic_cast<DefInit*>(Features->getElement(i++))) {
94             Record *Feature = DI->getDef();
95             std::string Name = Feature->getName();
96             OS << Name;
97             if (i != N) OS << " | ";
98           } else {
99             throw "Feature: " + Name +
100                   " expected feature in processor feature list!";
101           }
102         }
103       }
104       
105       OS << ((RI != E) ? " },\n" : " }\n");
106     }
107     OS << "};\n";
108   }
109
110   OS << "\n} // End llvm namespace \n";
111 }