blob: 61e237f1bd9a2c6c99cd741dc2503783172e0171 [file] [log] [blame]
Chris Lattner803a5f62004-08-01 04:04:35 +00001//===- CodeGenTarget.cpp - CodeGen Target Class Wrapper ---------*- C++ -*-===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell01d45822003-10-20 20:20:30 +00003// The LLVM Compiler Infrastructure
4//
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.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell01d45822003-10-20 20:20:30 +00008//===----------------------------------------------------------------------===//
Chris Lattner45872072003-08-07 05:38:11 +00009//
Chris Lattner803a5f62004-08-01 04:04:35 +000010// This class wrap target description classes used by the various code
Chris Lattner45872072003-08-07 05:38:11 +000011// 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.
14//
15//===----------------------------------------------------------------------===//
16
Chris Lattner803a5f62004-08-01 04:04:35 +000017#include "CodeGenTarget.h"
Chris Lattner45872072003-08-07 05:38:11 +000018#include "Record.h"
Chris Lattner560a79f2004-10-03 19:34:31 +000019#include "llvm/ADT/StringExtras.h"
20#include "llvm/Support/CommandLine.h"
Chris Lattner2082ebe2004-08-01 03:55:39 +000021using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000022
Chris Lattner560a79f2004-10-03 19:34:31 +000023static cl::opt<unsigned>
24AsmWriterNum("asmwriternum", cl::init(0),
25 cl::desc("Make -gen-asm-writer emit assembly writer #N"));
26
Chris Lattner45872072003-08-07 05:38:11 +000027/// getValueType - Return the MCV::ValueType that the specified TableGen record
28/// corresponds to.
Chris Lattner2082ebe2004-08-01 03:55:39 +000029MVT::ValueType llvm::getValueType(Record *Rec) {
Chris Lattner45872072003-08-07 05:38:11 +000030 return (MVT::ValueType)Rec->getValueAsInt("Value");
31}
32
Chris Lattner2082ebe2004-08-01 03:55:39 +000033std::string llvm::getName(MVT::ValueType T) {
Chris Lattner45872072003-08-07 05:38:11 +000034 switch (T) {
Chris Lattnerd3464c12003-08-07 23:15:21 +000035 case MVT::Other: return "UNKNOWN";
36 case MVT::i1: return "i1";
37 case MVT::i8: return "i8";
38 case MVT::i16: return "i16";
39 case MVT::i32: return "i32";
40 case MVT::i64: return "i64";
41 case MVT::i128: return "i128";
42 case MVT::f32: return "f32";
43 case MVT::f64: return "f64";
44 case MVT::f80: return "f80";
45 case MVT::f128: return "f128";
46 case MVT::isVoid:return "void";
47 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
Chris Lattner45872072003-08-07 05:38:11 +000048 }
Chris Lattnerd3464c12003-08-07 23:15:21 +000049}
50
Chris Lattner2082ebe2004-08-01 03:55:39 +000051std::string llvm::getEnumName(MVT::ValueType T) {
Chris Lattnerb72fb7e2003-08-10 19:50:32 +000052 switch (T) {
53 case MVT::Other: return "Other";
54 case MVT::i1: return "i1";
55 case MVT::i8: return "i8";
56 case MVT::i16: return "i16";
57 case MVT::i32: return "i32";
58 case MVT::i64: return "i64";
59 case MVT::i128: return "i128";
60 case MVT::f32: return "f32";
61 case MVT::f64: return "f64";
62 case MVT::f80: return "f80";
63 case MVT::f128: return "f128";
64 case MVT::isVoid:return "isVoid";
65 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
66 }
67}
68
Chris Lattnerd3464c12003-08-07 23:15:21 +000069
Chris Lattner2082ebe2004-08-01 03:55:39 +000070std::ostream &llvm::operator<<(std::ostream &OS, MVT::ValueType T) {
Chris Lattnerd3464c12003-08-07 23:15:21 +000071 return OS << getName(T);
Chris Lattner45872072003-08-07 05:38:11 +000072}
73
74
Chris Lattner45872072003-08-07 05:38:11 +000075/// getTarget - Return the current instance of the Target class.
76///
Brian Gaekebe7f4af2003-10-10 21:55:29 +000077CodeGenTarget::CodeGenTarget() : PointerType(MVT::Other) {
Chris Lattner45872072003-08-07 05:38:11 +000078 std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target");
Misha Brukmanbebdb202004-06-04 14:59:42 +000079 if (Targets.size() == 0)
Misha Brukman3da94ae2005-04-22 00:00:37 +000080 throw std::string("ERROR: No 'Target' subclasses defined!");
Chris Lattner45872072003-08-07 05:38:11 +000081 if (Targets.size() != 1)
82 throw std::string("ERROR: Multiple subclasses of Target defined!");
83 TargetRec = Targets[0];
84
85 // Read in all of the CalleeSavedRegisters...
86 ListInit *LI = TargetRec->getValueAsListInit("CalleeSavedRegisters");
87 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
88 if (DefInit *DI = dynamic_cast<DefInit*>(LI->getElement(i)))
89 CalleeSavedRegisters.push_back(DI->getDef());
90 else
91 throw "Target: " + TargetRec->getName() +
92 " expected register definition in CalleeSavedRegisters list!";
93
94 PointerType = getValueType(TargetRec->getValueAsDef("PointerType"));
95}
96
97
98const std::string &CodeGenTarget::getName() const {
99 return TargetRec->getName();
100}
101
102Record *CodeGenTarget::getInstructionSet() const {
103 return TargetRec->getValueAsDef("InstructionSet");
104}
Brian Gaeked0fde302003-11-11 22:41:34 +0000105
Chris Lattner175580c2004-08-14 22:50:53 +0000106/// getAsmWriter - Return the AssemblyWriter definition for this target.
107///
108Record *CodeGenTarget::getAsmWriter() const {
Chris Lattner560a79f2004-10-03 19:34:31 +0000109 ListInit *LI = TargetRec->getValueAsListInit("AssemblyWriters");
110 if (AsmWriterNum >= LI->getSize())
111 throw "Target does not have an AsmWriter #" + utostr(AsmWriterNum) + "!";
112 DefInit *DI = dynamic_cast<DefInit*>(LI->getElement(AsmWriterNum));
113 if (!DI) throw std::string("AssemblyWriter list should be a list of defs!");
114 return DI->getDef();
Chris Lattner175580c2004-08-14 22:50:53 +0000115}
116
Chris Lattner26693112004-08-16 01:10:21 +0000117void CodeGenTarget::ReadRegisters() const {
118 std::vector<Record*> Regs = Records.getAllDerivedDefinitions("Register");
119 if (Regs.empty())
120 throw std::string("No 'Register' subclasses defined!");
121
122 Registers.reserve(Regs.size());
123 Registers.assign(Regs.begin(), Regs.end());
124}
125
Chris Lattner7a680c62004-08-21 02:24:57 +0000126CodeGenRegister::CodeGenRegister(Record *R) : TheDef(R) {
127 DeclaredSpillSize = R->getValueAsInt("SpillSize");
128 DeclaredSpillAlignment = R->getValueAsInt("SpillAlignment");
129}
130
Chris Lattner26693112004-08-16 01:10:21 +0000131const std::string &CodeGenRegister::getName() const {
132 return TheDef->getName();
133}
134
Chris Lattner056afef2004-08-21 04:05:00 +0000135void CodeGenTarget::ReadRegisterClasses() const {
136 std::vector<Record*> RegClasses =
137 Records.getAllDerivedDefinitions("RegisterClass");
138 if (RegClasses.empty())
139 throw std::string("No 'RegisterClass' subclasses defined!");
140
141 RegisterClasses.reserve(RegClasses.size());
142 RegisterClasses.assign(RegClasses.begin(), RegClasses.end());
143}
144
145CodeGenRegisterClass::CodeGenRegisterClass(Record *R) : TheDef(R) {
Chris Lattnerc67c18f2005-08-19 18:45:20 +0000146 // Rename anonymous register classes.
147 if (R->getName().size() > 9 && R->getName()[9] == '.') {
148 static unsigned AnonCounter = 0;
149 R->setName("AnonRegClass_"+utostr(AnonCounter++));
150 }
151
152 Namespace = R->getValueAsString("Namespace");
Chris Lattner056afef2004-08-21 04:05:00 +0000153 SpillSize = R->getValueAsInt("Size");
Chris Lattner037d7322004-08-21 20:15:25 +0000154 SpillAlignment = R->getValueAsInt("Alignment");
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000155 VT = getValueType(R->getValueAsDef("RegType"));
Chris Lattner056afef2004-08-21 04:05:00 +0000156
Chris Lattnerac468932005-08-19 19:12:51 +0000157 if (CodeInit *CI = dynamic_cast<CodeInit*>(R->getValueInit("MethodBodies")))
158 MethodBodies = CI->getValue();
Chris Lattner57677752004-08-21 19:21:21 +0000159 else
Chris Lattnerac468932005-08-19 19:12:51 +0000160 throw "Expected 'code' fragment for 'MethodBodies' value in register "
161 "class '" + getName() + "'!";
Chris Lattner57677752004-08-21 19:21:21 +0000162
Chris Lattnerac468932005-08-19 19:12:51 +0000163 if (CodeInit *CI = dynamic_cast<CodeInit*>(R->getValueInit("MethodProtos")))
164 MethodProtos = CI->getValue();
165 else
166 throw "Expected 'code' fragment for 'MethodProtos' value in register "
167 "class '" + getName() + "'!";
168
Chris Lattner056afef2004-08-21 04:05:00 +0000169 ListInit *RegList = R->getValueAsListInit("MemberList");
170 for (unsigned i = 0, e = RegList->getSize(); i != e; ++i) {
171 DefInit *RegDef = dynamic_cast<DefInit*>(RegList->getElement(i));
Misha Brukman3da94ae2005-04-22 00:00:37 +0000172 if (!RegDef) throw "Register class member is not a record!";
Chris Lattner056afef2004-08-21 04:05:00 +0000173 Record *Reg = RegDef->getDef();
174
175 if (!Reg->isSubClassOf("Register"))
176 throw "Register Class member '" + Reg->getName() +
177 "' does not derive from the Register class!";
178 Elements.push_back(Reg);
179 }
180}
181
182const std::string &CodeGenRegisterClass::getName() const {
183 return TheDef->getName();
184}
185
Chris Lattnere9f4ba82005-09-08 21:43:21 +0000186void CodeGenTarget::ReadLegalValueTypes() const {
187 const std::vector<CodeGenRegisterClass> &RCs = getRegisterClasses();
188 for (unsigned i = 0, e = RCs.size(); i != e; ++i)
189 LegalValueTypes.push_back(RCs[i].VT);
190}
Chris Lattner056afef2004-08-21 04:05:00 +0000191
Chris Lattner175580c2004-08-14 22:50:53 +0000192
Chris Lattnerec352402004-08-01 05:04:00 +0000193void CodeGenTarget::ReadInstructions() const {
194 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
195
Chris Lattner26693112004-08-16 01:10:21 +0000196 if (Insts.empty())
Chris Lattnerec352402004-08-01 05:04:00 +0000197 throw std::string("No 'Instruction' subclasses defined!");
198
Chris Lattner175580c2004-08-14 22:50:53 +0000199 std::string InstFormatName =
200 getAsmWriter()->getValueAsString("InstFormatName");
201
202 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
203 std::string AsmStr = Insts[i]->getValueAsString(InstFormatName);
204 Instructions.insert(std::make_pair(Insts[i]->getName(),
205 CodeGenInstruction(Insts[i], AsmStr)));
206 }
Chris Lattnerec352402004-08-01 05:04:00 +0000207}
208
209/// getPHIInstruction - Return the designated PHI instruction.
Misha Brukman35e83cc2004-10-14 05:50:43 +0000210///
Chris Lattnerec352402004-08-01 05:04:00 +0000211const CodeGenInstruction &CodeGenTarget::getPHIInstruction() const {
212 Record *PHI = getInstructionSet()->getValueAsDef("PHIInst");
213 std::map<std::string, CodeGenInstruction>::const_iterator I =
214 getInstructions().find(PHI->getName());
215 if (I == Instructions.end())
216 throw "Could not find PHI instruction named '" + PHI->getName() + "'!";
217 return I->second;
218}
219
Chris Lattnerd6488672005-01-22 18:58:51 +0000220/// getInstructionsByEnumValue - Return all of the instructions defined by the
221/// target, ordered by their enum value.
222void CodeGenTarget::
223getInstructionsByEnumValue(std::vector<const CodeGenInstruction*>
224 &NumberedInstructions) {
225
226 // Print out the rest of the instructions now.
227 unsigned i = 0;
228 const CodeGenInstruction *PHI = &getPHIInstruction();
229 NumberedInstructions.push_back(PHI);
230 for (inst_iterator II = inst_begin(), E = inst_end(); II != E; ++II)
231 if (&II->second != PHI)
232 NumberedInstructions.push_back(&II->second);
233}
234
235
Misha Brukman35e83cc2004-10-14 05:50:43 +0000236/// isLittleEndianEncoding - Return whether this target encodes its instruction
237/// in little-endian format, i.e. bits laid out in the order [0..n]
238///
239bool CodeGenTarget::isLittleEndianEncoding() const {
240 return getInstructionSet()->getValueAsBit("isLittleEndianEncoding");
241}
242
Chris Lattner175580c2004-08-14 22:50:53 +0000243CodeGenInstruction::CodeGenInstruction(Record *R, const std::string &AsmStr)
244 : TheDef(R), AsmString(AsmStr) {
Chris Lattnerec352402004-08-01 05:04:00 +0000245 Name = R->getValueAsString("Name");
246 Namespace = R->getValueAsString("Namespace");
Chris Lattnerec352402004-08-01 05:04:00 +0000247
Chris Lattnerec352402004-08-01 05:04:00 +0000248 isReturn = R->getValueAsBit("isReturn");
249 isBranch = R->getValueAsBit("isBranch");
250 isBarrier = R->getValueAsBit("isBarrier");
251 isCall = R->getValueAsBit("isCall");
Nate Begemancdd66b52004-09-28 21:01:45 +0000252 isLoad = R->getValueAsBit("isLoad");
253 isStore = R->getValueAsBit("isStore");
Chris Lattnerec352402004-08-01 05:04:00 +0000254 isTwoAddress = R->getValueAsBit("isTwoAddress");
Chris Lattneraad75aa2005-01-02 02:29:04 +0000255 isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
256 isCommutable = R->getValueAsBit("isCommutable");
Chris Lattnerec352402004-08-01 05:04:00 +0000257 isTerminator = R->getValueAsBit("isTerminator");
Chris Lattner5b71d3a2004-09-28 18:38:01 +0000258 hasDelaySlot = R->getValueAsBit("hasDelaySlot");
Chris Lattnere3cbf822005-08-26 20:55:40 +0000259 usesCustomDAGSchedInserter = R->getValueAsBit("usesCustomDAGSchedInserter");
Chris Lattnercfbf96a2005-08-18 23:38:41 +0000260 hasVariableNumberOfOperands = false;
261
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000262 try {
263 DagInit *DI = R->getValueAsDag("OperandList");
264
Chris Lattnercf03da02004-08-11 02:22:39 +0000265 unsigned MIOperandNo = 0;
Chris Lattner87c59052004-08-01 07:42:39 +0000266 for (unsigned i = 0, e = DI->getNumArgs(); i != e; ++i)
267 if (DefInit *Arg = dynamic_cast<DefInit*>(DI->getArg(i))) {
268 Record *Rec = Arg->getDef();
269 MVT::ValueType Ty;
Chris Lattnercf03da02004-08-11 02:22:39 +0000270 std::string PrintMethod = "printOperand";
271 unsigned NumOps = 1;
Chris Lattnercfbf96a2005-08-18 23:38:41 +0000272 if (Rec->isSubClassOf("RegisterClass")) {
Chris Lattner87c59052004-08-01 07:42:39 +0000273 Ty = getValueType(Rec->getValueAsDef("RegType"));
Chris Lattnercfbf96a2005-08-18 23:38:41 +0000274 } else if (Rec->isSubClassOf("Operand")) {
Chris Lattner552a8422004-08-11 01:53:58 +0000275 Ty = getValueType(Rec->getValueAsDef("Type"));
Chris Lattnercf03da02004-08-11 02:22:39 +0000276 PrintMethod = Rec->getValueAsString("PrintMethod");
277 NumOps = Rec->getValueAsInt("NumMIOperands");
Chris Lattnercfbf96a2005-08-18 23:38:41 +0000278 } else if (Rec->getName() == "variable_ops") {
279 hasVariableNumberOfOperands = true;
Chris Lattnerbfd4f552005-08-19 06:16:04 +0000280 continue;
Chris Lattnercf03da02004-08-11 02:22:39 +0000281 } else
Chris Lattner87c59052004-08-01 07:42:39 +0000282 throw "Unknown operand class '" + Rec->getName() +
283 "' in instruction '" + R->getName() + "' instruction!";
Misha Brukman3da94ae2005-04-22 00:00:37 +0000284
Chris Lattnercf03da02004-08-11 02:22:39 +0000285 OperandList.push_back(OperandInfo(Rec, Ty, DI->getArgName(i),
Chris Lattnercfbf96a2005-08-18 23:38:41 +0000286 PrintMethod, MIOperandNo, NumOps));
Chris Lattnercf03da02004-08-11 02:22:39 +0000287 MIOperandNo += NumOps;
Chris Lattner87c59052004-08-01 07:42:39 +0000288 } else {
289 throw "Illegal operand for the '" + R->getName() + "' instruction!";
290 }
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000291 } catch (...) {
Chris Lattner87c59052004-08-01 07:42:39 +0000292 // Error parsing operands list, just ignore it.
293 AsmString.clear();
294 OperandList.clear();
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000295 }
Chris Lattnerec352402004-08-01 05:04:00 +0000296}
297
298
Chris Lattner87c59052004-08-01 07:42:39 +0000299
300/// getOperandNamed - Return the index of the operand with the specified
301/// non-empty name. If the instruction does not have an operand with the
302/// specified name, throw an exception.
Misha Brukman35e83cc2004-10-14 05:50:43 +0000303///
Chris Lattner87c59052004-08-01 07:42:39 +0000304unsigned CodeGenInstruction::getOperandNamed(const std::string &Name) const {
305 assert(!Name.empty() && "Cannot search for operand with no name!");
306 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
307 if (OperandList[i].Name == Name) return i;
308 throw "Instruction '" + TheDef->getName() +
309 "' does not have an operand named '$" + Name + "'!";
310}