blob: 5b0aedfbc836750e1b9bb82b67526a8b10ce0e1a [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;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000031
Chris Lattnerf55eed22010-03-18 21:07:39 +000032 DagInit *OutDI = R->getValueAsDag("OutOperandList");
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +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'";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +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'";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +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 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000061
Chris Lattnerf55eed22010-03-18 21:07:39 +000062 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!";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +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");
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +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";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000083
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;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +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") &&
NAKAMURA Takumi36c3bc42011-01-26 02:03:48 +000097 !Rec->isSubClassOf("PointerLikeRegClass") &&
98 Rec->getName() != "unknown")
Chris Lattner6cc654b2008-01-06 01:35:39 +000099 throw "Unknown operand class '" + Rec->getName() +
Chris Lattnerc240bb02010-11-01 04:03:32 +0000100 "' in '" + R->getName() + "' instruction!";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000101
Chris Lattner6cc654b2008-01-06 01:35:39 +0000102 // Check that the operand has a name and that it's unique.
Chris Lattnerf55eed22010-03-18 21:07:39 +0000103 if (ArgName.empty())
Chris Lattner6cc654b2008-01-06 01:35:39 +0000104 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
Chris Lattnerc240bb02010-11-01 04:03:32 +0000105 " has no name!";
Chris Lattnerf55eed22010-03-18 21:07:39 +0000106 if (!OperandNames.insert(ArgName).second)
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 the same name as a previous operand!";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000109
Jim Grosbach5013f742010-10-12 22:21:57 +0000110 OperandList.push_back(OperandInfo(Rec, ArgName, PrintMethod, EncoderMethod,
Chris Lattner6cc654b2008-01-06 01:35:39 +0000111 MIOperandNo, NumOps, MIOpInfo));
112 MIOperandNo += NumOps;
113 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000114
115
Chris Lattnerb501d4f2010-11-01 05:34:34 +0000116 // Make sure the constraints list for each operand is large enough to hold
117 // constraint info, even if none is present.
118 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
119 OperandList[i].Constraints.resize(OperandList[i].MINumOperands);
Chris Lattner6cc654b2008-01-06 01:35:39 +0000120}
121
Chris Lattnerc240bb02010-11-01 04:03:32 +0000122
Chris Lattner6cc654b2008-01-06 01:35:39 +0000123/// getOperandNamed - Return the index of the operand with the specified
124/// non-empty name. If the instruction does not have an operand with the
125/// specified name, throw an exception.
126///
Chris Lattnerc240bb02010-11-01 04:03:32 +0000127unsigned CGIOperandList::getOperandNamed(StringRef Name) const {
Jim Grosbach01855072010-10-11 18:25:51 +0000128 unsigned OpIdx;
129 if (hasOperandNamed(Name, OpIdx)) return OpIdx;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000130 throw "'" + TheDef->getName() + "' does not have an operand named '$" +
Chris Lattnerc240bb02010-11-01 04:03:32 +0000131 Name.str() + "'!";
Chris Lattner6cc654b2008-01-06 01:35:39 +0000132}
133
Jim Grosbach01855072010-10-11 18:25:51 +0000134/// hasOperandNamed - Query whether the instruction has an operand of the
135/// given name. If so, return true and set OpIdx to the index of the
136/// operand. Otherwise, return false.
Chris Lattnerc240bb02010-11-01 04:03:32 +0000137bool CGIOperandList::hasOperandNamed(StringRef Name, unsigned &OpIdx) const {
Jim Grosbach01855072010-10-11 18:25:51 +0000138 assert(!Name.empty() && "Cannot search for operand with no name!");
139 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
140 if (OperandList[i].Name == Name) {
141 OpIdx = i;
142 return true;
143 }
144 return false;
145}
146
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000147std::pair<unsigned,unsigned>
Chris Lattnerc240bb02010-11-01 04:03:32 +0000148CGIOperandList::ParseOperandName(const std::string &Op, bool AllowWholeOp) {
Chris Lattner6cc654b2008-01-06 01:35:39 +0000149 if (Op.empty() || Op[0] != '$')
150 throw TheDef->getName() + ": Illegal operand name: '" + Op + "'";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000151
Chris Lattner6cc654b2008-01-06 01:35:39 +0000152 std::string OpName = Op.substr(1);
153 std::string SubOpName;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000154
Chris Lattner6cc654b2008-01-06 01:35:39 +0000155 // Check to see if this is $foo.bar.
156 std::string::size_type DotIdx = OpName.find_first_of(".");
157 if (DotIdx != std::string::npos) {
158 SubOpName = OpName.substr(DotIdx+1);
159 if (SubOpName.empty())
160 throw TheDef->getName() + ": illegal empty suboperand name in '" +Op +"'";
161 OpName = OpName.substr(0, DotIdx);
162 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000163
Chris Lattner6cc654b2008-01-06 01:35:39 +0000164 unsigned OpIdx = getOperandNamed(OpName);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000165
Chris Lattner6cc654b2008-01-06 01:35:39 +0000166 if (SubOpName.empty()) { // If no suboperand name was specified:
167 // If one was needed, throw.
168 if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp &&
169 SubOpName.empty())
170 throw TheDef->getName() + ": Illegal to refer to"
Chris Lattnerc240bb02010-11-01 04:03:32 +0000171 " whole operand part of complex operand '" + Op + "'";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000172
Chris Lattner6cc654b2008-01-06 01:35:39 +0000173 // Otherwise, return the operand.
174 return std::make_pair(OpIdx, 0U);
175 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000176
Chris Lattner6cc654b2008-01-06 01:35:39 +0000177 // Find the suboperand number involved.
178 DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
179 if (MIOpInfo == 0)
180 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000181
Chris Lattner6cc654b2008-01-06 01:35:39 +0000182 // Find the operand with the right name.
183 for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i)
184 if (MIOpInfo->getArgName(i) == SubOpName)
185 return std::make_pair(OpIdx, i);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000186
Chris Lattner6cc654b2008-01-06 01:35:39 +0000187 // Otherwise, didn't find it!
188 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
189}
Chris Lattner9414ae52010-03-27 20:09:24 +0000190
Chris Lattnerc240bb02010-11-01 04:03:32 +0000191static void ParseConstraint(const std::string &CStr, CGIOperandList &Ops) {
192 // EARLY_CLOBBER: @early $reg
193 std::string::size_type wpos = CStr.find_first_of(" \t");
194 std::string::size_type start = CStr.find_first_not_of(" \t");
195 std::string Tok = CStr.substr(start, wpos - start);
196 if (Tok == "@earlyclobber") {
197 std::string Name = CStr.substr(wpos+1);
198 wpos = Name.find_first_not_of(" \t");
199 if (wpos == std::string::npos)
200 throw "Illegal format for @earlyclobber constraint: '" + CStr + "'";
201 Name = Name.substr(wpos);
202 std::pair<unsigned,unsigned> Op = Ops.ParseOperandName(Name, false);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000203
Chris Lattnerc240bb02010-11-01 04:03:32 +0000204 // Build the string for the operand
205 if (!Ops[Op.first].Constraints[Op.second].isNone())
206 throw "Operand '" + Name + "' cannot have multiple constraints!";
207 Ops[Op.first].Constraints[Op.second] =
208 CGIOperandList::ConstraintInfo::getEarlyClobber();
209 return;
210 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000211
Chris Lattnerc240bb02010-11-01 04:03:32 +0000212 // Only other constraint is "TIED_TO" for now.
213 std::string::size_type pos = CStr.find_first_of('=');
214 assert(pos != std::string::npos && "Unrecognized constraint");
215 start = CStr.find_first_not_of(" \t");
216 std::string Name = CStr.substr(start, pos - start);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000217
Chris Lattnerc240bb02010-11-01 04:03:32 +0000218 // TIED_TO: $src1 = $dst
219 wpos = Name.find_first_of(" \t");
220 if (wpos == std::string::npos)
221 throw "Illegal format for tied-to constraint: '" + CStr + "'";
222 std::string DestOpName = Name.substr(0, wpos);
223 std::pair<unsigned,unsigned> DestOp = Ops.ParseOperandName(DestOpName, false);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000224
Chris Lattnerc240bb02010-11-01 04:03:32 +0000225 Name = CStr.substr(pos+1);
226 wpos = Name.find_first_not_of(" \t");
227 if (wpos == std::string::npos)
228 throw "Illegal format for tied-to constraint: '" + CStr + "'";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000229
Chris Lattnerc240bb02010-11-01 04:03:32 +0000230 std::pair<unsigned,unsigned> SrcOp =
231 Ops.ParseOperandName(Name.substr(wpos), false);
232 if (SrcOp > DestOp)
233 throw "Illegal tied-to operand constraint '" + CStr + "'";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000234
235
Chris Lattnerc240bb02010-11-01 04:03:32 +0000236 unsigned FlatOpNo = Ops.getFlattenedOperandNumber(SrcOp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000237
Chris Lattnerc240bb02010-11-01 04:03:32 +0000238 if (!Ops[DestOp.first].Constraints[DestOp.second].isNone())
239 throw "Operand '" + DestOpName + "' cannot have multiple constraints!";
240 Ops[DestOp.first].Constraints[DestOp.second] =
241 CGIOperandList::ConstraintInfo::getTied(FlatOpNo);
242}
243
244static void ParseConstraints(const std::string &CStr, CGIOperandList &Ops) {
Chris Lattnerc240bb02010-11-01 04:03:32 +0000245 if (CStr.empty()) return;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000246
Chris Lattnerc240bb02010-11-01 04:03:32 +0000247 const std::string delims(",");
248 std::string::size_type bidx, eidx;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000249
Chris Lattnerc240bb02010-11-01 04:03:32 +0000250 bidx = CStr.find_first_not_of(delims);
251 while (bidx != std::string::npos) {
252 eidx = CStr.find_first_of(delims, bidx);
253 if (eidx == std::string::npos)
254 eidx = CStr.length();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000255
Chris Lattnerc240bb02010-11-01 04:03:32 +0000256 ParseConstraint(CStr.substr(bidx, eidx - bidx), Ops);
257 bidx = CStr.find_first_not_of(delims, eidx);
258 }
259}
260
261void CGIOperandList::ProcessDisableEncoding(std::string DisableEncoding) {
262 while (1) {
263 std::string OpName;
264 tie(OpName, DisableEncoding) = getToken(DisableEncoding, " ,\t");
265 if (OpName.empty()) break;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000266
Chris Lattnerc240bb02010-11-01 04:03:32 +0000267 // Figure out which operand this is.
268 std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000269
Chris Lattnerc240bb02010-11-01 04:03:32 +0000270 // Mark the operand as not-to-be encoded.
271 if (Op.second >= OperandList[Op.first].DoNotEncode.size())
272 OperandList[Op.first].DoNotEncode.resize(Op.second+1);
273 OperandList[Op.first].DoNotEncode[Op.second] = true;
274 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000275
Chris Lattnerc240bb02010-11-01 04:03:32 +0000276}
277
278//===----------------------------------------------------------------------===//
279// CodeGenInstruction Implementation
280//===----------------------------------------------------------------------===//
281
282CodeGenInstruction::CodeGenInstruction(Record *R) : TheDef(R), Operands(R) {
283 Namespace = R->getValueAsString("Namespace");
284 AsmString = R->getValueAsString("AsmString");
285
286 isReturn = R->getValueAsBit("isReturn");
287 isBranch = R->getValueAsBit("isBranch");
288 isIndirectBranch = R->getValueAsBit("isIndirectBranch");
289 isCompare = R->getValueAsBit("isCompare");
Evan Chengc4af4632010-11-17 20:13:28 +0000290 isMoveImm = R->getValueAsBit("isMoveImm");
Evan Cheng0f040a22011-03-15 05:09:26 +0000291 isBitcast = R->getValueAsBit("isBitcast");
Chris Lattnerc240bb02010-11-01 04:03:32 +0000292 isBarrier = R->getValueAsBit("isBarrier");
293 isCall = R->getValueAsBit("isCall");
294 canFoldAsLoad = R->getValueAsBit("canFoldAsLoad");
295 mayLoad = R->getValueAsBit("mayLoad");
296 mayStore = R->getValueAsBit("mayStore");
297 isPredicable = Operands.isPredicable || R->getValueAsBit("isPredicable");
298 isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
299 isCommutable = R->getValueAsBit("isCommutable");
300 isTerminator = R->getValueAsBit("isTerminator");
301 isReMaterializable = R->getValueAsBit("isReMaterializable");
302 hasDelaySlot = R->getValueAsBit("hasDelaySlot");
303 usesCustomInserter = R->getValueAsBit("usesCustomInserter");
304 hasCtrlDep = R->getValueAsBit("hasCtrlDep");
305 isNotDuplicable = R->getValueAsBit("isNotDuplicable");
306 hasSideEffects = R->getValueAsBit("hasSideEffects");
307 neverHasSideEffects = R->getValueAsBit("neverHasSideEffects");
308 isAsCheapAsAMove = R->getValueAsBit("isAsCheapAsAMove");
309 hasExtraSrcRegAllocReq = R->getValueAsBit("hasExtraSrcRegAllocReq");
310 hasExtraDefRegAllocReq = R->getValueAsBit("hasExtraDefRegAllocReq");
311 ImplicitDefs = R->getValueAsListOfDefs("Defs");
312 ImplicitUses = R->getValueAsListOfDefs("Uses");
313
314 if (neverHasSideEffects + hasSideEffects > 1)
315 throw R->getName() + ": multiple conflicting side-effect flags set!";
316
317 // Parse Constraints.
318 ParseConstraints(R->getValueAsString("Constraints"), Operands);
319
320 // Parse the DisableEncoding field.
321 Operands.ProcessDisableEncoding(R->getValueAsString("DisableEncoding"));
322}
Chris Lattner9414ae52010-03-27 20:09:24 +0000323
324/// HasOneImplicitDefWithKnownVT - If the instruction has at least one
325/// implicit def and it has a known VT, return the VT, otherwise return
326/// MVT::Other.
327MVT::SimpleValueType CodeGenInstruction::
328HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const {
329 if (ImplicitDefs.empty()) return MVT::Other;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000330
Chris Lattner9414ae52010-03-27 20:09:24 +0000331 // Check to see if the first implicit def has a resolvable type.
332 Record *FirstImplicitDef = ImplicitDefs[0];
333 assert(FirstImplicitDef->isSubClassOf("Register"));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000334 const std::vector<MVT::SimpleValueType> &RegVTs =
Chris Lattner9414ae52010-03-27 20:09:24 +0000335 TargetInfo.getRegisterVTs(FirstImplicitDef);
336 if (RegVTs.size() == 1)
337 return RegVTs[0];
338 return MVT::Other;
339}
340
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000341
342/// FlattenAsmStringVariants - Flatten the specified AsmString to only
343/// include text from the specified variant, returning the new string.
344std::string CodeGenInstruction::
345FlattenAsmStringVariants(StringRef Cur, unsigned Variant) {
346 std::string Res = "";
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000347
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000348 for (;;) {
349 // Find the start of the next variant string.
350 size_t VariantsStart = 0;
351 for (size_t e = Cur.size(); VariantsStart != e; ++VariantsStart)
352 if (Cur[VariantsStart] == '{' &&
353 (VariantsStart == 0 || (Cur[VariantsStart-1] != '$' &&
354 Cur[VariantsStart-1] != '\\')))
355 break;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000356
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000357 // Add the prefix to the result.
358 Res += Cur.slice(0, VariantsStart);
359 if (VariantsStart == Cur.size())
360 break;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000361
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000362 ++VariantsStart; // Skip the '{'.
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000363
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000364 // Scan to the end of the variants string.
365 size_t VariantsEnd = VariantsStart;
366 unsigned NestedBraces = 1;
367 for (size_t e = Cur.size(); VariantsEnd != e; ++VariantsEnd) {
368 if (Cur[VariantsEnd] == '}' && Cur[VariantsEnd-1] != '\\') {
369 if (--NestedBraces == 0)
370 break;
371 } else if (Cur[VariantsEnd] == '{')
372 ++NestedBraces;
373 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000374
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000375 // Select the Nth variant (or empty).
376 StringRef Selection = Cur.slice(VariantsStart, VariantsEnd);
377 for (unsigned i = 0; i != Variant; ++i)
378 Selection = Selection.split('|').second;
379 Res += Selection.split('|').first;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000380
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000381 assert(VariantsEnd != Cur.size() &&
382 "Unterminated variants in assembly string!");
383 Cur = Cur.substr(VariantsEnd + 1);
384 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000385
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000386 return Res;
387}
388
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000389
390//===----------------------------------------------------------------------===//
391/// CodeGenInstAlias Implementation
392//===----------------------------------------------------------------------===//
393
Bob Wilsona49c7df2011-01-26 19:44:55 +0000394/// tryAliasOpMatch - This is a helper function for the CodeGenInstAlias
395/// constructor. It checks if an argument in an InstAlias pattern matches
396/// the corresponding operand of the instruction. It returns true on a
397/// successful match, with ResOp set to the result operand to be used.
398bool CodeGenInstAlias::tryAliasOpMatch(DagInit *Result, unsigned AliasOpNo,
399 Record *InstOpRec, bool hasSubOps,
400 SMLoc Loc, CodeGenTarget &T,
401 ResultOperand &ResOp) {
402 Init *Arg = Result->getArg(AliasOpNo);
403 DefInit *ADI = dynamic_cast<DefInit*>(Arg);
404
405 if (ADI && ADI->getDef() == InstOpRec) {
406 // If the operand is a record, it must have a name, and the record type
407 // must match up with the instruction's argument type.
408 if (Result->getArgName(AliasOpNo).empty())
409 throw TGError(Loc, "result argument #" + utostr(AliasOpNo) +
410 " must have a name!");
411 ResOp = ResultOperand(Result->getArgName(AliasOpNo), ADI->getDef());
412 return true;
413 }
414
415 // Handle explicit registers.
416 if (ADI && ADI->getDef()->isSubClassOf("Register")) {
417 if (!InstOpRec->isSubClassOf("RegisterClass"))
418 return false;
419
420 if (!T.getRegisterClass(InstOpRec).containsRegister(ADI->getDef()))
421 throw TGError(Loc, "fixed register " +ADI->getDef()->getName()
422 + " is not a member of the " + InstOpRec->getName() +
423 " register class!");
424
425 if (!Result->getArgName(AliasOpNo).empty())
426 throw TGError(Loc, "result fixed register argument must "
427 "not have a name!");
428
429 ResOp = ResultOperand(ADI->getDef());
430 return true;
431 }
432
433 // Handle "zero_reg" for optional def operands.
434 if (ADI && ADI->getDef()->getName() == "zero_reg") {
435
436 // Check if this is an optional def.
437 if (!InstOpRec->isSubClassOf("OptionalDefOperand"))
438 throw TGError(Loc, "reg0 used for result that is not an "
439 "OptionalDefOperand!");
440
441 ResOp = ResultOperand(static_cast<Record*>(0));
442 return true;
443 }
444
445 if (IntInit *II = dynamic_cast<IntInit*>(Arg)) {
446 if (hasSubOps || !InstOpRec->isSubClassOf("Operand"))
447 return false;
448 // Integer arguments can't have names.
449 if (!Result->getArgName(AliasOpNo).empty())
450 throw TGError(Loc, "result argument #" + utostr(AliasOpNo) +
451 " must not have a name!");
452 ResOp = ResultOperand(II->getValue());
453 return true;
454 }
455
456 return false;
457}
458
Chris Lattner662e5a32010-11-06 07:14:44 +0000459CodeGenInstAlias::CodeGenInstAlias(Record *R, CodeGenTarget &T) : TheDef(R) {
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000460 AsmString = R->getValueAsString("AsmString");
Chris Lattnerb501d4f2010-11-01 05:34:34 +0000461 Result = R->getValueAsDag("ResultInst");
Chris Lattner225549f2010-11-06 06:39:47 +0000462
463 // Verify that the root of the result is an instruction.
464 DefInit *DI = dynamic_cast<DefInit*>(Result->getOperator());
465 if (DI == 0 || !DI->getDef()->isSubClassOf("Instruction"))
466 throw TGError(R->getLoc(), "result of inst alias should be an instruction");
467
468 ResultInst = &T.getInstruction(DI->getDef());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000469
Chris Lattner3f2c8e42010-11-06 07:06:09 +0000470 // NameClass - If argument names are repeated, we need to verify they have
471 // the same class.
472 StringMap<Record*> NameClass;
Bob Wilson55931ab2011-01-20 18:38:10 +0000473 for (unsigned i = 0, e = Result->getNumArgs(); i != e; ++i) {
474 DefInit *ADI = dynamic_cast<DefInit*>(Result->getArg(i));
475 if (!ADI || Result->getArgName(i).empty())
476 continue;
477 // Verify we don't have something like: (someinst GR16:$foo, GR32:$foo)
478 // $foo can exist multiple times in the result list, but it must have the
479 // same type.
480 Record *&Entry = NameClass[Result->getArgName(i)];
481 if (Entry && Entry != ADI->getDef())
482 throw TGError(R->getLoc(), "result value $" + Result->getArgName(i) +
483 " is both " + Entry->getName() + " and " +
484 ADI->getDef()->getName() + "!");
485 Entry = ADI->getDef();
486 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000487
Chris Lattnerd0f225c2010-11-06 06:54:38 +0000488 // Decode and validate the arguments of the result.
Chris Lattner41409852010-11-06 07:31:43 +0000489 unsigned AliasOpNo = 0;
490 for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) {
Bob Wilsona49c7df2011-01-26 19:44:55 +0000491
Chris Lattner41409852010-11-06 07:31:43 +0000492 // Tied registers don't have an entry in the result dag.
493 if (ResultInst->Operands[i].getTiedRegister() != -1)
494 continue;
495
496 if (AliasOpNo >= Result->getNumArgs())
Bob Wilsona49c7df2011-01-26 19:44:55 +0000497 throw TGError(R->getLoc(), "not enough arguments for instruction!");
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000498
Bob Wilsona49c7df2011-01-26 19:44:55 +0000499 Record *InstOpRec = ResultInst->Operands[i].Rec;
500 unsigned NumSubOps = ResultInst->Operands[i].MINumOperands;
501 ResultOperand ResOp(static_cast<int64_t>(0));
502 if (tryAliasOpMatch(Result, AliasOpNo, InstOpRec, (NumSubOps > 1),
503 R->getLoc(), T, ResOp)) {
504 ResultOperands.push_back(ResOp);
505 ResultInstOperandIndex.push_back(std::make_pair(i, -1));
Chris Lattner41409852010-11-06 07:31:43 +0000506 ++AliasOpNo;
Chris Lattnerd0f225c2010-11-06 06:54:38 +0000507 continue;
508 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000509
Bob Wilsona49c7df2011-01-26 19:44:55 +0000510 // If the argument did not match the instruction operand, and the operand
511 // is composed of multiple suboperands, try matching the suboperands.
512 if (NumSubOps > 1) {
513 DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo;
514 for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) {
515 if (AliasOpNo >= Result->getNumArgs())
516 throw TGError(R->getLoc(), "not enough arguments for instruction!");
517 Record *SubRec = dynamic_cast<DefInit*>(MIOI->getArg(SubOp))->getDef();
518 if (tryAliasOpMatch(Result, AliasOpNo, SubRec, false,
519 R->getLoc(), T, ResOp)) {
520 ResultOperands.push_back(ResOp);
521 ResultInstOperandIndex.push_back(std::make_pair(i, SubOp));
522 ++AliasOpNo;
523 } else {
524 throw TGError(R->getLoc(), "result argument #" + utostr(AliasOpNo) +
525 " does not match instruction operand class " +
526 (SubOp == 0 ? InstOpRec->getName() :SubRec->getName()));
527 }
528 }
Chris Lattner98c870f2010-11-06 19:25:43 +0000529 continue;
530 }
Bob Wilsona49c7df2011-01-26 19:44:55 +0000531 throw TGError(R->getLoc(), "result argument #" + utostr(AliasOpNo) +
532 " does not match instruction operand class " +
533 InstOpRec->getName());
Chris Lattnerd0f225c2010-11-06 06:54:38 +0000534 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000535
Chris Lattner41409852010-11-06 07:31:43 +0000536 if (AliasOpNo != Result->getNumArgs())
Bob Wilsona49c7df2011-01-26 19:44:55 +0000537 throw TGError(R->getLoc(), "too many operands for instruction!");
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000538}