blob: 575852b1ad73a9741f6620cdf3a7a1d0b609538c [file] [log] [blame]
Chris Lattner6cc654b2008-01-06 01:35:39 +00001//===- CodeGenInstruction.cpp - CodeGen Instruction Class Wrapper ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the CodeGenInstruction class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenInstruction.h"
Chris Lattner9414ae52010-03-27 20:09:24 +000015#include "CodeGenTarget.h"
Chris Lattner6cc654b2008-01-06 01:35:39 +000016#include "Record.h"
17#include "llvm/ADT/StringExtras.h"
Chris Lattner3f2c8e42010-11-06 07:06:09 +000018#include "llvm/ADT/StringMap.h"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000019#include "llvm/ADT/STLExtras.h"
Chris Lattner6cc654b2008-01-06 01:35:39 +000020#include <set>
21using namespace llvm;
22
Chris Lattnerc240bb02010-11-01 04:03:32 +000023//===----------------------------------------------------------------------===//
24// CGIOperandList Implementation
25//===----------------------------------------------------------------------===//
Jim Grosbach06801722009-12-16 19:43:02 +000026
Chris Lattnerc240bb02010-11-01 04:03:32 +000027CGIOperandList::CGIOperandList(Record *R) : TheDef(R) {
28 isPredicable = false;
Chris Lattner6cc654b2008-01-06 01:35:39 +000029 hasOptionalDef = false;
Chris Lattner8f707e12008-01-07 05:19:29 +000030 isVariadic = false;
Chris Lattnerc240bb02010-11-01 04:03:32 +000031
Chris Lattnerf55eed22010-03-18 21:07:39 +000032 DagInit *OutDI = R->getValueAsDag("OutOperandList");
Chris Lattnerc240bb02010-11-01 04:03:32 +000033
Chris Lattnerf55eed22010-03-18 21:07:39 +000034 if (DefInit *Init = dynamic_cast<DefInit*>(OutDI->getOperator())) {
Chris Lattnerb0be4d22010-03-18 20:56:35 +000035 if (Init->getDef()->getName() != "outs")
Chris Lattnercedef1c2010-03-18 20:50:52 +000036 throw R->getName() + ": invalid def name for output list: use 'outs'";
37 } else
38 throw R->getName() + ": invalid output list: use 'outs'";
Chris Lattnerc240bb02010-11-01 04:03:32 +000039
Chris Lattnerf55eed22010-03-18 21:07:39 +000040 NumDefs = OutDI->getNumArgs();
Chris Lattnerc240bb02010-11-01 04:03:32 +000041
Chris Lattnerf55eed22010-03-18 21:07:39 +000042 DagInit *InDI = R->getValueAsDag("InOperandList");
43 if (DefInit *Init = dynamic_cast<DefInit*>(InDI->getOperator())) {
Chris Lattnerb0be4d22010-03-18 20:56:35 +000044 if (Init->getDef()->getName() != "ins")
Chris Lattnercedef1c2010-03-18 20:50:52 +000045 throw R->getName() + ": invalid def name for input list: use 'ins'";
46 } else
47 throw R->getName() + ": invalid input list: use 'ins'";
Chris Lattnerc240bb02010-11-01 04:03:32 +000048
Chris Lattner6cc654b2008-01-06 01:35:39 +000049 unsigned MIOperandNo = 0;
50 std::set<std::string> OperandNames;
Chris Lattnerf55eed22010-03-18 21:07:39 +000051 for (unsigned i = 0, e = InDI->getNumArgs()+OutDI->getNumArgs(); i != e; ++i){
52 Init *ArgInit;
53 std::string ArgName;
54 if (i < NumDefs) {
55 ArgInit = OutDI->getArg(i);
56 ArgName = OutDI->getArgName(i);
57 } else {
58 ArgInit = InDI->getArg(i-NumDefs);
59 ArgName = InDI->getArgName(i-NumDefs);
60 }
61
62 DefInit *Arg = dynamic_cast<DefInit*>(ArgInit);
Chris Lattner6cc654b2008-01-06 01:35:39 +000063 if (!Arg)
64 throw "Illegal operand for the '" + R->getName() + "' instruction!";
Chris Lattnerc240bb02010-11-01 04:03:32 +000065
Chris Lattner6cc654b2008-01-06 01:35:39 +000066 Record *Rec = Arg->getDef();
67 std::string PrintMethod = "printOperand";
Jim Grosbach5013f742010-10-12 22:21:57 +000068 std::string EncoderMethod;
Chris Lattner6cc654b2008-01-06 01:35:39 +000069 unsigned NumOps = 1;
70 DagInit *MIOpInfo = 0;
71 if (Rec->isSubClassOf("Operand")) {
72 PrintMethod = Rec->getValueAsString("PrintMethod");
Jim Grosbach5013f742010-10-12 22:21:57 +000073 // If there is an explicit encoder method, use it.
Chris Lattner2ac19022010-11-15 05:19:05 +000074 EncoderMethod = Rec->getValueAsString("EncoderMethod");
Chris Lattner6cc654b2008-01-06 01:35:39 +000075 MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
Chris Lattnerc240bb02010-11-01 04:03:32 +000076
Chris Lattner6cc654b2008-01-06 01:35:39 +000077 // Verify that MIOpInfo has an 'ops' root value.
78 if (!dynamic_cast<DefInit*>(MIOpInfo->getOperator()) ||
79 dynamic_cast<DefInit*>(MIOpInfo->getOperator())
Chris Lattnerc240bb02010-11-01 04:03:32 +000080 ->getDef()->getName() != "ops")
Chris Lattner6cc654b2008-01-06 01:35:39 +000081 throw "Bad value for MIOperandInfo in operand '" + Rec->getName() +
Chris Lattnerc240bb02010-11-01 04:03:32 +000082 "'\n";
83
Chris Lattner6cc654b2008-01-06 01:35:39 +000084 // If we have MIOpInfo, then we have #operands equal to number of entries
85 // in MIOperandInfo.
86 if (unsigned NumArgs = MIOpInfo->getNumArgs())
87 NumOps = NumArgs;
Chris Lattnerc240bb02010-11-01 04:03:32 +000088
Chris Lattner6cc654b2008-01-06 01:35:39 +000089 if (Rec->isSubClassOf("PredicateOperand"))
90 isPredicable = true;
91 else if (Rec->isSubClassOf("OptionalDefOperand"))
92 hasOptionalDef = true;
93 } else if (Rec->getName() == "variable_ops") {
Chris Lattner8f707e12008-01-07 05:19:29 +000094 isVariadic = true;
Chris Lattner6cc654b2008-01-06 01:35:39 +000095 continue;
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000096 } else if (!Rec->isSubClassOf("RegisterClass") &&
Dan Gohmane4c67cd2008-05-31 02:11:25 +000097 Rec->getName() != "ptr_rc" && Rec->getName() != "unknown")
Chris Lattner6cc654b2008-01-06 01:35:39 +000098 throw "Unknown operand class '" + Rec->getName() +
Chris Lattnerc240bb02010-11-01 04:03:32 +000099 "' in '" + R->getName() + "' instruction!";
100
Chris Lattner6cc654b2008-01-06 01:35:39 +0000101 // Check that the operand has a name and that it's unique.
Chris Lattnerf55eed22010-03-18 21:07:39 +0000102 if (ArgName.empty())
Chris Lattner6cc654b2008-01-06 01:35:39 +0000103 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
Chris Lattnerc240bb02010-11-01 04:03:32 +0000104 " has no name!";
Chris Lattnerf55eed22010-03-18 21:07:39 +0000105 if (!OperandNames.insert(ArgName).second)
Chris Lattner6cc654b2008-01-06 01:35:39 +0000106 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
Chris Lattnerc240bb02010-11-01 04:03:32 +0000107 " has the same name as a previous operand!";
108
Jim Grosbach5013f742010-10-12 22:21:57 +0000109 OperandList.push_back(OperandInfo(Rec, ArgName, PrintMethod, EncoderMethod,
Chris Lattner6cc654b2008-01-06 01:35:39 +0000110 MIOperandNo, NumOps, MIOpInfo));
111 MIOperandNo += NumOps;
112 }
Chris Lattnerb501d4f2010-11-01 05:34:34 +0000113
114
115 // Make sure the constraints list for each operand is large enough to hold
116 // constraint info, even if none is present.
117 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
118 OperandList[i].Constraints.resize(OperandList[i].MINumOperands);
Chris Lattner6cc654b2008-01-06 01:35:39 +0000119}
120
Chris Lattnerc240bb02010-11-01 04:03:32 +0000121
Chris Lattner6cc654b2008-01-06 01:35:39 +0000122/// getOperandNamed - Return the index of the operand with the specified
123/// non-empty name. If the instruction does not have an operand with the
124/// specified name, throw an exception.
125///
Chris Lattnerc240bb02010-11-01 04:03:32 +0000126unsigned CGIOperandList::getOperandNamed(StringRef Name) const {
Jim Grosbach01855072010-10-11 18:25:51 +0000127 unsigned OpIdx;
128 if (hasOperandNamed(Name, OpIdx)) return OpIdx;
Chris Lattnerc240bb02010-11-01 04:03:32 +0000129 throw "'" + TheDef->getName() + "' does not have an operand named '$" +
130 Name.str() + "'!";
Chris Lattner6cc654b2008-01-06 01:35:39 +0000131}
132
Jim Grosbach01855072010-10-11 18:25:51 +0000133/// hasOperandNamed - Query whether the instruction has an operand of the
134/// given name. If so, return true and set OpIdx to the index of the
135/// operand. Otherwise, return false.
Chris Lattnerc240bb02010-11-01 04:03:32 +0000136bool CGIOperandList::hasOperandNamed(StringRef Name, unsigned &OpIdx) const {
Jim Grosbach01855072010-10-11 18:25:51 +0000137 assert(!Name.empty() && "Cannot search for operand with no name!");
138 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
139 if (OperandList[i].Name == Name) {
140 OpIdx = i;
141 return true;
142 }
143 return false;
144}
145
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000146std::pair<unsigned,unsigned>
Chris Lattnerc240bb02010-11-01 04:03:32 +0000147CGIOperandList::ParseOperandName(const std::string &Op, bool AllowWholeOp) {
Chris Lattner6cc654b2008-01-06 01:35:39 +0000148 if (Op.empty() || Op[0] != '$')
149 throw TheDef->getName() + ": Illegal operand name: '" + Op + "'";
Chris Lattnerc240bb02010-11-01 04:03:32 +0000150
Chris Lattner6cc654b2008-01-06 01:35:39 +0000151 std::string OpName = Op.substr(1);
152 std::string SubOpName;
Chris Lattnerc240bb02010-11-01 04:03:32 +0000153
Chris Lattner6cc654b2008-01-06 01:35:39 +0000154 // Check to see if this is $foo.bar.
155 std::string::size_type DotIdx = OpName.find_first_of(".");
156 if (DotIdx != std::string::npos) {
157 SubOpName = OpName.substr(DotIdx+1);
158 if (SubOpName.empty())
159 throw TheDef->getName() + ": illegal empty suboperand name in '" +Op +"'";
160 OpName = OpName.substr(0, DotIdx);
161 }
Chris Lattnerc240bb02010-11-01 04:03:32 +0000162
Chris Lattner6cc654b2008-01-06 01:35:39 +0000163 unsigned OpIdx = getOperandNamed(OpName);
Chris Lattnerc240bb02010-11-01 04:03:32 +0000164
Chris Lattner6cc654b2008-01-06 01:35:39 +0000165 if (SubOpName.empty()) { // If no suboperand name was specified:
166 // If one was needed, throw.
167 if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp &&
168 SubOpName.empty())
169 throw TheDef->getName() + ": Illegal to refer to"
Chris Lattnerc240bb02010-11-01 04:03:32 +0000170 " whole operand part of complex operand '" + Op + "'";
171
Chris Lattner6cc654b2008-01-06 01:35:39 +0000172 // Otherwise, return the operand.
173 return std::make_pair(OpIdx, 0U);
174 }
Chris Lattnerc240bb02010-11-01 04:03:32 +0000175
Chris Lattner6cc654b2008-01-06 01:35:39 +0000176 // Find the suboperand number involved.
177 DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
178 if (MIOpInfo == 0)
179 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
Chris Lattnerc240bb02010-11-01 04:03:32 +0000180
Chris Lattner6cc654b2008-01-06 01:35:39 +0000181 // Find the operand with the right name.
182 for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i)
183 if (MIOpInfo->getArgName(i) == SubOpName)
184 return std::make_pair(OpIdx, i);
Chris Lattnerc240bb02010-11-01 04:03:32 +0000185
Chris Lattner6cc654b2008-01-06 01:35:39 +0000186 // Otherwise, didn't find it!
187 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
188}
Chris Lattner9414ae52010-03-27 20:09:24 +0000189
Chris Lattnerc240bb02010-11-01 04:03:32 +0000190static void ParseConstraint(const std::string &CStr, CGIOperandList &Ops) {
191 // EARLY_CLOBBER: @early $reg
192 std::string::size_type wpos = CStr.find_first_of(" \t");
193 std::string::size_type start = CStr.find_first_not_of(" \t");
194 std::string Tok = CStr.substr(start, wpos - start);
195 if (Tok == "@earlyclobber") {
196 std::string Name = CStr.substr(wpos+1);
197 wpos = Name.find_first_not_of(" \t");
198 if (wpos == std::string::npos)
199 throw "Illegal format for @earlyclobber constraint: '" + CStr + "'";
200 Name = Name.substr(wpos);
201 std::pair<unsigned,unsigned> Op = Ops.ParseOperandName(Name, false);
202
203 // Build the string for the operand
204 if (!Ops[Op.first].Constraints[Op.second].isNone())
205 throw "Operand '" + Name + "' cannot have multiple constraints!";
206 Ops[Op.first].Constraints[Op.second] =
207 CGIOperandList::ConstraintInfo::getEarlyClobber();
208 return;
209 }
210
211 // Only other constraint is "TIED_TO" for now.
212 std::string::size_type pos = CStr.find_first_of('=');
213 assert(pos != std::string::npos && "Unrecognized constraint");
214 start = CStr.find_first_not_of(" \t");
215 std::string Name = CStr.substr(start, pos - start);
216
217 // TIED_TO: $src1 = $dst
218 wpos = Name.find_first_of(" \t");
219 if (wpos == std::string::npos)
220 throw "Illegal format for tied-to constraint: '" + CStr + "'";
221 std::string DestOpName = Name.substr(0, wpos);
222 std::pair<unsigned,unsigned> DestOp = Ops.ParseOperandName(DestOpName, false);
223
224 Name = CStr.substr(pos+1);
225 wpos = Name.find_first_not_of(" \t");
226 if (wpos == std::string::npos)
227 throw "Illegal format for tied-to constraint: '" + CStr + "'";
228
229 std::pair<unsigned,unsigned> SrcOp =
230 Ops.ParseOperandName(Name.substr(wpos), false);
231 if (SrcOp > DestOp)
232 throw "Illegal tied-to operand constraint '" + CStr + "'";
233
234
235 unsigned FlatOpNo = Ops.getFlattenedOperandNumber(SrcOp);
236
237 if (!Ops[DestOp.first].Constraints[DestOp.second].isNone())
238 throw "Operand '" + DestOpName + "' cannot have multiple constraints!";
239 Ops[DestOp.first].Constraints[DestOp.second] =
240 CGIOperandList::ConstraintInfo::getTied(FlatOpNo);
241}
242
243static void ParseConstraints(const std::string &CStr, CGIOperandList &Ops) {
Chris Lattnerc240bb02010-11-01 04:03:32 +0000244 if (CStr.empty()) return;
245
246 const std::string delims(",");
247 std::string::size_type bidx, eidx;
248
249 bidx = CStr.find_first_not_of(delims);
250 while (bidx != std::string::npos) {
251 eidx = CStr.find_first_of(delims, bidx);
252 if (eidx == std::string::npos)
253 eidx = CStr.length();
254
255 ParseConstraint(CStr.substr(bidx, eidx - bidx), Ops);
256 bidx = CStr.find_first_not_of(delims, eidx);
257 }
258}
259
260void CGIOperandList::ProcessDisableEncoding(std::string DisableEncoding) {
261 while (1) {
262 std::string OpName;
263 tie(OpName, DisableEncoding) = getToken(DisableEncoding, " ,\t");
264 if (OpName.empty()) break;
265
266 // Figure out which operand this is.
267 std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false);
268
269 // Mark the operand as not-to-be encoded.
270 if (Op.second >= OperandList[Op.first].DoNotEncode.size())
271 OperandList[Op.first].DoNotEncode.resize(Op.second+1);
272 OperandList[Op.first].DoNotEncode[Op.second] = true;
273 }
274
275}
276
277//===----------------------------------------------------------------------===//
278// CodeGenInstruction Implementation
279//===----------------------------------------------------------------------===//
280
281CodeGenInstruction::CodeGenInstruction(Record *R) : TheDef(R), Operands(R) {
282 Namespace = R->getValueAsString("Namespace");
283 AsmString = R->getValueAsString("AsmString");
284
285 isReturn = R->getValueAsBit("isReturn");
286 isBranch = R->getValueAsBit("isBranch");
287 isIndirectBranch = R->getValueAsBit("isIndirectBranch");
288 isCompare = R->getValueAsBit("isCompare");
289 isBarrier = R->getValueAsBit("isBarrier");
290 isCall = R->getValueAsBit("isCall");
291 canFoldAsLoad = R->getValueAsBit("canFoldAsLoad");
292 mayLoad = R->getValueAsBit("mayLoad");
293 mayStore = R->getValueAsBit("mayStore");
294 isPredicable = Operands.isPredicable || R->getValueAsBit("isPredicable");
295 isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
296 isCommutable = R->getValueAsBit("isCommutable");
297 isTerminator = R->getValueAsBit("isTerminator");
298 isReMaterializable = R->getValueAsBit("isReMaterializable");
299 hasDelaySlot = R->getValueAsBit("hasDelaySlot");
300 usesCustomInserter = R->getValueAsBit("usesCustomInserter");
301 hasCtrlDep = R->getValueAsBit("hasCtrlDep");
302 isNotDuplicable = R->getValueAsBit("isNotDuplicable");
303 hasSideEffects = R->getValueAsBit("hasSideEffects");
304 neverHasSideEffects = R->getValueAsBit("neverHasSideEffects");
305 isAsCheapAsAMove = R->getValueAsBit("isAsCheapAsAMove");
306 hasExtraSrcRegAllocReq = R->getValueAsBit("hasExtraSrcRegAllocReq");
307 hasExtraDefRegAllocReq = R->getValueAsBit("hasExtraDefRegAllocReq");
308 ImplicitDefs = R->getValueAsListOfDefs("Defs");
309 ImplicitUses = R->getValueAsListOfDefs("Uses");
310
311 if (neverHasSideEffects + hasSideEffects > 1)
312 throw R->getName() + ": multiple conflicting side-effect flags set!";
313
314 // Parse Constraints.
315 ParseConstraints(R->getValueAsString("Constraints"), Operands);
316
317 // Parse the DisableEncoding field.
318 Operands.ProcessDisableEncoding(R->getValueAsString("DisableEncoding"));
319}
Chris Lattner9414ae52010-03-27 20:09:24 +0000320
321/// HasOneImplicitDefWithKnownVT - If the instruction has at least one
322/// implicit def and it has a known VT, return the VT, otherwise return
323/// MVT::Other.
324MVT::SimpleValueType CodeGenInstruction::
325HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const {
326 if (ImplicitDefs.empty()) return MVT::Other;
327
328 // Check to see if the first implicit def has a resolvable type.
329 Record *FirstImplicitDef = ImplicitDefs[0];
330 assert(FirstImplicitDef->isSubClassOf("Register"));
331 const std::vector<MVT::SimpleValueType> &RegVTs =
332 TargetInfo.getRegisterVTs(FirstImplicitDef);
333 if (RegVTs.size() == 1)
334 return RegVTs[0];
335 return MVT::Other;
336}
337
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000338
339/// FlattenAsmStringVariants - Flatten the specified AsmString to only
340/// include text from the specified variant, returning the new string.
341std::string CodeGenInstruction::
342FlattenAsmStringVariants(StringRef Cur, unsigned Variant) {
343 std::string Res = "";
344
345 for (;;) {
346 // Find the start of the next variant string.
347 size_t VariantsStart = 0;
348 for (size_t e = Cur.size(); VariantsStart != e; ++VariantsStart)
349 if (Cur[VariantsStart] == '{' &&
350 (VariantsStart == 0 || (Cur[VariantsStart-1] != '$' &&
351 Cur[VariantsStart-1] != '\\')))
352 break;
353
354 // Add the prefix to the result.
355 Res += Cur.slice(0, VariantsStart);
356 if (VariantsStart == Cur.size())
357 break;
358
359 ++VariantsStart; // Skip the '{'.
360
361 // Scan to the end of the variants string.
362 size_t VariantsEnd = VariantsStart;
363 unsigned NestedBraces = 1;
364 for (size_t e = Cur.size(); VariantsEnd != e; ++VariantsEnd) {
365 if (Cur[VariantsEnd] == '}' && Cur[VariantsEnd-1] != '\\') {
366 if (--NestedBraces == 0)
367 break;
368 } else if (Cur[VariantsEnd] == '{')
369 ++NestedBraces;
370 }
371
372 // Select the Nth variant (or empty).
373 StringRef Selection = Cur.slice(VariantsStart, VariantsEnd);
374 for (unsigned i = 0; i != Variant; ++i)
375 Selection = Selection.split('|').second;
376 Res += Selection.split('|').first;
377
378 assert(VariantsEnd != Cur.size() &&
379 "Unterminated variants in assembly string!");
380 Cur = Cur.substr(VariantsEnd + 1);
381 }
382
383 return Res;
384}
385
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000386
387//===----------------------------------------------------------------------===//
388/// CodeGenInstAlias Implementation
389//===----------------------------------------------------------------------===//
390
Chris Lattner662e5a32010-11-06 07:14:44 +0000391CodeGenInstAlias::CodeGenInstAlias(Record *R, CodeGenTarget &T) : TheDef(R) {
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000392 AsmString = R->getValueAsString("AsmString");
Chris Lattnerb501d4f2010-11-01 05:34:34 +0000393 Result = R->getValueAsDag("ResultInst");
Chris Lattner225549f2010-11-06 06:39:47 +0000394
395 // Verify that the root of the result is an instruction.
396 DefInit *DI = dynamic_cast<DefInit*>(Result->getOperator());
397 if (DI == 0 || !DI->getDef()->isSubClassOf("Instruction"))
398 throw TGError(R->getLoc(), "result of inst alias should be an instruction");
399
400 ResultInst = &T.getInstruction(DI->getDef());
Chris Lattnerd0f225c2010-11-06 06:54:38 +0000401
Chris Lattner3f2c8e42010-11-06 07:06:09 +0000402 // NameClass - If argument names are repeated, we need to verify they have
403 // the same class.
404 StringMap<Record*> NameClass;
Chris Lattnerd0f225c2010-11-06 06:54:38 +0000405
406 // Decode and validate the arguments of the result.
Chris Lattner41409852010-11-06 07:31:43 +0000407 unsigned AliasOpNo = 0;
408 for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) {
409 // Tied registers don't have an entry in the result dag.
410 if (ResultInst->Operands[i].getTiedRegister() != -1)
411 continue;
412
413 if (AliasOpNo >= Result->getNumArgs())
414 throw TGError(R->getLoc(), "result has " + utostr(Result->getNumArgs()) +
415 " arguments, but " + ResultInst->TheDef->getName() +
416 " instruction expects " +
417 utostr(ResultInst->Operands.size()) + " operands!");
418
419
420 Init *Arg = Result->getArg(AliasOpNo);
Chris Lattner90fd7972010-11-06 19:57:21 +0000421 Record *ResultOpRec = ResultInst->Operands[i].Rec;
422
423 // Handle explicit registers.
424 if (DefInit *ADI = dynamic_cast<DefInit*>(Arg)) {
425 if (ADI->getDef()->isSubClassOf("Register")) {
426 if (!Result->getArgName(AliasOpNo).empty())
427 throw TGError(R->getLoc(), "result fixed register argument must "
428 "not have a name!");
429
430 if (!ResultOpRec->isSubClassOf("RegisterClass"))
431 throw TGError(R->getLoc(), "result fixed register argument is not "
432 "passed to a RegisterClass operand!");
433
434 if (!T.getRegisterClass(ResultOpRec).containsRegister(ADI->getDef()))
435 throw TGError(R->getLoc(), "fixed register " +ADI->getDef()->getName()
436 + " is not a member of the " + ResultOpRec->getName() +
437 " register class!");
438
439 // Now that it is validated, add it.
440 ResultOperands.push_back(ResultOperand(ADI->getDef()));
441 ++AliasOpNo;
442 continue;
443 }
444 }
Chris Lattnerd0f225c2010-11-06 06:54:38 +0000445
446 // If the operand is a record, it must have a name, and the record type must
447 // match up with the instruction's argument type.
448 if (DefInit *ADI = dynamic_cast<DefInit*>(Arg)) {
Chris Lattner41409852010-11-06 07:31:43 +0000449 if (Result->getArgName(AliasOpNo).empty())
450 throw TGError(R->getLoc(), "result argument #" + utostr(AliasOpNo) +
Chris Lattnerd0f225c2010-11-06 06:54:38 +0000451 " must have a name!");
452
Chris Lattner90fd7972010-11-06 19:57:21 +0000453 if (ADI->getDef() != ResultOpRec)
Chris Lattner41409852010-11-06 07:31:43 +0000454 throw TGError(R->getLoc(), "result argument #" + utostr(AliasOpNo) +
Chris Lattnerd0f225c2010-11-06 06:54:38 +0000455 " declared with class " + ADI->getDef()->getName() +
456 ", instruction operand is class " +
Chris Lattner90fd7972010-11-06 19:57:21 +0000457 ResultOpRec->getName());
Chris Lattnerd0f225c2010-11-06 06:54:38 +0000458
Chris Lattner3f2c8e42010-11-06 07:06:09 +0000459 // Verify we don't have something like: (someinst GR16:$foo, GR32:$foo)
460 // $foo can exist multiple times in the result list, but it must have the
461 // same type.
Chris Lattner41409852010-11-06 07:31:43 +0000462 Record *&Entry = NameClass[Result->getArgName(AliasOpNo)];
Chris Lattner3f2c8e42010-11-06 07:06:09 +0000463 if (Entry && Entry != ADI->getDef())
Chris Lattner41409852010-11-06 07:31:43 +0000464 throw TGError(R->getLoc(), "result value $" +
465 Result->getArgName(AliasOpNo) +
Chris Lattner3f2c8e42010-11-06 07:06:09 +0000466 " is both " + Entry->getName() + " and " +
467 ADI->getDef()->getName() + "!");
468
Chris Lattnerd0f225c2010-11-06 06:54:38 +0000469 // Now that it is validated, add it.
Chris Lattner41409852010-11-06 07:31:43 +0000470 ResultOperands.push_back(ResultOperand(Result->getArgName(AliasOpNo),
Chris Lattnerd0f225c2010-11-06 06:54:38 +0000471 ADI->getDef()));
Chris Lattner41409852010-11-06 07:31:43 +0000472 ++AliasOpNo;
Chris Lattnerd0f225c2010-11-06 06:54:38 +0000473 continue;
474 }
Chris Lattner98c870f2010-11-06 19:25:43 +0000475
476 if (IntInit *II = dynamic_cast<IntInit*>(Arg)) {
477 // Integer arguments can't have names.
478 if (!Result->getArgName(AliasOpNo).empty())
479 throw TGError(R->getLoc(), "result argument #" + utostr(AliasOpNo) +
480 " must not have a name!");
481 if (ResultInst->Operands[i].MINumOperands != 1 ||
Chris Lattner90fd7972010-11-06 19:57:21 +0000482 !ResultOpRec->isSubClassOf("Operand"))
Chris Lattner98c870f2010-11-06 19:25:43 +0000483 throw TGError(R->getLoc(), "invalid argument class " +
Chris Lattner90fd7972010-11-06 19:57:21 +0000484 ResultOpRec->getName() +
Chris Lattner98c870f2010-11-06 19:25:43 +0000485 " for integer result operand!");
486 ResultOperands.push_back(ResultOperand(II->getValue()));
487 ++AliasOpNo;
488 continue;
489 }
Chris Lattnerd0f225c2010-11-06 06:54:38 +0000490
491 throw TGError(R->getLoc(), "result of inst alias has unknown operand type");
492 }
Chris Lattner41409852010-11-06 07:31:43 +0000493
494 if (AliasOpNo != Result->getNumArgs())
495 throw TGError(R->getLoc(), "result has " + utostr(Result->getNumArgs()) +
496 " arguments, but " + ResultInst->TheDef->getName() +
497 " instruction expects " + utostr(ResultInst->Operands.size())+
498 " operands!");
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000499}
Chris Lattner5bde7342010-11-06 08:20:59 +0000500
501/// getResultInstOperandIndexForResultOperandIndex - Given an index into the
502/// ResultOperands array, translate it to a valid index in ResultInst's
503/// operand list.
504unsigned CodeGenInstAlias::
505getResultInstOperandIndexForResultOperandIndex(unsigned OpNo) const {
506 unsigned OpIdx = 0;
507
508 for (unsigned i = 0;; ++i) {
509 assert(i != ResultInst->Operands.size() && "Didn't find entry");
510 if (ResultInst->Operands[i].getTiedRegister() != -1)
511 continue;
512
513 if (OpIdx == OpNo) return i;
514
515 ++OpIdx;
516 }
517}