blob: e0d585c6187a10440257a819bb1be2452724fa68 [file] [log] [blame]
Chris Lattner803a5f62004-08-01 04:04:35 +00001//===- CodeGenTarget.cpp - CodeGen Target Class Wrapper ---------*- C++ -*-===//
John Criswell01d45822003-10-20 20:20:30 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
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 Lattner2082ebe2004-08-01 03:55:39 +000019using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000020
Chris Lattner45872072003-08-07 05:38:11 +000021/// getValueType - Return the MCV::ValueType that the specified TableGen record
22/// corresponds to.
Chris Lattner2082ebe2004-08-01 03:55:39 +000023MVT::ValueType llvm::getValueType(Record *Rec) {
Chris Lattner45872072003-08-07 05:38:11 +000024 return (MVT::ValueType)Rec->getValueAsInt("Value");
25}
26
Chris Lattner2082ebe2004-08-01 03:55:39 +000027std::string llvm::getName(MVT::ValueType T) {
Chris Lattner45872072003-08-07 05:38:11 +000028 switch (T) {
Chris Lattnerd3464c12003-08-07 23:15:21 +000029 case MVT::Other: return "UNKNOWN";
30 case MVT::i1: return "i1";
31 case MVT::i8: return "i8";
32 case MVT::i16: return "i16";
33 case MVT::i32: return "i32";
34 case MVT::i64: return "i64";
35 case MVT::i128: return "i128";
36 case MVT::f32: return "f32";
37 case MVT::f64: return "f64";
38 case MVT::f80: return "f80";
39 case MVT::f128: return "f128";
40 case MVT::isVoid:return "void";
41 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
Chris Lattner45872072003-08-07 05:38:11 +000042 }
Chris Lattnerd3464c12003-08-07 23:15:21 +000043}
44
Chris Lattner2082ebe2004-08-01 03:55:39 +000045std::string llvm::getEnumName(MVT::ValueType T) {
Chris Lattnerb72fb7e2003-08-10 19:50:32 +000046 switch (T) {
47 case MVT::Other: return "Other";
48 case MVT::i1: return "i1";
49 case MVT::i8: return "i8";
50 case MVT::i16: return "i16";
51 case MVT::i32: return "i32";
52 case MVT::i64: return "i64";
53 case MVT::i128: return "i128";
54 case MVT::f32: return "f32";
55 case MVT::f64: return "f64";
56 case MVT::f80: return "f80";
57 case MVT::f128: return "f128";
58 case MVT::isVoid:return "isVoid";
59 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
60 }
61}
62
Chris Lattnerd3464c12003-08-07 23:15:21 +000063
Chris Lattner2082ebe2004-08-01 03:55:39 +000064std::ostream &llvm::operator<<(std::ostream &OS, MVT::ValueType T) {
Chris Lattnerd3464c12003-08-07 23:15:21 +000065 return OS << getName(T);
Chris Lattner45872072003-08-07 05:38:11 +000066}
67
68
Chris Lattner45872072003-08-07 05:38:11 +000069/// getTarget - Return the current instance of the Target class.
70///
Brian Gaekebe7f4af2003-10-10 21:55:29 +000071CodeGenTarget::CodeGenTarget() : PointerType(MVT::Other) {
Chris Lattner45872072003-08-07 05:38:11 +000072 std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target");
Misha Brukmanbebdb202004-06-04 14:59:42 +000073 if (Targets.size() == 0)
74 throw std::string("ERROR: No 'Target' subclasses defined!");
Chris Lattner45872072003-08-07 05:38:11 +000075 if (Targets.size() != 1)
76 throw std::string("ERROR: Multiple subclasses of Target defined!");
77 TargetRec = Targets[0];
78
79 // Read in all of the CalleeSavedRegisters...
80 ListInit *LI = TargetRec->getValueAsListInit("CalleeSavedRegisters");
81 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
82 if (DefInit *DI = dynamic_cast<DefInit*>(LI->getElement(i)))
83 CalleeSavedRegisters.push_back(DI->getDef());
84 else
85 throw "Target: " + TargetRec->getName() +
86 " expected register definition in CalleeSavedRegisters list!";
87
88 PointerType = getValueType(TargetRec->getValueAsDef("PointerType"));
89}
90
91
92const std::string &CodeGenTarget::getName() const {
93 return TargetRec->getName();
94}
95
96Record *CodeGenTarget::getInstructionSet() const {
97 return TargetRec->getValueAsDef("InstructionSet");
98}
Brian Gaeked0fde302003-11-11 22:41:34 +000099
Chris Lattner175580c2004-08-14 22:50:53 +0000100/// getAsmWriter - Return the AssemblyWriter definition for this target.
101///
102Record *CodeGenTarget::getAsmWriter() const {
103 return TargetRec->getValueAsDef("AssemblyWriter");
104}
105
Chris Lattner26693112004-08-16 01:10:21 +0000106void CodeGenTarget::ReadRegisters() const {
107 std::vector<Record*> Regs = Records.getAllDerivedDefinitions("Register");
108 if (Regs.empty())
109 throw std::string("No 'Register' subclasses defined!");
110
111 Registers.reserve(Regs.size());
112 Registers.assign(Regs.begin(), Regs.end());
113}
114
Chris Lattner7a680c62004-08-21 02:24:57 +0000115CodeGenRegister::CodeGenRegister(Record *R) : TheDef(R) {
116 DeclaredSpillSize = R->getValueAsInt("SpillSize");
117 DeclaredSpillAlignment = R->getValueAsInt("SpillAlignment");
118}
119
Chris Lattner26693112004-08-16 01:10:21 +0000120const std::string &CodeGenRegister::getName() const {
121 return TheDef->getName();
122}
123
Chris Lattner056afef2004-08-21 04:05:00 +0000124void CodeGenTarget::ReadRegisterClasses() const {
125 std::vector<Record*> RegClasses =
126 Records.getAllDerivedDefinitions("RegisterClass");
127 if (RegClasses.empty())
128 throw std::string("No 'RegisterClass' subclasses defined!");
129
130 RegisterClasses.reserve(RegClasses.size());
131 RegisterClasses.assign(RegClasses.begin(), RegClasses.end());
132}
133
134CodeGenRegisterClass::CodeGenRegisterClass(Record *R) : TheDef(R) {
135 SpillSize = R->getValueAsInt("Size");
136 SpillAlignment = R->getValueAsInt("Alignment");
137
138 ListInit *RegList = R->getValueAsListInit("MemberList");
139 for (unsigned i = 0, e = RegList->getSize(); i != e; ++i) {
140 DefInit *RegDef = dynamic_cast<DefInit*>(RegList->getElement(i));
141 if (!RegDef) throw "Register class member is not a record!";
142 Record *Reg = RegDef->getDef();
143
144 if (!Reg->isSubClassOf("Register"))
145 throw "Register Class member '" + Reg->getName() +
146 "' does not derive from the Register class!";
147 Elements.push_back(Reg);
148 }
149}
150
151const std::string &CodeGenRegisterClass::getName() const {
152 return TheDef->getName();
153}
154
155
Chris Lattner175580c2004-08-14 22:50:53 +0000156
Chris Lattnerec352402004-08-01 05:04:00 +0000157void CodeGenTarget::ReadInstructions() const {
158 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
159
Chris Lattner26693112004-08-16 01:10:21 +0000160 if (Insts.empty())
Chris Lattnerec352402004-08-01 05:04:00 +0000161 throw std::string("No 'Instruction' subclasses defined!");
162
Chris Lattner175580c2004-08-14 22:50:53 +0000163 std::string InstFormatName =
164 getAsmWriter()->getValueAsString("InstFormatName");
165
166 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
167 std::string AsmStr = Insts[i]->getValueAsString(InstFormatName);
168 Instructions.insert(std::make_pair(Insts[i]->getName(),
169 CodeGenInstruction(Insts[i], AsmStr)));
170 }
Chris Lattnerec352402004-08-01 05:04:00 +0000171}
172
173/// getPHIInstruction - Return the designated PHI instruction.
174const CodeGenInstruction &CodeGenTarget::getPHIInstruction() const {
175 Record *PHI = getInstructionSet()->getValueAsDef("PHIInst");
176 std::map<std::string, CodeGenInstruction>::const_iterator I =
177 getInstructions().find(PHI->getName());
178 if (I == Instructions.end())
179 throw "Could not find PHI instruction named '" + PHI->getName() + "'!";
180 return I->second;
181}
182
Chris Lattner175580c2004-08-14 22:50:53 +0000183CodeGenInstruction::CodeGenInstruction(Record *R, const std::string &AsmStr)
184 : TheDef(R), AsmString(AsmStr) {
Chris Lattnerec352402004-08-01 05:04:00 +0000185 Name = R->getValueAsString("Name");
186 Namespace = R->getValueAsString("Namespace");
Chris Lattnerec352402004-08-01 05:04:00 +0000187
Chris Lattnerec352402004-08-01 05:04:00 +0000188 isReturn = R->getValueAsBit("isReturn");
189 isBranch = R->getValueAsBit("isBranch");
190 isBarrier = R->getValueAsBit("isBarrier");
191 isCall = R->getValueAsBit("isCall");
192 isTwoAddress = R->getValueAsBit("isTwoAddress");
193 isTerminator = R->getValueAsBit("isTerminator");
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000194
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000195 try {
196 DagInit *DI = R->getValueAsDag("OperandList");
197
Chris Lattnercf03da02004-08-11 02:22:39 +0000198 unsigned MIOperandNo = 0;
Chris Lattner87c59052004-08-01 07:42:39 +0000199 for (unsigned i = 0, e = DI->getNumArgs(); i != e; ++i)
200 if (DefInit *Arg = dynamic_cast<DefInit*>(DI->getArg(i))) {
201 Record *Rec = Arg->getDef();
202 MVT::ValueType Ty;
Chris Lattnercf03da02004-08-11 02:22:39 +0000203 std::string PrintMethod = "printOperand";
204 unsigned NumOps = 1;
Chris Lattner87c59052004-08-01 07:42:39 +0000205 if (Rec->isSubClassOf("RegisterClass"))
206 Ty = getValueType(Rec->getValueAsDef("RegType"));
Chris Lattnercf03da02004-08-11 02:22:39 +0000207 else if (Rec->isSubClassOf("Operand")) {
Chris Lattner552a8422004-08-11 01:53:58 +0000208 Ty = getValueType(Rec->getValueAsDef("Type"));
Chris Lattnercf03da02004-08-11 02:22:39 +0000209 PrintMethod = Rec->getValueAsString("PrintMethod");
210 NumOps = Rec->getValueAsInt("NumMIOperands");
211 } else
Chris Lattner87c59052004-08-01 07:42:39 +0000212 throw "Unknown operand class '" + Rec->getName() +
213 "' in instruction '" + R->getName() + "' instruction!";
214
Chris Lattnercf03da02004-08-11 02:22:39 +0000215 OperandList.push_back(OperandInfo(Rec, Ty, DI->getArgName(i),
216 PrintMethod, MIOperandNo));
217 MIOperandNo += NumOps;
Chris Lattner87c59052004-08-01 07:42:39 +0000218 } else {
219 throw "Illegal operand for the '" + R->getName() + "' instruction!";
220 }
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000221 } catch (...) {
Chris Lattner87c59052004-08-01 07:42:39 +0000222 // Error parsing operands list, just ignore it.
223 AsmString.clear();
224 OperandList.clear();
Chris Lattner2e1f51b2004-08-01 05:59:33 +0000225 }
Chris Lattnerec352402004-08-01 05:04:00 +0000226}
227
228
Chris Lattner87c59052004-08-01 07:42:39 +0000229
230/// getOperandNamed - Return the index of the operand with the specified
231/// non-empty name. If the instruction does not have an operand with the
232/// specified name, throw an exception.
233unsigned CodeGenInstruction::getOperandNamed(const std::string &Name) const {
234 assert(!Name.empty() && "Cannot search for operand with no name!");
235 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
236 if (OperandList[i].Name == Name) return i;
237 throw "Instruction '" + TheDef->getName() +
238 "' does not have an operand named '$" + Name + "'!";
239}