Jim Laskey | 4bb9cbb | 2005-10-21 19:00:04 +0000 | [diff] [blame] | 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 | |
Jim Laskey | 7dc0204 | 2005-10-22 07:59:56 +0000 | [diff] [blame] | 23 | // |
| 24 | // Convenience types. |
| 25 | // |
Jim Laskey | 4bb9cbb | 2005-10-21 19:00:04 +0000 | [diff] [blame] | 26 | typedef std::vector<Record*> RecordList; |
| 27 | typedef std::vector<Record*>::iterator RecordListIter; |
| 28 | |
Jim Laskey | 7dc0204 | 2005-10-22 07:59:56 +0000 | [diff] [blame] | 29 | // |
| 30 | // Record sort by name function. |
| 31 | // |
| 32 | struct LessRecord { |
| 33 | bool operator()(const Record *Rec1, const Record *Rec2) const { |
| 34 | return Rec1->getName() < Rec2->getName(); |
| 35 | } |
| 36 | }; |
Jim Laskey | 4bb9cbb | 2005-10-21 19:00:04 +0000 | [diff] [blame] | 37 | |
Jim Laskey | 7dc0204 | 2005-10-22 07:59:56 +0000 | [diff] [blame] | 38 | // |
| 39 | // Record sort by field "Name" function. |
| 40 | // |
| 41 | struct LessRecordFieldName { |
| 42 | bool operator()(const Record *Rec1, const Record *Rec2) const { |
| 43 | return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name"); |
| 44 | } |
| 45 | }; |
| 46 | |
Jim Laskey | b3b1d5f | 2005-10-25 15:16:36 +0000 | [diff] [blame^] | 47 | // |
| 48 | // FeatureEnumeration - Emit an enumeration of all the subtarget features. |
| 49 | // |
| 50 | void SubtargetEmitter::FeatureEnumeration(std::ostream &OS) { |
| 51 | RecordList Features = Records.getAllDerivedDefinitions("SubtargetFeature"); |
| 52 | sort(Features.begin(), Features.end(), LessRecord()); |
| 53 | |
| 54 | int i = 0; |
| 55 | |
| 56 | OS << "enum {\n"; |
| 57 | |
| 58 | for (RecordListIter RI = Features.begin(), E = Features.end(); RI != E;){ |
| 59 | Record *R = *RI++; |
| 60 | std::string Instance = R->getName(); |
| 61 | OS << " " |
| 62 | << Instance |
| 63 | << " = " |
| 64 | << " 1 << " << i++ |
| 65 | << ((RI != E) ? ",\n" : "\n"); |
| 66 | } |
| 67 | |
| 68 | OS << "};\n"; |
| 69 | } |
| 70 | |
| 71 | // |
| 72 | // FeatureKeyValues - Emit data of all the subtarget features. Used by command |
| 73 | // line. |
| 74 | // |
| 75 | void SubtargetEmitter::FeatureKeyValues(std::ostream &OS) { |
| 76 | RecordList Features = Records.getAllDerivedDefinitions("SubtargetFeature"); |
| 77 | sort(Features.begin(), Features.end(), LessRecord()); |
| 78 | |
| 79 | OS << "\n" |
| 80 | << "// Sorted (by key) array of values for CPU features.\n" |
| 81 | << "static llvm::SubtargetFeatureKV FeatureKV[] = {\n"; |
| 82 | for (RecordListIter RI = Features.begin(), E = Features.end(); RI != E;) { |
| 83 | Record *R = *RI++; |
| 84 | std::string Instance = R->getName(); |
| 85 | std::string Name = R->getValueAsString("Name"); |
| 86 | std::string Desc = R->getValueAsString("Desc"); |
| 87 | OS << " { " |
| 88 | << "\"" << Name << "\", " |
| 89 | << "\"" << Desc << "\", " |
| 90 | << Instance |
| 91 | << ((RI != E) ? " },\n" : " }\n"); |
| 92 | } |
| 93 | OS << "};\n"; |
| 94 | |
| 95 | OS<<"\nenum {\n"; |
| 96 | OS<<" FeatureKVSize = sizeof(FeatureKV)/sizeof(llvm::SubtargetFeatureKV)\n"; |
| 97 | OS<<"};\n"; |
| 98 | } |
| 99 | |
| 100 | // |
| 101 | // CPUKeyValues - Emit data of all the subtarget processors. Used by command |
| 102 | // line. |
| 103 | // |
| 104 | void SubtargetEmitter::CPUKeyValues(std::ostream &OS) { |
| 105 | RecordList Processors = Records.getAllDerivedDefinitions("Processor"); |
| 106 | sort(Processors.begin(), Processors.end(), LessRecordFieldName()); |
| 107 | |
| 108 | OS << "\n" |
| 109 | << "// Sorted (by key) array of values for CPU subtype.\n" |
| 110 | << "static const llvm::SubtargetFeatureKV SubTypeKV[] = {\n"; |
| 111 | for (RecordListIter RI = Processors.begin(), E = Processors.end(); |
| 112 | RI != E;) { |
| 113 | Record *R = *RI++; |
| 114 | std::string Name = R->getValueAsString("Name"); |
| 115 | Record *ProcItin = R->getValueAsDef("ProcItin"); |
| 116 | ListInit *Features = R->getValueAsListInit("Features"); |
| 117 | unsigned N = Features->getSize(); |
| 118 | OS << " { " |
| 119 | << "\"" << Name << "\", " |
| 120 | << "\"Select the " << Name << " processor\", "; |
| 121 | |
| 122 | |
| 123 | if (N == 0) { |
| 124 | OS << "0"; |
| 125 | } else { |
| 126 | for (unsigned i = 0; i < N; ) { |
| 127 | if (DefInit *DI = dynamic_cast<DefInit*>(Features->getElement(i++))) { |
| 128 | Record *Feature = DI->getDef(); |
| 129 | std::string Name = Feature->getName(); |
| 130 | OS << Name; |
| 131 | if (i != N) OS << " | "; |
| 132 | } else { |
| 133 | throw "Feature: " + Name + |
| 134 | " expected feature in processor feature list!"; |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | OS << ((RI != E) ? " },\n" : " }\n"); |
| 140 | } |
| 141 | OS << "};\n"; |
| 142 | |
| 143 | OS<<"\nenum {\n"; |
| 144 | OS<<" SubTypeKVSize = sizeof(SubTypeKV)/sizeof(llvm::SubtargetFeatureKV)\n"; |
| 145 | OS<<"};\n"; |
| 146 | } |
Jim Laskey | 7dc0204 | 2005-10-22 07:59:56 +0000 | [diff] [blame] | 147 | |
| 148 | // |
Jim Laskey | 4bb9cbb | 2005-10-21 19:00:04 +0000 | [diff] [blame] | 149 | // SubtargetEmitter::run - Main subtarget enumeration emitter. |
| 150 | // |
| 151 | void SubtargetEmitter::run(std::ostream &OS) { |
| 152 | EmitSourceFileHeader("Subtarget Enumeration Source Fragment", OS); |
Jim Laskey | 4bb9cbb | 2005-10-21 19:00:04 +0000 | [diff] [blame] | 153 | |
Chris Lattner | d4d0797 | 2005-10-23 22:33:08 +0000 | [diff] [blame] | 154 | OS << "#include \"llvm/Target/SubtargetFeature.h\"\n\n"; |
Jim Laskey | 4bb9cbb | 2005-10-21 19:00:04 +0000 | [diff] [blame] | 155 | |
Jim Laskey | b3b1d5f | 2005-10-25 15:16:36 +0000 | [diff] [blame^] | 156 | FeatureEnumeration(OS); |
| 157 | FeatureKeyValues(OS); |
| 158 | CPUKeyValues(OS); |
Jim Laskey | 4bb9cbb | 2005-10-21 19:00:04 +0000 | [diff] [blame] | 159 | } |