blob: 28b56ba1295531aaf4743c3d42b3bbcf99c86daa [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"
Jim Grosbach0b6a44a2011-06-21 22:55:50 +000016#include "Error.h"
Chris Lattner6cc654b2008-01-06 01:35:39 +000017#include "Record.h"
18#include "llvm/ADT/StringExtras.h"
Chris Lattner3f2c8e42010-11-06 07:06:09 +000019#include "llvm/ADT/StringMap.h"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000020#include "llvm/ADT/STLExtras.h"
Chris Lattner6cc654b2008-01-06 01:35:39 +000021#include <set>
22using namespace llvm;
23
Chris Lattnerc240bb02010-11-01 04:03:32 +000024//===----------------------------------------------------------------------===//
25// CGIOperandList Implementation
26//===----------------------------------------------------------------------===//
Jim Grosbach06801722009-12-16 19:43:02 +000027
Chris Lattnerc240bb02010-11-01 04:03:32 +000028CGIOperandList::CGIOperandList(Record *R) : TheDef(R) {
29 isPredicable = false;
Chris Lattner6cc654b2008-01-06 01:35:39 +000030 hasOptionalDef = false;
Chris Lattner8f707e12008-01-07 05:19:29 +000031 isVariadic = false;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000032
Chris Lattnerf55eed22010-03-18 21:07:39 +000033 DagInit *OutDI = R->getValueAsDag("OutOperandList");
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000034
Chris Lattnerf55eed22010-03-18 21:07:39 +000035 if (DefInit *Init = dynamic_cast<DefInit*>(OutDI->getOperator())) {
Chris Lattnerb0be4d22010-03-18 20:56:35 +000036 if (Init->getDef()->getName() != "outs")
Chris Lattnercedef1c2010-03-18 20:50:52 +000037 throw R->getName() + ": invalid def name for output list: use 'outs'";
38 } else
39 throw R->getName() + ": invalid output list: use 'outs'";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000040
Chris Lattnerf55eed22010-03-18 21:07:39 +000041 NumDefs = OutDI->getNumArgs();
Chris Lattnerc240bb02010-11-01 04:03:32 +000042
Chris Lattnerf55eed22010-03-18 21:07:39 +000043 DagInit *InDI = R->getValueAsDag("InOperandList");
44 if (DefInit *Init = dynamic_cast<DefInit*>(InDI->getOperator())) {
Chris Lattnerb0be4d22010-03-18 20:56:35 +000045 if (Init->getDef()->getName() != "ins")
Chris Lattnercedef1c2010-03-18 20:50:52 +000046 throw R->getName() + ": invalid def name for input list: use 'ins'";
47 } else
48 throw R->getName() + ": invalid input list: use 'ins'";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000049
Chris Lattner6cc654b2008-01-06 01:35:39 +000050 unsigned MIOperandNo = 0;
51 std::set<std::string> OperandNames;
Chris Lattnerf55eed22010-03-18 21:07:39 +000052 for (unsigned i = 0, e = InDI->getNumArgs()+OutDI->getNumArgs(); i != e; ++i){
53 Init *ArgInit;
54 std::string ArgName;
55 if (i < NumDefs) {
56 ArgInit = OutDI->getArg(i);
57 ArgName = OutDI->getArgName(i);
58 } else {
59 ArgInit = InDI->getArg(i-NumDefs);
60 ArgName = InDI->getArgName(i-NumDefs);
61 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000062
Chris Lattnerf55eed22010-03-18 21:07:39 +000063 DefInit *Arg = dynamic_cast<DefInit*>(ArgInit);
Chris Lattner6cc654b2008-01-06 01:35:39 +000064 if (!Arg)
65 throw "Illegal operand for the '" + R->getName() + "' instruction!";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000066
Chris Lattner6cc654b2008-01-06 01:35:39 +000067 Record *Rec = Arg->getDef();
68 std::string PrintMethod = "printOperand";
Jim Grosbach5013f742010-10-12 22:21:57 +000069 std::string EncoderMethod;
Chris Lattner6cc654b2008-01-06 01:35:39 +000070 unsigned NumOps = 1;
71 DagInit *MIOpInfo = 0;
Owen Andersonbea6f612011-06-27 21:06:21 +000072 if (Rec->isSubClassOf("RegisterOperand")) {
73 PrintMethod = Rec->getValueAsString("PrintMethod");
74 } else if (Rec->isSubClassOf("Operand")) {
Chris Lattner6cc654b2008-01-06 01:35:39 +000075 PrintMethod = Rec->getValueAsString("PrintMethod");
Jim Grosbach5013f742010-10-12 22:21:57 +000076 // If there is an explicit encoder method, use it.
Chris Lattner2ac19022010-11-15 05:19:05 +000077 EncoderMethod = Rec->getValueAsString("EncoderMethod");
Chris Lattner6cc654b2008-01-06 01:35:39 +000078 MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000079
Chris Lattner6cc654b2008-01-06 01:35:39 +000080 // Verify that MIOpInfo has an 'ops' root value.
81 if (!dynamic_cast<DefInit*>(MIOpInfo->getOperator()) ||
82 dynamic_cast<DefInit*>(MIOpInfo->getOperator())
Chris Lattnerc240bb02010-11-01 04:03:32 +000083 ->getDef()->getName() != "ops")
Chris Lattner6cc654b2008-01-06 01:35:39 +000084 throw "Bad value for MIOperandInfo in operand '" + Rec->getName() +
Chris Lattnerc240bb02010-11-01 04:03:32 +000085 "'\n";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000086
Chris Lattner6cc654b2008-01-06 01:35:39 +000087 // If we have MIOpInfo, then we have #operands equal to number of entries
88 // in MIOperandInfo.
89 if (unsigned NumArgs = MIOpInfo->getNumArgs())
90 NumOps = NumArgs;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000091
Chris Lattner6cc654b2008-01-06 01:35:39 +000092 if (Rec->isSubClassOf("PredicateOperand"))
93 isPredicable = true;
94 else if (Rec->isSubClassOf("OptionalDefOperand"))
95 hasOptionalDef = true;
96 } else if (Rec->getName() == "variable_ops") {
Chris Lattner8f707e12008-01-07 05:19:29 +000097 isVariadic = true;
Chris Lattner6cc654b2008-01-06 01:35:39 +000098 continue;
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000099 } else if (!Rec->isSubClassOf("RegisterClass") &&
NAKAMURA Takumi36c3bc42011-01-26 02:03:48 +0000100 !Rec->isSubClassOf("PointerLikeRegClass") &&
101 Rec->getName() != "unknown")
Chris Lattner6cc654b2008-01-06 01:35:39 +0000102 throw "Unknown operand class '" + Rec->getName() +
Chris Lattnerc240bb02010-11-01 04:03:32 +0000103 "' in '" + R->getName() + "' instruction!";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000104
Chris Lattner6cc654b2008-01-06 01:35:39 +0000105 // Check that the operand has a name and that it's unique.
Chris Lattnerf55eed22010-03-18 21:07:39 +0000106 if (ArgName.empty())
Chris Lattner6cc654b2008-01-06 01:35:39 +0000107 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
Chris Lattnerc240bb02010-11-01 04:03:32 +0000108 " has no name!";
Chris Lattnerf55eed22010-03-18 21:07:39 +0000109 if (!OperandNames.insert(ArgName).second)
Chris Lattner6cc654b2008-01-06 01:35:39 +0000110 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
Chris Lattnerc240bb02010-11-01 04:03:32 +0000111 " has the same name as a previous operand!";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000112
Jim Grosbach5013f742010-10-12 22:21:57 +0000113 OperandList.push_back(OperandInfo(Rec, ArgName, PrintMethod, EncoderMethod,
Chris Lattner6cc654b2008-01-06 01:35:39 +0000114 MIOperandNo, NumOps, MIOpInfo));
115 MIOperandNo += NumOps;
116 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000117
118
Chris Lattnerb501d4f2010-11-01 05:34:34 +0000119 // Make sure the constraints list for each operand is large enough to hold
120 // constraint info, even if none is present.
121 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
122 OperandList[i].Constraints.resize(OperandList[i].MINumOperands);
Chris Lattner6cc654b2008-01-06 01:35:39 +0000123}
124
Chris Lattnerc240bb02010-11-01 04:03:32 +0000125
Chris Lattner6cc654b2008-01-06 01:35:39 +0000126/// getOperandNamed - Return the index of the operand with the specified
127/// non-empty name. If the instruction does not have an operand with the
128/// specified name, throw an exception.
129///
Chris Lattnerc240bb02010-11-01 04:03:32 +0000130unsigned CGIOperandList::getOperandNamed(StringRef Name) const {
Jim Grosbach01855072010-10-11 18:25:51 +0000131 unsigned OpIdx;
132 if (hasOperandNamed(Name, OpIdx)) return OpIdx;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000133 throw "'" + TheDef->getName() + "' does not have an operand named '$" +
Chris Lattnerc240bb02010-11-01 04:03:32 +0000134 Name.str() + "'!";
Chris Lattner6cc654b2008-01-06 01:35:39 +0000135}
136
Jim Grosbach01855072010-10-11 18:25:51 +0000137/// hasOperandNamed - Query whether the instruction has an operand of the
138/// given name. If so, return true and set OpIdx to the index of the
139/// operand. Otherwise, return false.
Chris Lattnerc240bb02010-11-01 04:03:32 +0000140bool CGIOperandList::hasOperandNamed(StringRef Name, unsigned &OpIdx) const {
Jim Grosbach01855072010-10-11 18:25:51 +0000141 assert(!Name.empty() && "Cannot search for operand with no name!");
142 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
143 if (OperandList[i].Name == Name) {
144 OpIdx = i;
145 return true;
146 }
147 return false;
148}
149
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000150std::pair<unsigned,unsigned>
Chris Lattnerc240bb02010-11-01 04:03:32 +0000151CGIOperandList::ParseOperandName(const std::string &Op, bool AllowWholeOp) {
Chris Lattner6cc654b2008-01-06 01:35:39 +0000152 if (Op.empty() || Op[0] != '$')
153 throw TheDef->getName() + ": Illegal operand name: '" + Op + "'";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000154
Chris Lattner6cc654b2008-01-06 01:35:39 +0000155 std::string OpName = Op.substr(1);
156 std::string SubOpName;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000157
Chris Lattner6cc654b2008-01-06 01:35:39 +0000158 // Check to see if this is $foo.bar.
159 std::string::size_type DotIdx = OpName.find_first_of(".");
160 if (DotIdx != std::string::npos) {
161 SubOpName = OpName.substr(DotIdx+1);
162 if (SubOpName.empty())
163 throw TheDef->getName() + ": illegal empty suboperand name in '" +Op +"'";
164 OpName = OpName.substr(0, DotIdx);
165 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000166
Chris Lattner6cc654b2008-01-06 01:35:39 +0000167 unsigned OpIdx = getOperandNamed(OpName);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000168
Chris Lattner6cc654b2008-01-06 01:35:39 +0000169 if (SubOpName.empty()) { // If no suboperand name was specified:
170 // If one was needed, throw.
171 if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp &&
172 SubOpName.empty())
173 throw TheDef->getName() + ": Illegal to refer to"
Chris Lattnerc240bb02010-11-01 04:03:32 +0000174 " whole operand part of complex operand '" + Op + "'";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000175
Chris Lattner6cc654b2008-01-06 01:35:39 +0000176 // Otherwise, return the operand.
177 return std::make_pair(OpIdx, 0U);
178 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000179
Chris Lattner6cc654b2008-01-06 01:35:39 +0000180 // Find the suboperand number involved.
181 DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
182 if (MIOpInfo == 0)
183 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000184
Chris Lattner6cc654b2008-01-06 01:35:39 +0000185 // Find the operand with the right name.
186 for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i)
187 if (MIOpInfo->getArgName(i) == SubOpName)
188 return std::make_pair(OpIdx, i);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000189
Chris Lattner6cc654b2008-01-06 01:35:39 +0000190 // Otherwise, didn't find it!
191 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
192}
Chris Lattner9414ae52010-03-27 20:09:24 +0000193
Chris Lattnerc240bb02010-11-01 04:03:32 +0000194static void ParseConstraint(const std::string &CStr, CGIOperandList &Ops) {
195 // EARLY_CLOBBER: @early $reg
196 std::string::size_type wpos = CStr.find_first_of(" \t");
197 std::string::size_type start = CStr.find_first_not_of(" \t");
198 std::string Tok = CStr.substr(start, wpos - start);
199 if (Tok == "@earlyclobber") {
200 std::string Name = CStr.substr(wpos+1);
201 wpos = Name.find_first_not_of(" \t");
202 if (wpos == std::string::npos)
203 throw "Illegal format for @earlyclobber constraint: '" + CStr + "'";
204 Name = Name.substr(wpos);
205 std::pair<unsigned,unsigned> Op = Ops.ParseOperandName(Name, false);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000206
Chris Lattnerc240bb02010-11-01 04:03:32 +0000207 // Build the string for the operand
208 if (!Ops[Op.first].Constraints[Op.second].isNone())
209 throw "Operand '" + Name + "' cannot have multiple constraints!";
210 Ops[Op.first].Constraints[Op.second] =
211 CGIOperandList::ConstraintInfo::getEarlyClobber();
212 return;
213 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000214
Chris Lattnerc240bb02010-11-01 04:03:32 +0000215 // Only other constraint is "TIED_TO" for now.
216 std::string::size_type pos = CStr.find_first_of('=');
217 assert(pos != std::string::npos && "Unrecognized constraint");
218 start = CStr.find_first_not_of(" \t");
219 std::string Name = CStr.substr(start, pos - start);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000220
Chris Lattnerc240bb02010-11-01 04:03:32 +0000221 // TIED_TO: $src1 = $dst
222 wpos = Name.find_first_of(" \t");
223 if (wpos == std::string::npos)
224 throw "Illegal format for tied-to constraint: '" + CStr + "'";
225 std::string DestOpName = Name.substr(0, wpos);
226 std::pair<unsigned,unsigned> DestOp = Ops.ParseOperandName(DestOpName, false);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000227
Chris Lattnerc240bb02010-11-01 04:03:32 +0000228 Name = CStr.substr(pos+1);
229 wpos = Name.find_first_not_of(" \t");
230 if (wpos == std::string::npos)
231 throw "Illegal format for tied-to constraint: '" + CStr + "'";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000232
Chris Lattnerc240bb02010-11-01 04:03:32 +0000233 std::pair<unsigned,unsigned> SrcOp =
234 Ops.ParseOperandName(Name.substr(wpos), false);
235 if (SrcOp > DestOp)
236 throw "Illegal tied-to operand constraint '" + CStr + "'";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000237
238
Chris Lattnerc240bb02010-11-01 04:03:32 +0000239 unsigned FlatOpNo = Ops.getFlattenedOperandNumber(SrcOp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000240
Chris Lattnerc240bb02010-11-01 04:03:32 +0000241 if (!Ops[DestOp.first].Constraints[DestOp.second].isNone())
242 throw "Operand '" + DestOpName + "' cannot have multiple constraints!";
243 Ops[DestOp.first].Constraints[DestOp.second] =
244 CGIOperandList::ConstraintInfo::getTied(FlatOpNo);
245}
246
247static void ParseConstraints(const std::string &CStr, CGIOperandList &Ops) {
Chris Lattnerc240bb02010-11-01 04:03:32 +0000248 if (CStr.empty()) return;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000249
Chris Lattnerc240bb02010-11-01 04:03:32 +0000250 const std::string delims(",");
251 std::string::size_type bidx, eidx;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000252
Chris Lattnerc240bb02010-11-01 04:03:32 +0000253 bidx = CStr.find_first_not_of(delims);
254 while (bidx != std::string::npos) {
255 eidx = CStr.find_first_of(delims, bidx);
256 if (eidx == std::string::npos)
257 eidx = CStr.length();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000258
Chris Lattnerc240bb02010-11-01 04:03:32 +0000259 ParseConstraint(CStr.substr(bidx, eidx - bidx), Ops);
260 bidx = CStr.find_first_not_of(delims, eidx);
261 }
262}
263
264void CGIOperandList::ProcessDisableEncoding(std::string DisableEncoding) {
265 while (1) {
266 std::string OpName;
267 tie(OpName, DisableEncoding) = getToken(DisableEncoding, " ,\t");
268 if (OpName.empty()) break;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000269
Chris Lattnerc240bb02010-11-01 04:03:32 +0000270 // Figure out which operand this is.
271 std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000272
Chris Lattnerc240bb02010-11-01 04:03:32 +0000273 // Mark the operand as not-to-be encoded.
274 if (Op.second >= OperandList[Op.first].DoNotEncode.size())
275 OperandList[Op.first].DoNotEncode.resize(Op.second+1);
276 OperandList[Op.first].DoNotEncode[Op.second] = true;
277 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000278
Chris Lattnerc240bb02010-11-01 04:03:32 +0000279}
280
281//===----------------------------------------------------------------------===//
282// CodeGenInstruction Implementation
283//===----------------------------------------------------------------------===//
284
285CodeGenInstruction::CodeGenInstruction(Record *R) : TheDef(R), Operands(R) {
286 Namespace = R->getValueAsString("Namespace");
287 AsmString = R->getValueAsString("AsmString");
288
289 isReturn = R->getValueAsBit("isReturn");
290 isBranch = R->getValueAsBit("isBranch");
291 isIndirectBranch = R->getValueAsBit("isIndirectBranch");
292 isCompare = R->getValueAsBit("isCompare");
Evan Chengc4af4632010-11-17 20:13:28 +0000293 isMoveImm = R->getValueAsBit("isMoveImm");
Evan Cheng0f040a22011-03-15 05:09:26 +0000294 isBitcast = R->getValueAsBit("isBitcast");
Chris Lattnerc240bb02010-11-01 04:03:32 +0000295 isBarrier = R->getValueAsBit("isBarrier");
296 isCall = R->getValueAsBit("isCall");
297 canFoldAsLoad = R->getValueAsBit("canFoldAsLoad");
298 mayLoad = R->getValueAsBit("mayLoad");
299 mayStore = R->getValueAsBit("mayStore");
300 isPredicable = Operands.isPredicable || R->getValueAsBit("isPredicable");
301 isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
302 isCommutable = R->getValueAsBit("isCommutable");
303 isTerminator = R->getValueAsBit("isTerminator");
304 isReMaterializable = R->getValueAsBit("isReMaterializable");
305 hasDelaySlot = R->getValueAsBit("hasDelaySlot");
306 usesCustomInserter = R->getValueAsBit("usesCustomInserter");
307 hasCtrlDep = R->getValueAsBit("hasCtrlDep");
308 isNotDuplicable = R->getValueAsBit("isNotDuplicable");
309 hasSideEffects = R->getValueAsBit("hasSideEffects");
310 neverHasSideEffects = R->getValueAsBit("neverHasSideEffects");
311 isAsCheapAsAMove = R->getValueAsBit("isAsCheapAsAMove");
312 hasExtraSrcRegAllocReq = R->getValueAsBit("hasExtraSrcRegAllocReq");
313 hasExtraDefRegAllocReq = R->getValueAsBit("hasExtraDefRegAllocReq");
Jim Grosbach806fcc02011-07-06 21:33:38 +0000314 isPseudo = R->getValueAsBit("isPseudo");
Chris Lattnerc240bb02010-11-01 04:03:32 +0000315 ImplicitDefs = R->getValueAsListOfDefs("Defs");
316 ImplicitUses = R->getValueAsListOfDefs("Uses");
317
318 if (neverHasSideEffects + hasSideEffects > 1)
319 throw R->getName() + ": multiple conflicting side-effect flags set!";
320
321 // Parse Constraints.
322 ParseConstraints(R->getValueAsString("Constraints"), Operands);
323
324 // Parse the DisableEncoding field.
325 Operands.ProcessDisableEncoding(R->getValueAsString("DisableEncoding"));
326}
Chris Lattner9414ae52010-03-27 20:09:24 +0000327
328/// HasOneImplicitDefWithKnownVT - If the instruction has at least one
329/// implicit def and it has a known VT, return the VT, otherwise return
330/// MVT::Other.
331MVT::SimpleValueType CodeGenInstruction::
332HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const {
333 if (ImplicitDefs.empty()) return MVT::Other;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000334
Chris Lattner9414ae52010-03-27 20:09:24 +0000335 // Check to see if the first implicit def has a resolvable type.
336 Record *FirstImplicitDef = ImplicitDefs[0];
337 assert(FirstImplicitDef->isSubClassOf("Register"));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000338 const std::vector<MVT::SimpleValueType> &RegVTs =
Chris Lattner9414ae52010-03-27 20:09:24 +0000339 TargetInfo.getRegisterVTs(FirstImplicitDef);
340 if (RegVTs.size() == 1)
341 return RegVTs[0];
342 return MVT::Other;
343}
344
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000345
346/// FlattenAsmStringVariants - Flatten the specified AsmString to only
347/// include text from the specified variant, returning the new string.
348std::string CodeGenInstruction::
349FlattenAsmStringVariants(StringRef Cur, unsigned Variant) {
350 std::string Res = "";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000351
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000352 for (;;) {
353 // Find the start of the next variant string.
354 size_t VariantsStart = 0;
355 for (size_t e = Cur.size(); VariantsStart != e; ++VariantsStart)
356 if (Cur[VariantsStart] == '{' &&
357 (VariantsStart == 0 || (Cur[VariantsStart-1] != '$' &&
358 Cur[VariantsStart-1] != '\\')))
359 break;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000360
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000361 // Add the prefix to the result.
362 Res += Cur.slice(0, VariantsStart);
363 if (VariantsStart == Cur.size())
364 break;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000365
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000366 ++VariantsStart; // Skip the '{'.
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000367
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000368 // Scan to the end of the variants string.
369 size_t VariantsEnd = VariantsStart;
370 unsigned NestedBraces = 1;
371 for (size_t e = Cur.size(); VariantsEnd != e; ++VariantsEnd) {
372 if (Cur[VariantsEnd] == '}' && Cur[VariantsEnd-1] != '\\') {
373 if (--NestedBraces == 0)
374 break;
375 } else if (Cur[VariantsEnd] == '{')
376 ++NestedBraces;
377 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000378
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000379 // Select the Nth variant (or empty).
380 StringRef Selection = Cur.slice(VariantsStart, VariantsEnd);
381 for (unsigned i = 0; i != Variant; ++i)
382 Selection = Selection.split('|').second;
383 Res += Selection.split('|').first;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000384
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000385 assert(VariantsEnd != Cur.size() &&
386 "Unterminated variants in assembly string!");
387 Cur = Cur.substr(VariantsEnd + 1);
388 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000389
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000390 return Res;
391}
392
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000393
394//===----------------------------------------------------------------------===//
395/// CodeGenInstAlias Implementation
396//===----------------------------------------------------------------------===//
397
Bob Wilsona49c7df2011-01-26 19:44:55 +0000398/// tryAliasOpMatch - This is a helper function for the CodeGenInstAlias
399/// constructor. It checks if an argument in an InstAlias pattern matches
400/// the corresponding operand of the instruction. It returns true on a
401/// successful match, with ResOp set to the result operand to be used.
402bool CodeGenInstAlias::tryAliasOpMatch(DagInit *Result, unsigned AliasOpNo,
403 Record *InstOpRec, bool hasSubOps,
404 SMLoc Loc, CodeGenTarget &T,
405 ResultOperand &ResOp) {
406 Init *Arg = Result->getArg(AliasOpNo);
407 DefInit *ADI = dynamic_cast<DefInit*>(Arg);
408
409 if (ADI && ADI->getDef() == InstOpRec) {
410 // If the operand is a record, it must have a name, and the record type
411 // must match up with the instruction's argument type.
412 if (Result->getArgName(AliasOpNo).empty())
413 throw TGError(Loc, "result argument #" + utostr(AliasOpNo) +
414 " must have a name!");
415 ResOp = ResultOperand(Result->getArgName(AliasOpNo), ADI->getDef());
416 return true;
417 }
418
419 // Handle explicit registers.
420 if (ADI && ADI->getDef()->isSubClassOf("Register")) {
Owen Andersonbea6f612011-06-27 21:06:21 +0000421 if (InstOpRec->isSubClassOf("RegisterOperand"))
422 InstOpRec = InstOpRec->getValueAsDef("RegClass");
423
Bob Wilsona49c7df2011-01-26 19:44:55 +0000424 if (!InstOpRec->isSubClassOf("RegisterClass"))
425 return false;
426
Jakob Stoklund Olesenae1920b2011-06-15 04:50:36 +0000427 if (!T.getRegisterClass(InstOpRec)
428 .contains(T.getRegBank().getReg(ADI->getDef())))
Bob Wilsona49c7df2011-01-26 19:44:55 +0000429 throw TGError(Loc, "fixed register " +ADI->getDef()->getName()
430 + " is not a member of the " + InstOpRec->getName() +
431 " register class!");
432
433 if (!Result->getArgName(AliasOpNo).empty())
434 throw TGError(Loc, "result fixed register argument must "
435 "not have a name!");
436
437 ResOp = ResultOperand(ADI->getDef());
438 return true;
439 }
440
441 // Handle "zero_reg" for optional def operands.
442 if (ADI && ADI->getDef()->getName() == "zero_reg") {
443
444 // Check if this is an optional def.
445 if (!InstOpRec->isSubClassOf("OptionalDefOperand"))
446 throw TGError(Loc, "reg0 used for result that is not an "
447 "OptionalDefOperand!");
448
449 ResOp = ResultOperand(static_cast<Record*>(0));
450 return true;
451 }
452
453 if (IntInit *II = dynamic_cast<IntInit*>(Arg)) {
454 if (hasSubOps || !InstOpRec->isSubClassOf("Operand"))
455 return false;
456 // Integer arguments can't have names.
457 if (!Result->getArgName(AliasOpNo).empty())
458 throw TGError(Loc, "result argument #" + utostr(AliasOpNo) +
459 " must not have a name!");
460 ResOp = ResultOperand(II->getValue());
461 return true;
462 }
463
464 return false;
465}
466
Chris Lattner662e5a32010-11-06 07:14:44 +0000467CodeGenInstAlias::CodeGenInstAlias(Record *R, CodeGenTarget &T) : TheDef(R) {
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000468 AsmString = R->getValueAsString("AsmString");
Chris Lattnerb501d4f2010-11-01 05:34:34 +0000469 Result = R->getValueAsDag("ResultInst");
Chris Lattner225549f2010-11-06 06:39:47 +0000470
471 // Verify that the root of the result is an instruction.
472 DefInit *DI = dynamic_cast<DefInit*>(Result->getOperator());
473 if (DI == 0 || !DI->getDef()->isSubClassOf("Instruction"))
474 throw TGError(R->getLoc(), "result of inst alias should be an instruction");
475
476 ResultInst = &T.getInstruction(DI->getDef());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000477
Chris Lattner3f2c8e42010-11-06 07:06:09 +0000478 // NameClass - If argument names are repeated, we need to verify they have
479 // the same class.
480 StringMap<Record*> NameClass;
Bob Wilson55931ab2011-01-20 18:38:10 +0000481 for (unsigned i = 0, e = Result->getNumArgs(); i != e; ++i) {
482 DefInit *ADI = dynamic_cast<DefInit*>(Result->getArg(i));
483 if (!ADI || Result->getArgName(i).empty())
484 continue;
485 // Verify we don't have something like: (someinst GR16:$foo, GR32:$foo)
486 // $foo can exist multiple times in the result list, but it must have the
487 // same type.
488 Record *&Entry = NameClass[Result->getArgName(i)];
489 if (Entry && Entry != ADI->getDef())
490 throw TGError(R->getLoc(), "result value $" + Result->getArgName(i) +
491 " is both " + Entry->getName() + " and " +
492 ADI->getDef()->getName() + "!");
493 Entry = ADI->getDef();
494 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000495
Chris Lattnerd0f225c2010-11-06 06:54:38 +0000496 // Decode and validate the arguments of the result.
Chris Lattner41409852010-11-06 07:31:43 +0000497 unsigned AliasOpNo = 0;
498 for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) {
Bob Wilsona49c7df2011-01-26 19:44:55 +0000499
Chris Lattner41409852010-11-06 07:31:43 +0000500 // Tied registers don't have an entry in the result dag.
501 if (ResultInst->Operands[i].getTiedRegister() != -1)
502 continue;
503
504 if (AliasOpNo >= Result->getNumArgs())
Bob Wilsona49c7df2011-01-26 19:44:55 +0000505 throw TGError(R->getLoc(), "not enough arguments for instruction!");
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000506
Bob Wilsona49c7df2011-01-26 19:44:55 +0000507 Record *InstOpRec = ResultInst->Operands[i].Rec;
508 unsigned NumSubOps = ResultInst->Operands[i].MINumOperands;
509 ResultOperand ResOp(static_cast<int64_t>(0));
510 if (tryAliasOpMatch(Result, AliasOpNo, InstOpRec, (NumSubOps > 1),
511 R->getLoc(), T, ResOp)) {
512 ResultOperands.push_back(ResOp);
513 ResultInstOperandIndex.push_back(std::make_pair(i, -1));
Chris Lattner41409852010-11-06 07:31:43 +0000514 ++AliasOpNo;
Chris Lattnerd0f225c2010-11-06 06:54:38 +0000515 continue;
516 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000517
Bob Wilsona49c7df2011-01-26 19:44:55 +0000518 // If the argument did not match the instruction operand, and the operand
519 // is composed of multiple suboperands, try matching the suboperands.
520 if (NumSubOps > 1) {
521 DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo;
522 for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) {
523 if (AliasOpNo >= Result->getNumArgs())
524 throw TGError(R->getLoc(), "not enough arguments for instruction!");
525 Record *SubRec = dynamic_cast<DefInit*>(MIOI->getArg(SubOp))->getDef();
526 if (tryAliasOpMatch(Result, AliasOpNo, SubRec, false,
527 R->getLoc(), T, ResOp)) {
528 ResultOperands.push_back(ResOp);
529 ResultInstOperandIndex.push_back(std::make_pair(i, SubOp));
530 ++AliasOpNo;
531 } else {
532 throw TGError(R->getLoc(), "result argument #" + utostr(AliasOpNo) +
533 " does not match instruction operand class " +
534 (SubOp == 0 ? InstOpRec->getName() :SubRec->getName()));
535 }
536 }
Chris Lattner98c870f2010-11-06 19:25:43 +0000537 continue;
538 }
Bob Wilsona49c7df2011-01-26 19:44:55 +0000539 throw TGError(R->getLoc(), "result argument #" + utostr(AliasOpNo) +
540 " does not match instruction operand class " +
541 InstOpRec->getName());
Chris Lattnerd0f225c2010-11-06 06:54:38 +0000542 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000543
Chris Lattner41409852010-11-06 07:31:43 +0000544 if (AliasOpNo != Result->getNumArgs())
Bob Wilsona49c7df2011-01-26 19:44:55 +0000545 throw TGError(R->getLoc(), "too many operands for instruction!");
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000546}