1 //===- CodeGenTarget.cpp - CodeGen Target Class Wrapper ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This class wrap target description classes used by the various code
11 // generation TableGen backends. This makes it easier to access the data and
12 // provides a single place that needs to check it for validity. All of these
13 // classes throw exceptions on error conditions.
15 //===----------------------------------------------------------------------===//
17 #include "CodeGenTarget.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/Support/CommandLine.h"
25 static cl::opt<unsigned>
26 AsmWriterNum("asmwriternum", cl::init(0),
27 cl::desc("Make -gen-asm-writer emit assembly writer #N"));
29 /// getValueType - Return the MCV::ValueType that the specified TableGen record
31 MVT::ValueType llvm::getValueType(Record *Rec) {
32 return (MVT::ValueType)Rec->getValueAsInt("Value");
35 std::string llvm::getName(MVT::ValueType T) {
37 case MVT::Other: return "UNKNOWN";
38 case MVT::i1: return "i1";
39 case MVT::i8: return "i8";
40 case MVT::i16: return "i16";
41 case MVT::i32: return "i32";
42 case MVT::i64: return "i64";
43 case MVT::i128: return "i128";
44 case MVT::f32: return "f32";
45 case MVT::f64: return "f64";
46 case MVT::f80: return "f80";
47 case MVT::f128: return "f128";
48 case MVT::isVoid:return "void";
49 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
53 std::string llvm::getEnumName(MVT::ValueType T) {
55 case MVT::Other: return "Other";
56 case MVT::i1: return "i1";
57 case MVT::i8: return "i8";
58 case MVT::i16: return "i16";
59 case MVT::i32: return "i32";
60 case MVT::i64: return "i64";
61 case MVT::i128: return "i128";
62 case MVT::f32: return "f32";
63 case MVT::f64: return "f64";
64 case MVT::f80: return "f80";
65 case MVT::f128: return "f128";
66 case MVT::isVoid:return "isVoid";
67 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
72 std::ostream &llvm::operator<<(std::ostream &OS, MVT::ValueType T) {
73 return OS << getName(T);
77 /// getTarget - Return the current instance of the Target class.
79 CodeGenTarget::CodeGenTarget() : PointerType(MVT::Other) {
80 std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target");
81 if (Targets.size() == 0)
82 throw std::string("ERROR: No 'Target' subclasses defined!");
83 if (Targets.size() != 1)
84 throw std::string("ERROR: Multiple subclasses of Target defined!");
85 TargetRec = Targets[0];
87 // Read in all of the CalleeSavedRegisters.
88 CalleeSavedRegisters =TargetRec->getValueAsListOfDefs("CalleeSavedRegisters");
89 PointerType = getValueType(TargetRec->getValueAsDef("PointerType"));
93 const std::string &CodeGenTarget::getName() const {
94 return TargetRec->getName();
97 Record *CodeGenTarget::getInstructionSet() const {
98 return TargetRec->getValueAsDef("InstructionSet");
101 /// getAsmWriter - Return the AssemblyWriter definition for this target.
103 Record *CodeGenTarget::getAsmWriter() const {
104 std::vector<Record*> LI = TargetRec->getValueAsListOfDefs("AssemblyWriters");
105 if (AsmWriterNum >= LI.size())
106 throw "Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!";
107 return LI[AsmWriterNum];
110 void CodeGenTarget::ReadRegisters() const {
111 std::vector<Record*> Regs = Records.getAllDerivedDefinitions("Register");
113 throw std::string("No 'Register' subclasses defined!");
115 Registers.reserve(Regs.size());
116 Registers.assign(Regs.begin(), Regs.end());
119 CodeGenRegister::CodeGenRegister(Record *R) : TheDef(R) {
120 DeclaredSpillSize = R->getValueAsInt("SpillSize");
121 DeclaredSpillAlignment = R->getValueAsInt("SpillAlignment");
124 const std::string &CodeGenRegister::getName() const {
125 return TheDef->getName();
128 void CodeGenTarget::ReadRegisterClasses() const {
129 std::vector<Record*> RegClasses =
130 Records.getAllDerivedDefinitions("RegisterClass");
131 if (RegClasses.empty())
132 throw std::string("No 'RegisterClass' subclasses defined!");
134 RegisterClasses.reserve(RegClasses.size());
135 RegisterClasses.assign(RegClasses.begin(), RegClasses.end());
138 CodeGenRegisterClass::CodeGenRegisterClass(Record *R) : TheDef(R) {
139 // Rename anonymous register classes.
140 if (R->getName().size() > 9 && R->getName()[9] == '.') {
141 static unsigned AnonCounter = 0;
142 R->setName("AnonRegClass_"+utostr(AnonCounter++));
145 Namespace = R->getValueAsString("Namespace");
146 SpillSize = R->getValueAsInt("Size");
147 SpillAlignment = R->getValueAsInt("Alignment");
148 VT = getValueType(R->getValueAsDef("RegType"));
150 MethodBodies = R->getValueAsCode("MethodBodies");
151 MethodProtos = R->getValueAsCode("MethodProtos");
153 std::vector<Record*> RegList = R->getValueAsListOfDefs("MemberList");
154 for (unsigned i = 0, e = RegList.size(); i != e; ++i) {
155 Record *Reg = RegList[i];
156 if (!Reg->isSubClassOf("Register"))
157 throw "Register Class member '" + Reg->getName() +
158 "' does not derive from the Register class!";
159 Elements.push_back(Reg);
163 const std::string &CodeGenRegisterClass::getName() const {
164 return TheDef->getName();
167 void CodeGenTarget::ReadLegalValueTypes() const {
168 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
169 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
170 LegalValueTypes.push_back(RCs[i].VT);
172 // Remove duplicates.
173 std::sort(LegalValueTypes.begin(), LegalValueTypes.end());
174 LegalValueTypes.erase(std::unique(LegalValueTypes.begin(),
175 LegalValueTypes.end()),
176 LegalValueTypes.end());
180 void CodeGenTarget::ReadInstructions() const {
181 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
184 throw std::string("No 'Instruction' subclasses defined!");
186 std::string InstFormatName =
187 getAsmWriter()->getValueAsString("InstFormatName");
189 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
190 std::string AsmStr = Insts[i]->getValueAsString(InstFormatName);
191 Instructions.insert(std::make_pair(Insts[i]->getName(),
192 CodeGenInstruction(Insts[i], AsmStr)));
196 /// getPHIInstruction - Return the designated PHI instruction.
198 const CodeGenInstruction &CodeGenTarget::getPHIInstruction() const {
199 Record *PHI = getInstructionSet()->getValueAsDef("PHIInst");
200 std::map<std::string, CodeGenInstruction>::const_iterator I =
201 getInstructions().find(PHI->getName());
202 if (I == Instructions.end())
203 throw "Could not find PHI instruction named '" + PHI->getName() + "'!";
207 /// getInstructionsByEnumValue - Return all of the instructions defined by the
208 /// target, ordered by their enum value.
210 getInstructionsByEnumValue(std::vector<const CodeGenInstruction*>
211 &NumberedInstructions) {
213 // Print out the rest of the instructions now.
215 const CodeGenInstruction *PHI = &getPHIInstruction();
216 NumberedInstructions.push_back(PHI);
217 for (inst_iterator II = inst_begin(), E = inst_end(); II != E; ++II)
218 if (&II->second != PHI)
219 NumberedInstructions.push_back(&II->second);
223 /// isLittleEndianEncoding - Return whether this target encodes its instruction
224 /// in little-endian format, i.e. bits laid out in the order [0..n]
226 bool CodeGenTarget::isLittleEndianEncoding() const {
227 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
230 CodeGenInstruction::CodeGenInstruction(Record *R, const std::string &AsmStr)
231 : TheDef(R), AsmString(AsmStr) {
232 Name = R->getValueAsString("Name");
233 Namespace = R->getValueAsString("Namespace");
235 isReturn = R->getValueAsBit("isReturn");
236 isBranch = R->getValueAsBit("isBranch");
237 isBarrier = R->getValueAsBit("isBarrier");
238 isCall = R->getValueAsBit("isCall");
239 isLoad = R->getValueAsBit("isLoad");
240 isStore = R->getValueAsBit("isStore");
241 isTwoAddress = R->getValueAsBit("isTwoAddress");
242 isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
243 isCommutable = R->getValueAsBit("isCommutable");
244 isTerminator = R->getValueAsBit("isTerminator");
245 hasDelaySlot = R->getValueAsBit("hasDelaySlot");
246 usesCustomDAGSchedInserter = R->getValueAsBit("usesCustomDAGSchedInserter");
247 hasVariableNumberOfOperands = false;
251 DI = R->getValueAsDag("OperandList");
253 // Error getting operand list, just ignore it (sparcv9).
259 unsigned MIOperandNo = 0;
260 std::set<std::string> OperandNames;
261 for (unsigned i = 0, e = DI->getNumArgs(); i != e; ++i) {
262 DefInit *Arg = dynamic_cast<DefInit*>(DI->getArg(i));
264 throw "Illegal operand for the '" + R->getName() + "' instruction!";
266 Record *Rec = Arg->getDef();
268 std::string PrintMethod = "printOperand";
271 if (Rec->isSubClassOf("RegisterClass")) {
272 Ty = getValueType(Rec->getValueAsDef("RegType"));
273 } else if (Rec->isSubClassOf("Operand")) {
274 Ty = getValueType(Rec->getValueAsDef("Type"));
275 PrintMethod = Rec->getValueAsString("PrintMethod");
276 NumOps = Rec->getValueAsInt("NumMIOperands");
277 MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
278 } else if (Rec->getName() == "variable_ops") {
279 hasVariableNumberOfOperands = true;
282 throw "Unknown operand class '" + Rec->getName() +
283 "' in instruction '" + R->getName() + "' instruction!";
285 // Check that the operand has a name and that it's unique.
286 if (DI->getArgName(i).empty())
287 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
289 if (!OperandNames.insert(DI->getArgName(i)).second)
290 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
291 " has the same name as a previous operand!";
293 OperandList.push_back(OperandInfo(Rec, Ty, DI->getArgName(i),
294 PrintMethod, MIOperandNo, NumOps,
296 MIOperandNo += NumOps;
302 /// getOperandNamed - Return the index of the operand with the specified
303 /// non-empty name. If the instruction does not have an operand with the
304 /// specified name, throw an exception.
306 unsigned CodeGenInstruction::getOperandNamed(const std::string &Name) const {
307 assert(!Name.empty() && "Cannot search for operand with no name!");
308 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
309 if (OperandList[i].Name == Name) return i;
310 throw "Instruction '" + TheDef->getName() +
311 "' does not have an operand named '$" + Name + "'!";