blob: e9557d2b4bcb61378dcb6322524a099fd0c3cc75 [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;
72 if (Rec->isSubClassOf("Operand")) {
73 PrintMethod = Rec->getValueAsString("PrintMethod");
Jim Grosbach5013f742010-10-12 22:21:57 +000074 // If there is an explicit encoder method, use it.
Chris Lattner2ac19022010-11-15 05:19:05 +000075 EncoderMethod = Rec->getValueAsString("EncoderMethod");
Chris Lattner6cc654b2008-01-06 01:35:39 +000076 MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000077
Chris Lattner6cc654b2008-01-06 01:35:39 +000078 // Verify that MIOpInfo has an 'ops' root value.
79 if (!dynamic_cast<DefInit*>(MIOpInfo->getOperator()) ||
80 dynamic_cast<DefInit*>(MIOpInfo->getOperator())
Chris Lattnerc240bb02010-11-01 04:03:32 +000081 ->getDef()->getName() != "ops")
Chris Lattner6cc654b2008-01-06 01:35:39 +000082 throw "Bad value for MIOperandInfo in operand '" + Rec->getName() +
Chris Lattnerc240bb02010-11-01 04:03:32 +000083 "'\n";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000084
Chris Lattner6cc654b2008-01-06 01:35:39 +000085 // If we have MIOpInfo, then we have #operands equal to number of entries
86 // in MIOperandInfo.
87 if (unsigned NumArgs = MIOpInfo->getNumArgs())
88 NumOps = NumArgs;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000089
Chris Lattner6cc654b2008-01-06 01:35:39 +000090 if (Rec->isSubClassOf("PredicateOperand"))
91 isPredicable = true;
92 else if (Rec->isSubClassOf("OptionalDefOperand"))
93 hasOptionalDef = true;
94 } else if (Rec->getName() == "variable_ops") {
Chris Lattner8f707e12008-01-07 05:19:29 +000095 isVariadic = true;
Chris Lattner6cc654b2008-01-06 01:35:39 +000096 continue;
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000097 } else if (!Rec->isSubClassOf("RegisterClass") &&
NAKAMURA Takumi36c3bc42011-01-26 02:03:48 +000098 !Rec->isSubClassOf("PointerLikeRegClass") &&
99 Rec->getName() != "unknown")
Chris Lattner6cc654b2008-01-06 01:35:39 +0000100 throw "Unknown operand class '" + Rec->getName() +
Chris Lattnerc240bb02010-11-01 04:03:32 +0000101 "' in '" + R->getName() + "' instruction!";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000102
Chris Lattner6cc654b2008-01-06 01:35:39 +0000103 // Check that the operand has a name and that it's unique.
Chris Lattnerf55eed22010-03-18 21:07:39 +0000104 if (ArgName.empty())
Chris Lattner6cc654b2008-01-06 01:35:39 +0000105 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
Chris Lattnerc240bb02010-11-01 04:03:32 +0000106 " has no name!";
Chris Lattnerf55eed22010-03-18 21:07:39 +0000107 if (!OperandNames.insert(ArgName).second)
Chris Lattner6cc654b2008-01-06 01:35:39 +0000108 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
Chris Lattnerc240bb02010-11-01 04:03:32 +0000109 " has the same name as a previous operand!";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000110
Jim Grosbach5013f742010-10-12 22:21:57 +0000111 OperandList.push_back(OperandInfo(Rec, ArgName, PrintMethod, EncoderMethod,
Chris Lattner6cc654b2008-01-06 01:35:39 +0000112 MIOperandNo, NumOps, MIOpInfo));
113 MIOperandNo += NumOps;
114 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000115
116
Chris Lattnerb501d4f2010-11-01 05:34:34 +0000117 // Make sure the constraints list for each operand is large enough to hold
118 // constraint info, even if none is present.
119 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
120 OperandList[i].Constraints.resize(OperandList[i].MINumOperands);
Chris Lattner6cc654b2008-01-06 01:35:39 +0000121}
122
Chris Lattnerc240bb02010-11-01 04:03:32 +0000123
Chris Lattner6cc654b2008-01-06 01:35:39 +0000124/// getOperandNamed - Return the index of the operand with the specified
125/// non-empty name. If the instruction does not have an operand with the
126/// specified name, throw an exception.
127///
Chris Lattnerc240bb02010-11-01 04:03:32 +0000128unsigned CGIOperandList::getOperandNamed(StringRef Name) const {
Jim Grosbach01855072010-10-11 18:25:51 +0000129 unsigned OpIdx;
130 if (hasOperandNamed(Name, OpIdx)) return OpIdx;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000131 throw "'" + TheDef->getName() + "' does not have an operand named '$" +
Chris Lattnerc240bb02010-11-01 04:03:32 +0000132 Name.str() + "'!";
Chris Lattner6cc654b2008-01-06 01:35:39 +0000133}
134
Jim Grosbach01855072010-10-11 18:25:51 +0000135/// hasOperandNamed - Query whether the instruction has an operand of the
136/// given name. If so, return true and set OpIdx to the index of the
137/// operand. Otherwise, return false.
Chris Lattnerc240bb02010-11-01 04:03:32 +0000138bool CGIOperandList::hasOperandNamed(StringRef Name, unsigned &OpIdx) const {
Jim Grosbach01855072010-10-11 18:25:51 +0000139 assert(!Name.empty() && "Cannot search for operand with no name!");
140 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
141 if (OperandList[i].Name == Name) {
142 OpIdx = i;
143 return true;
144 }
145 return false;
146}
147
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000148std::pair<unsigned,unsigned>
Chris Lattnerc240bb02010-11-01 04:03:32 +0000149CGIOperandList::ParseOperandName(const std::string &Op, bool AllowWholeOp) {
Chris Lattner6cc654b2008-01-06 01:35:39 +0000150 if (Op.empty() || Op[0] != '$')
151 throw TheDef->getName() + ": Illegal operand name: '" + Op + "'";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000152
Chris Lattner6cc654b2008-01-06 01:35:39 +0000153 std::string OpName = Op.substr(1);
154 std::string SubOpName;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000155
Chris Lattner6cc654b2008-01-06 01:35:39 +0000156 // Check to see if this is $foo.bar.
157 std::string::size_type DotIdx = OpName.find_first_of(".");
158 if (DotIdx != std::string::npos) {
159 SubOpName = OpName.substr(DotIdx+1);
160 if (SubOpName.empty())
161 throw TheDef->getName() + ": illegal empty suboperand name in '" +Op +"'";
162 OpName = OpName.substr(0, DotIdx);
163 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000164
Chris Lattner6cc654b2008-01-06 01:35:39 +0000165 unsigned OpIdx = getOperandNamed(OpName);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000166
Chris Lattner6cc654b2008-01-06 01:35:39 +0000167 if (SubOpName.empty()) { // If no suboperand name was specified:
168 // If one was needed, throw.
169 if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp &&
170 SubOpName.empty())
171 throw TheDef->getName() + ": Illegal to refer to"
Chris Lattnerc240bb02010-11-01 04:03:32 +0000172 " whole operand part of complex operand '" + Op + "'";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000173
Chris Lattner6cc654b2008-01-06 01:35:39 +0000174 // Otherwise, return the operand.
175 return std::make_pair(OpIdx, 0U);
176 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000177
Chris Lattner6cc654b2008-01-06 01:35:39 +0000178 // Find the suboperand number involved.
179 DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
180 if (MIOpInfo == 0)
181 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000182
Chris Lattner6cc654b2008-01-06 01:35:39 +0000183 // Find the operand with the right name.
184 for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i)
185 if (MIOpInfo->getArgName(i) == SubOpName)
186 return std::make_pair(OpIdx, i);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000187
Chris Lattner6cc654b2008-01-06 01:35:39 +0000188 // Otherwise, didn't find it!
189 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
190}
Chris Lattner9414ae52010-03-27 20:09:24 +0000191
Chris Lattnerc240bb02010-11-01 04:03:32 +0000192static void ParseConstraint(const std::string &CStr, CGIOperandList &Ops) {
193 // EARLY_CLOBBER: @early $reg
194 std::string::size_type wpos = CStr.find_first_of(" \t");
195 std::string::size_type start = CStr.find_first_not_of(" \t");
196 std::string Tok = CStr.substr(start, wpos - start);
197 if (Tok == "@earlyclobber") {
198 std::string Name = CStr.substr(wpos+1);
199 wpos = Name.find_first_not_of(" \t");
200 if (wpos == std::string::npos)
201 throw "Illegal format for @earlyclobber constraint: '" + CStr + "'";
202 Name = Name.substr(wpos);
203 std::pair<unsigned,unsigned> Op = Ops.ParseOperandName(Name, false);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000204
Chris Lattnerc240bb02010-11-01 04:03:32 +0000205 // Build the string for the operand
206 if (!Ops[Op.first].Constraints[Op.second].isNone())
207 throw "Operand '" + Name + "' cannot have multiple constraints!";
208 Ops[Op.first].Constraints[Op.second] =
209 CGIOperandList::ConstraintInfo::getEarlyClobber();
210 return;
211 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000212
Chris Lattnerc240bb02010-11-01 04:03:32 +0000213 // Only other constraint is "TIED_TO" for now.
214 std::string::size_type pos = CStr.find_first_of('=');
215 assert(pos != std::string::npos && "Unrecognized constraint");
216 start = CStr.find_first_not_of(" \t");
217 std::string Name = CStr.substr(start, pos - start);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000218
Chris Lattnerc240bb02010-11-01 04:03:32 +0000219 // TIED_TO: $src1 = $dst
220 wpos = Name.find_first_of(" \t");
221 if (wpos == std::string::npos)
222 throw "Illegal format for tied-to constraint: '" + CStr + "'";
223 std::string DestOpName = Name.substr(0, wpos);
224 std::pair<unsigned,unsigned> DestOp = Ops.ParseOperandName(DestOpName, false);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000225
Chris Lattnerc240bb02010-11-01 04:03:32 +0000226 Name = CStr.substr(pos+1);
227 wpos = Name.find_first_not_of(" \t");
228 if (wpos == std::string::npos)
229 throw "Illegal format for tied-to constraint: '" + CStr + "'";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000230
Chris Lattnerc240bb02010-11-01 04:03:32 +0000231 std::pair<unsigned,unsigned> SrcOp =
232 Ops.ParseOperandName(Name.substr(wpos), false);
233 if (SrcOp > DestOp)
234 throw "Illegal tied-to operand constraint '" + CStr + "'";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000235
236
Chris Lattnerc240bb02010-11-01 04:03:32 +0000237 unsigned FlatOpNo = Ops.getFlattenedOperandNumber(SrcOp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000238
Chris Lattnerc240bb02010-11-01 04:03:32 +0000239 if (!Ops[DestOp.first].Constraints[DestOp.second].isNone())
240 throw "Operand '" + DestOpName + "' cannot have multiple constraints!";
241 Ops[DestOp.first].Constraints[DestOp.second] =
242 CGIOperandList::ConstraintInfo::getTied(FlatOpNo);
243}
244
245static void ParseConstraints(const std::string &CStr, CGIOperandList &Ops) {
Chris Lattnerc240bb02010-11-01 04:03:32 +0000246 if (CStr.empty()) return;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000247
Chris Lattnerc240bb02010-11-01 04:03:32 +0000248 const std::string delims(",");
249 std::string::size_type bidx, eidx;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000250
Chris Lattnerc240bb02010-11-01 04:03:32 +0000251 bidx = CStr.find_first_not_of(delims);
252 while (bidx != std::string::npos) {
253 eidx = CStr.find_first_of(delims, bidx);
254 if (eidx == std::string::npos)
255 eidx = CStr.length();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000256
Chris Lattnerc240bb02010-11-01 04:03:32 +0000257 ParseConstraint(CStr.substr(bidx, eidx - bidx), Ops);
258 bidx = CStr.find_first_not_of(delims, eidx);
259 }
260}
261
262void CGIOperandList::ProcessDisableEncoding(std::string DisableEncoding) {
263 while (1) {
264 std::string OpName;
265 tie(OpName, DisableEncoding) = getToken(DisableEncoding, " ,\t");
266 if (OpName.empty()) break;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000267
Chris Lattnerc240bb02010-11-01 04:03:32 +0000268 // Figure out which operand this is.
269 std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000270
Chris Lattnerc240bb02010-11-01 04:03:32 +0000271 // Mark the operand as not-to-be encoded.
272 if (Op.second >= OperandList[Op.first].DoNotEncode.size())
273 OperandList[Op.first].DoNotEncode.resize(Op.second+1);
274 OperandList[Op.first].DoNotEncode[Op.second] = true;
275 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000276
Chris Lattnerc240bb02010-11-01 04:03:32 +0000277}
278
279//===----------------------------------------------------------------------===//
280// CodeGenInstruction Implementation
281//===----------------------------------------------------------------------===//
282
283CodeGenInstruction::CodeGenInstruction(Record *R) : TheDef(R), Operands(R) {
284 Namespace = R->getValueAsString("Namespace");
285 AsmString = R->getValueAsString("AsmString");
286
287 isReturn = R->getValueAsBit("isReturn");
288 isBranch = R->getValueAsBit("isBranch");
289 isIndirectBranch = R->getValueAsBit("isIndirectBranch");
290 isCompare = R->getValueAsBit("isCompare");
Evan Chengc4af4632010-11-17 20:13:28 +0000291 isMoveImm = R->getValueAsBit("isMoveImm");
Evan Cheng0f040a22011-03-15 05:09:26 +0000292 isBitcast = R->getValueAsBit("isBitcast");
Chris Lattnerc240bb02010-11-01 04:03:32 +0000293 isBarrier = R->getValueAsBit("isBarrier");
294 isCall = R->getValueAsBit("isCall");
295 canFoldAsLoad = R->getValueAsBit("canFoldAsLoad");
296 mayLoad = R->getValueAsBit("mayLoad");
297 mayStore = R->getValueAsBit("mayStore");
298 isPredicable = Operands.isPredicable || R->getValueAsBit("isPredicable");
299 isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
300 isCommutable = R->getValueAsBit("isCommutable");
301 isTerminator = R->getValueAsBit("isTerminator");
302 isReMaterializable = R->getValueAsBit("isReMaterializable");
303 hasDelaySlot = R->getValueAsBit("hasDelaySlot");
304 usesCustomInserter = R->getValueAsBit("usesCustomInserter");
305 hasCtrlDep = R->getValueAsBit("hasCtrlDep");
306 isNotDuplicable = R->getValueAsBit("isNotDuplicable");
307 hasSideEffects = R->getValueAsBit("hasSideEffects");
308 neverHasSideEffects = R->getValueAsBit("neverHasSideEffects");
309 isAsCheapAsAMove = R->getValueAsBit("isAsCheapAsAMove");
310 hasExtraSrcRegAllocReq = R->getValueAsBit("hasExtraSrcRegAllocReq");
311 hasExtraDefRegAllocReq = R->getValueAsBit("hasExtraDefRegAllocReq");
312 ImplicitDefs = R->getValueAsListOfDefs("Defs");
313 ImplicitUses = R->getValueAsListOfDefs("Uses");
314
315 if (neverHasSideEffects + hasSideEffects > 1)
316 throw R->getName() + ": multiple conflicting side-effect flags set!";
317
318 // Parse Constraints.
319 ParseConstraints(R->getValueAsString("Constraints"), Operands);
320
321 // Parse the DisableEncoding field.
322 Operands.ProcessDisableEncoding(R->getValueAsString("DisableEncoding"));
323}
Chris Lattner9414ae52010-03-27 20:09:24 +0000324
325/// HasOneImplicitDefWithKnownVT - If the instruction has at least one
326/// implicit def and it has a known VT, return the VT, otherwise return
327/// MVT::Other.
328MVT::SimpleValueType CodeGenInstruction::
329HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const {
330 if (ImplicitDefs.empty()) return MVT::Other;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000331
Chris Lattner9414ae52010-03-27 20:09:24 +0000332 // Check to see if the first implicit def has a resolvable type.
333 Record *FirstImplicitDef = ImplicitDefs[0];
334 assert(FirstImplicitDef->isSubClassOf("Register"));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000335 const std::vector<MVT::SimpleValueType> &RegVTs =
Chris Lattner9414ae52010-03-27 20:09:24 +0000336 TargetInfo.getRegisterVTs(FirstImplicitDef);
337 if (RegVTs.size() == 1)
338 return RegVTs[0];
339 return MVT::Other;
340}
341
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000342
343/// FlattenAsmStringVariants - Flatten the specified AsmString to only
344/// include text from the specified variant, returning the new string.
345std::string CodeGenInstruction::
346FlattenAsmStringVariants(StringRef Cur, unsigned Variant) {
347 std::string Res = "";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000348
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000349 for (;;) {
350 // Find the start of the next variant string.
351 size_t VariantsStart = 0;
352 for (size_t e = Cur.size(); VariantsStart != e; ++VariantsStart)
353 if (Cur[VariantsStart] == '{' &&
354 (VariantsStart == 0 || (Cur[VariantsStart-1] != '$' &&
355 Cur[VariantsStart-1] != '\\')))
356 break;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000357
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000358 // Add the prefix to the result.
359 Res += Cur.slice(0, VariantsStart);
360 if (VariantsStart == Cur.size())
361 break;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000362
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000363 ++VariantsStart; // Skip the '{'.
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000364
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000365 // Scan to the end of the variants string.
366 size_t VariantsEnd = VariantsStart;
367 unsigned NestedBraces = 1;
368 for (size_t e = Cur.size(); VariantsEnd != e; ++VariantsEnd) {
369 if (Cur[VariantsEnd] == '}' && Cur[VariantsEnd-1] != '\\') {
370 if (--NestedBraces == 0)
371 break;
372 } else if (Cur[VariantsEnd] == '{')
373 ++NestedBraces;
374 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000375
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000376 // Select the Nth variant (or empty).
377 StringRef Selection = Cur.slice(VariantsStart, VariantsEnd);
378 for (unsigned i = 0; i != Variant; ++i)
379 Selection = Selection.split('|').second;
380 Res += Selection.split('|').first;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000381
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000382 assert(VariantsEnd != Cur.size() &&
383 "Unterminated variants in assembly string!");
384 Cur = Cur.substr(VariantsEnd + 1);
385 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000386
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000387 return Res;
388}
389
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000390
391//===----------------------------------------------------------------------===//
392/// CodeGenInstAlias Implementation
393//===----------------------------------------------------------------------===//
394
Bob Wilsona49c7df2011-01-26 19:44:55 +0000395/// tryAliasOpMatch - This is a helper function for the CodeGenInstAlias
396/// constructor. It checks if an argument in an InstAlias pattern matches
397/// the corresponding operand of the instruction. It returns true on a
398/// successful match, with ResOp set to the result operand to be used.
399bool CodeGenInstAlias::tryAliasOpMatch(DagInit *Result, unsigned AliasOpNo,
400 Record *InstOpRec, bool hasSubOps,
401 SMLoc Loc, CodeGenTarget &T,
402 ResultOperand &ResOp) {
403 Init *Arg = Result->getArg(AliasOpNo);
404 DefInit *ADI = dynamic_cast<DefInit*>(Arg);
405
406 if (ADI && ADI->getDef() == InstOpRec) {
407 // If the operand is a record, it must have a name, and the record type
408 // must match up with the instruction's argument type.
409 if (Result->getArgName(AliasOpNo).empty())
410 throw TGError(Loc, "result argument #" + utostr(AliasOpNo) +
411 " must have a name!");
412 ResOp = ResultOperand(Result->getArgName(AliasOpNo), ADI->getDef());
413 return true;
414 }
415
416 // Handle explicit registers.
417 if (ADI && ADI->getDef()->isSubClassOf("Register")) {
418 if (!InstOpRec->isSubClassOf("RegisterClass"))
419 return false;
420
Jakob Stoklund Olesenae1920b2011-06-15 04:50:36 +0000421 if (!T.getRegisterClass(InstOpRec)
422 .contains(T.getRegBank().getReg(ADI->getDef())))
Bob Wilsona49c7df2011-01-26 19:44:55 +0000423 throw TGError(Loc, "fixed register " +ADI->getDef()->getName()
424 + " is not a member of the " + InstOpRec->getName() +
425 " register class!");
426
427 if (!Result->getArgName(AliasOpNo).empty())
428 throw TGError(Loc, "result fixed register argument must "
429 "not have a name!");
430
431 ResOp = ResultOperand(ADI->getDef());
432 return true;
433 }
434
435 // Handle "zero_reg" for optional def operands.
436 if (ADI && ADI->getDef()->getName() == "zero_reg") {
437
438 // Check if this is an optional def.
439 if (!InstOpRec->isSubClassOf("OptionalDefOperand"))
440 throw TGError(Loc, "reg0 used for result that is not an "
441 "OptionalDefOperand!");
442
443 ResOp = ResultOperand(static_cast<Record*>(0));
444 return true;
445 }
446
447 if (IntInit *II = dynamic_cast<IntInit*>(Arg)) {
448 if (hasSubOps || !InstOpRec->isSubClassOf("Operand"))
449 return false;
450 // Integer arguments can't have names.
451 if (!Result->getArgName(AliasOpNo).empty())
452 throw TGError(Loc, "result argument #" + utostr(AliasOpNo) +
453 " must not have a name!");
454 ResOp = ResultOperand(II->getValue());
455 return true;
456 }
457
458 return false;
459}
460
Chris Lattner662e5a32010-11-06 07:14:44 +0000461CodeGenInstAlias::CodeGenInstAlias(Record *R, CodeGenTarget &T) : TheDef(R) {
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000462 AsmString = R->getValueAsString("AsmString");
Chris Lattnerb501d4f2010-11-01 05:34:34 +0000463 Result = R->getValueAsDag("ResultInst");
Chris Lattner225549f2010-11-06 06:39:47 +0000464
465 // Verify that the root of the result is an instruction.
466 DefInit *DI = dynamic_cast<DefInit*>(Result->getOperator());
467 if (DI == 0 || !DI->getDef()->isSubClassOf("Instruction"))
468 throw TGError(R->getLoc(), "result of inst alias should be an instruction");
469
470 ResultInst = &T.getInstruction(DI->getDef());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000471
Chris Lattner3f2c8e42010-11-06 07:06:09 +0000472 // NameClass - If argument names are repeated, we need to verify they have
473 // the same class.
474 StringMap<Record*> NameClass;
Bob Wilson55931ab2011-01-20 18:38:10 +0000475 for (unsigned i = 0, e = Result->getNumArgs(); i != e; ++i) {
476 DefInit *ADI = dynamic_cast<DefInit*>(Result->getArg(i));
477 if (!ADI || Result->getArgName(i).empty())
478 continue;
479 // Verify we don't have something like: (someinst GR16:$foo, GR32:$foo)
480 // $foo can exist multiple times in the result list, but it must have the
481 // same type.
482 Record *&Entry = NameClass[Result->getArgName(i)];
483 if (Entry && Entry != ADI->getDef())
484 throw TGError(R->getLoc(), "result value $" + Result->getArgName(i) +
485 " is both " + Entry->getName() + " and " +
486 ADI->getDef()->getName() + "!");
487 Entry = ADI->getDef();
488 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000489
Chris Lattnerd0f225c2010-11-06 06:54:38 +0000490 // Decode and validate the arguments of the result.
Chris Lattner41409852010-11-06 07:31:43 +0000491 unsigned AliasOpNo = 0;
492 for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) {
Bob Wilsona49c7df2011-01-26 19:44:55 +0000493
Chris Lattner41409852010-11-06 07:31:43 +0000494 // Tied registers don't have an entry in the result dag.
495 if (ResultInst->Operands[i].getTiedRegister() != -1)
496 continue;
497
498 if (AliasOpNo >= Result->getNumArgs())
Bob Wilsona49c7df2011-01-26 19:44:55 +0000499 throw TGError(R->getLoc(), "not enough arguments for instruction!");
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000500
Bob Wilsona49c7df2011-01-26 19:44:55 +0000501 Record *InstOpRec = ResultInst->Operands[i].Rec;
502 unsigned NumSubOps = ResultInst->Operands[i].MINumOperands;
503 ResultOperand ResOp(static_cast<int64_t>(0));
504 if (tryAliasOpMatch(Result, AliasOpNo, InstOpRec, (NumSubOps > 1),
505 R->getLoc(), T, ResOp)) {
506 ResultOperands.push_back(ResOp);
507 ResultInstOperandIndex.push_back(std::make_pair(i, -1));
Chris Lattner41409852010-11-06 07:31:43 +0000508 ++AliasOpNo;
Chris Lattnerd0f225c2010-11-06 06:54:38 +0000509 continue;
510 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000511
Bob Wilsona49c7df2011-01-26 19:44:55 +0000512 // If the argument did not match the instruction operand, and the operand
513 // is composed of multiple suboperands, try matching the suboperands.
514 if (NumSubOps > 1) {
515 DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo;
516 for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) {
517 if (AliasOpNo >= Result->getNumArgs())
518 throw TGError(R->getLoc(), "not enough arguments for instruction!");
519 Record *SubRec = dynamic_cast<DefInit*>(MIOI->getArg(SubOp))->getDef();
520 if (tryAliasOpMatch(Result, AliasOpNo, SubRec, false,
521 R->getLoc(), T, ResOp)) {
522 ResultOperands.push_back(ResOp);
523 ResultInstOperandIndex.push_back(std::make_pair(i, SubOp));
524 ++AliasOpNo;
525 } else {
526 throw TGError(R->getLoc(), "result argument #" + utostr(AliasOpNo) +
527 " does not match instruction operand class " +
528 (SubOp == 0 ? InstOpRec->getName() :SubRec->getName()));
529 }
530 }
Chris Lattner98c870f2010-11-06 19:25:43 +0000531 continue;
532 }
Bob Wilsona49c7df2011-01-26 19:44:55 +0000533 throw TGError(R->getLoc(), "result argument #" + utostr(AliasOpNo) +
534 " does not match instruction operand class " +
535 InstOpRec->getName());
Chris Lattnerd0f225c2010-11-06 06:54:38 +0000536 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000537
Chris Lattner41409852010-11-06 07:31:43 +0000538 if (AliasOpNo != Result->getNumArgs())
Bob Wilsona49c7df2011-01-26 19:44:55 +0000539 throw TGError(R->getLoc(), "too many operands for instruction!");
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000540}