blob: a52ce86c485a18993e9cf0b77a7ab2fea54963d2 [file] [log] [blame]
Chris Lattnerea2d52d2008-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 Lattner7bc5d9b2010-03-27 20:09:24 +000015#include "CodeGenTarget.h"
Jim Grosbach797cff02011-06-21 22:55:50 +000016#include "Error.h"
Chris Lattnerea2d52d2008-01-06 01:35:39 +000017#include "Record.h"
18#include "llvm/ADT/StringExtras.h"
Chris Lattnerb625dd22010-11-06 07:06:09 +000019#include "llvm/ADT/StringMap.h"
Benjamin Kramerc6fe3c32010-01-11 18:03:24 +000020#include "llvm/ADT/STLExtras.h"
Chris Lattnerea2d52d2008-01-06 01:35:39 +000021#include <set>
22using namespace llvm;
23
Chris Lattnerd8adec72010-11-01 04:03:32 +000024//===----------------------------------------------------------------------===//
25// CGIOperandList Implementation
26//===----------------------------------------------------------------------===//
Jim Grosbach2a282f22009-12-16 19:43:02 +000027
Chris Lattnerd8adec72010-11-01 04:03:32 +000028CGIOperandList::CGIOperandList(Record *R) : TheDef(R) {
29 isPredicable = false;
Chris Lattnerea2d52d2008-01-06 01:35:39 +000030 hasOptionalDef = false;
Chris Lattnerf376c992008-01-07 05:19:29 +000031 isVariadic = false;
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +000032
Eric Christopher71520a82011-07-11 23:06:52 +000033 DagInit *OutDI = R->getValueAsDag("OutOperandList");
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +000034
Eric Christopher71520a82011-07-11 23:06:52 +000035 if (DefInit *Init = dynamic_cast<DefInit*>(OutDI->getOperator())) {
Chris Lattner0f037472010-03-18 20:56:35 +000036 if (Init->getDef()->getName() != "outs")
Chris Lattner5f418ea2010-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 Takumi9d29eff2011-01-26 02:03:37 +000040
Chris Lattner1f22d272010-03-18 21:07:39 +000041 NumDefs = OutDI->getNumArgs();
Chris Lattnerd8adec72010-11-01 04:03:32 +000042
Eric Christopher71520a82011-07-11 23:06:52 +000043 DagInit *InDI = R->getValueAsDag("InOperandList");
44 if (DefInit *Init = dynamic_cast<DefInit*>(InDI->getOperator())) {
Chris Lattner0f037472010-03-18 20:56:35 +000045 if (Init->getDef()->getName() != "ins")
Chris Lattner5f418ea2010-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 Takumi9d29eff2011-01-26 02:03:37 +000049
Chris Lattnerea2d52d2008-01-06 01:35:39 +000050 unsigned MIOperandNo = 0;
51 std::set<std::string> OperandNames;
Chris Lattner1f22d272010-03-18 21:07:39 +000052 for (unsigned i = 0, e = InDI->getNumArgs()+OutDI->getNumArgs(); i != e; ++i){
Eric Christopher71520a82011-07-11 23:06:52 +000053 Init *ArgInit;
Chris Lattner1f22d272010-03-18 21:07:39 +000054 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 Takumi9d29eff2011-01-26 02:03:37 +000062
Eric Christopher71520a82011-07-11 23:06:52 +000063 DefInit *Arg = dynamic_cast<DefInit*>(ArgInit);
Chris Lattnerea2d52d2008-01-06 01:35:39 +000064 if (!Arg)
65 throw "Illegal operand for the '" + R->getName() + "' instruction!";
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +000066
Chris Lattnerea2d52d2008-01-06 01:35:39 +000067 Record *Rec = Arg->getDef();
68 std::string PrintMethod = "printOperand";
Jim Grosbach51a12eb2010-10-12 22:21:57 +000069 std::string EncoderMethod;
Benjamin Kramerc8dc46b2011-07-14 21:47:18 +000070 std::string OperandType = "OPERAND_UNKNOWN";
Chris Lattnerea2d52d2008-01-06 01:35:39 +000071 unsigned NumOps = 1;
Eric Christopher71520a82011-07-11 23:06:52 +000072 DagInit *MIOpInfo = 0;
Owen Andersona84be6c2011-06-27 21:06:21 +000073 if (Rec->isSubClassOf("RegisterOperand")) {
74 PrintMethod = Rec->getValueAsString("PrintMethod");
75 } else if (Rec->isSubClassOf("Operand")) {
Chris Lattnerea2d52d2008-01-06 01:35:39 +000076 PrintMethod = Rec->getValueAsString("PrintMethod");
Benjamin Kramerc8dc46b2011-07-14 21:47:18 +000077 OperandType = Rec->getValueAsString("OperandType");
Jim Grosbach51a12eb2010-10-12 22:21:57 +000078 // If there is an explicit encoder method, use it.
Chris Lattner63274cb2010-11-15 05:19:05 +000079 EncoderMethod = Rec->getValueAsString("EncoderMethod");
Chris Lattnerea2d52d2008-01-06 01:35:39 +000080 MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +000081
Chris Lattnerea2d52d2008-01-06 01:35:39 +000082 // Verify that MIOpInfo has an 'ops' root value.
Eric Christopher71520a82011-07-11 23:06:52 +000083 if (!dynamic_cast<DefInit*>(MIOpInfo->getOperator()) ||
84 dynamic_cast<DefInit*>(MIOpInfo->getOperator())
Chris Lattnerd8adec72010-11-01 04:03:32 +000085 ->getDef()->getName() != "ops")
Chris Lattnerea2d52d2008-01-06 01:35:39 +000086 throw "Bad value for MIOperandInfo in operand '" + Rec->getName() +
Chris Lattnerd8adec72010-11-01 04:03:32 +000087 "'\n";
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +000088
Chris Lattnerea2d52d2008-01-06 01:35:39 +000089 // If we have MIOpInfo, then we have #operands equal to number of entries
90 // in MIOperandInfo.
91 if (unsigned NumArgs = MIOpInfo->getNumArgs())
92 NumOps = NumArgs;
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +000093
Chris Lattnerea2d52d2008-01-06 01:35:39 +000094 if (Rec->isSubClassOf("PredicateOperand"))
95 isPredicable = true;
96 else if (Rec->isSubClassOf("OptionalDefOperand"))
97 hasOptionalDef = true;
98 } else if (Rec->getName() == "variable_ops") {
Chris Lattnerf376c992008-01-07 05:19:29 +000099 isVariadic = true;
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000100 continue;
Benjamin Kramerc8dc46b2011-07-14 21:47:18 +0000101 } else if (Rec->isSubClassOf("RegisterClass")) {
102 OperandType = "OPERAND_REGISTER";
103 } else if (!Rec->isSubClassOf("PointerLikeRegClass") &&
NAKAMURA Takumi31aafbd2011-01-26 02:03:48 +0000104 Rec->getName() != "unknown")
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000105 throw "Unknown operand class '" + Rec->getName() +
Chris Lattnerd8adec72010-11-01 04:03:32 +0000106 "' in '" + R->getName() + "' instruction!";
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000107
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000108 // Check that the operand has a name and that it's unique.
Chris Lattner1f22d272010-03-18 21:07:39 +0000109 if (ArgName.empty())
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000110 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
Chris Lattnerd8adec72010-11-01 04:03:32 +0000111 " has no name!";
Chris Lattner1f22d272010-03-18 21:07:39 +0000112 if (!OperandNames.insert(ArgName).second)
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000113 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
Chris Lattnerd8adec72010-11-01 04:03:32 +0000114 " has the same name as a previous operand!";
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000115
Jim Grosbach51a12eb2010-10-12 22:21:57 +0000116 OperandList.push_back(OperandInfo(Rec, ArgName, PrintMethod, EncoderMethod,
Benjamin Kramerc8dc46b2011-07-14 21:47:18 +0000117 OperandType, MIOperandNo, NumOps,
118 MIOpInfo));
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000119 MIOperandNo += NumOps;
120 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000121
122
Chris Lattnerdd3b09c2010-11-01 05:34:34 +0000123 // Make sure the constraints list for each operand is large enough to hold
124 // constraint info, even if none is present.
125 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
126 OperandList[i].Constraints.resize(OperandList[i].MINumOperands);
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000127}
128
Chris Lattnerd8adec72010-11-01 04:03:32 +0000129
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000130/// getOperandNamed - Return the index of the operand with the specified
131/// non-empty name. If the instruction does not have an operand with the
132/// specified name, throw an exception.
133///
Chris Lattnerd8adec72010-11-01 04:03:32 +0000134unsigned CGIOperandList::getOperandNamed(StringRef Name) const {
Jim Grosbach191ad7c2010-10-11 18:25:51 +0000135 unsigned OpIdx;
136 if (hasOperandNamed(Name, OpIdx)) return OpIdx;
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000137 throw "'" + TheDef->getName() + "' does not have an operand named '$" +
Chris Lattnerd8adec72010-11-01 04:03:32 +0000138 Name.str() + "'!";
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000139}
140
Jim Grosbach191ad7c2010-10-11 18:25:51 +0000141/// hasOperandNamed - Query whether the instruction has an operand of the
142/// given name. If so, return true and set OpIdx to the index of the
143/// operand. Otherwise, return false.
Chris Lattnerd8adec72010-11-01 04:03:32 +0000144bool CGIOperandList::hasOperandNamed(StringRef Name, unsigned &OpIdx) const {
Jim Grosbach191ad7c2010-10-11 18:25:51 +0000145 assert(!Name.empty() && "Cannot search for operand with no name!");
146 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
147 if (OperandList[i].Name == Name) {
148 OpIdx = i;
149 return true;
150 }
151 return false;
152}
153
Jim Grosbach38c4f4b2009-12-15 19:28:13 +0000154std::pair<unsigned,unsigned>
Chris Lattnerd8adec72010-11-01 04:03:32 +0000155CGIOperandList::ParseOperandName(const std::string &Op, bool AllowWholeOp) {
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000156 if (Op.empty() || Op[0] != '$')
157 throw TheDef->getName() + ": Illegal operand name: '" + Op + "'";
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000158
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000159 std::string OpName = Op.substr(1);
160 std::string SubOpName;
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000161
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000162 // Check to see if this is $foo.bar.
163 std::string::size_type DotIdx = OpName.find_first_of(".");
164 if (DotIdx != std::string::npos) {
165 SubOpName = OpName.substr(DotIdx+1);
166 if (SubOpName.empty())
167 throw TheDef->getName() + ": illegal empty suboperand name in '" +Op +"'";
168 OpName = OpName.substr(0, DotIdx);
169 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000170
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000171 unsigned OpIdx = getOperandNamed(OpName);
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000172
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000173 if (SubOpName.empty()) { // If no suboperand name was specified:
174 // If one was needed, throw.
175 if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp &&
176 SubOpName.empty())
177 throw TheDef->getName() + ": Illegal to refer to"
Chris Lattnerd8adec72010-11-01 04:03:32 +0000178 " whole operand part of complex operand '" + Op + "'";
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000179
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000180 // Otherwise, return the operand.
181 return std::make_pair(OpIdx, 0U);
182 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000183
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000184 // Find the suboperand number involved.
Eric Christopher71520a82011-07-11 23:06:52 +0000185 DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000186 if (MIOpInfo == 0)
187 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000188
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000189 // Find the operand with the right name.
190 for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i)
191 if (MIOpInfo->getArgName(i) == SubOpName)
192 return std::make_pair(OpIdx, i);
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000193
Chris Lattnerea2d52d2008-01-06 01:35:39 +0000194 // Otherwise, didn't find it!
195 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
196}
Chris Lattner7bc5d9b2010-03-27 20:09:24 +0000197
Chris Lattnerd8adec72010-11-01 04:03:32 +0000198static void ParseConstraint(const std::string &CStr, CGIOperandList &Ops) {
199 // EARLY_CLOBBER: @early $reg
200 std::string::size_type wpos = CStr.find_first_of(" \t");
201 std::string::size_type start = CStr.find_first_not_of(" \t");
202 std::string Tok = CStr.substr(start, wpos - start);
203 if (Tok == "@earlyclobber") {
204 std::string Name = CStr.substr(wpos+1);
205 wpos = Name.find_first_not_of(" \t");
206 if (wpos == std::string::npos)
207 throw "Illegal format for @earlyclobber constraint: '" + CStr + "'";
208 Name = Name.substr(wpos);
209 std::pair<unsigned,unsigned> Op = Ops.ParseOperandName(Name, false);
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000210
Chris Lattnerd8adec72010-11-01 04:03:32 +0000211 // Build the string for the operand
212 if (!Ops[Op.first].Constraints[Op.second].isNone())
213 throw "Operand '" + Name + "' cannot have multiple constraints!";
214 Ops[Op.first].Constraints[Op.second] =
215 CGIOperandList::ConstraintInfo::getEarlyClobber();
216 return;
217 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000218
Chris Lattnerd8adec72010-11-01 04:03:32 +0000219 // Only other constraint is "TIED_TO" for now.
220 std::string::size_type pos = CStr.find_first_of('=');
221 assert(pos != std::string::npos && "Unrecognized constraint");
222 start = CStr.find_first_not_of(" \t");
223 std::string Name = CStr.substr(start, pos - start);
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000224
Chris Lattnerd8adec72010-11-01 04:03:32 +0000225 // TIED_TO: $src1 = $dst
226 wpos = Name.find_first_of(" \t");
227 if (wpos == std::string::npos)
228 throw "Illegal format for tied-to constraint: '" + CStr + "'";
229 std::string DestOpName = Name.substr(0, wpos);
230 std::pair<unsigned,unsigned> DestOp = Ops.ParseOperandName(DestOpName, false);
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000231
Chris Lattnerd8adec72010-11-01 04:03:32 +0000232 Name = CStr.substr(pos+1);
233 wpos = Name.find_first_not_of(" \t");
234 if (wpos == std::string::npos)
235 throw "Illegal format for tied-to constraint: '" + CStr + "'";
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000236
Chris Lattnerd8adec72010-11-01 04:03:32 +0000237 std::pair<unsigned,unsigned> SrcOp =
238 Ops.ParseOperandName(Name.substr(wpos), false);
239 if (SrcOp > DestOp)
240 throw "Illegal tied-to operand constraint '" + CStr + "'";
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000241
242
Chris Lattnerd8adec72010-11-01 04:03:32 +0000243 unsigned FlatOpNo = Ops.getFlattenedOperandNumber(SrcOp);
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000244
Chris Lattnerd8adec72010-11-01 04:03:32 +0000245 if (!Ops[DestOp.first].Constraints[DestOp.second].isNone())
246 throw "Operand '" + DestOpName + "' cannot have multiple constraints!";
247 Ops[DestOp.first].Constraints[DestOp.second] =
248 CGIOperandList::ConstraintInfo::getTied(FlatOpNo);
249}
250
251static void ParseConstraints(const std::string &CStr, CGIOperandList &Ops) {
Chris Lattnerd8adec72010-11-01 04:03:32 +0000252 if (CStr.empty()) return;
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000253
Chris Lattnerd8adec72010-11-01 04:03:32 +0000254 const std::string delims(",");
255 std::string::size_type bidx, eidx;
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000256
Chris Lattnerd8adec72010-11-01 04:03:32 +0000257 bidx = CStr.find_first_not_of(delims);
258 while (bidx != std::string::npos) {
259 eidx = CStr.find_first_of(delims, bidx);
260 if (eidx == std::string::npos)
261 eidx = CStr.length();
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000262
Chris Lattnerd8adec72010-11-01 04:03:32 +0000263 ParseConstraint(CStr.substr(bidx, eidx - bidx), Ops);
264 bidx = CStr.find_first_not_of(delims, eidx);
265 }
266}
267
268void CGIOperandList::ProcessDisableEncoding(std::string DisableEncoding) {
269 while (1) {
270 std::string OpName;
271 tie(OpName, DisableEncoding) = getToken(DisableEncoding, " ,\t");
272 if (OpName.empty()) break;
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000273
Chris Lattnerd8adec72010-11-01 04:03:32 +0000274 // Figure out which operand this is.
275 std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false);
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000276
Chris Lattnerd8adec72010-11-01 04:03:32 +0000277 // Mark the operand as not-to-be encoded.
278 if (Op.second >= OperandList[Op.first].DoNotEncode.size())
279 OperandList[Op.first].DoNotEncode.resize(Op.second+1);
280 OperandList[Op.first].DoNotEncode[Op.second] = true;
281 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000282
Chris Lattnerd8adec72010-11-01 04:03:32 +0000283}
284
285//===----------------------------------------------------------------------===//
286// CodeGenInstruction Implementation
287//===----------------------------------------------------------------------===//
288
289CodeGenInstruction::CodeGenInstruction(Record *R) : TheDef(R), Operands(R) {
290 Namespace = R->getValueAsString("Namespace");
291 AsmString = R->getValueAsString("AsmString");
292
293 isReturn = R->getValueAsBit("isReturn");
294 isBranch = R->getValueAsBit("isBranch");
295 isIndirectBranch = R->getValueAsBit("isIndirectBranch");
296 isCompare = R->getValueAsBit("isCompare");
Evan Cheng7f8ab6e2010-11-17 20:13:28 +0000297 isMoveImm = R->getValueAsBit("isMoveImm");
Evan Cheng880e299d2011-03-15 05:09:26 +0000298 isBitcast = R->getValueAsBit("isBitcast");
Chris Lattnerd8adec72010-11-01 04:03:32 +0000299 isBarrier = R->getValueAsBit("isBarrier");
300 isCall = R->getValueAsBit("isCall");
301 canFoldAsLoad = R->getValueAsBit("canFoldAsLoad");
302 mayLoad = R->getValueAsBit("mayLoad");
303 mayStore = R->getValueAsBit("mayStore");
304 isPredicable = Operands.isPredicable || R->getValueAsBit("isPredicable");
305 isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
306 isCommutable = R->getValueAsBit("isCommutable");
307 isTerminator = R->getValueAsBit("isTerminator");
308 isReMaterializable = R->getValueAsBit("isReMaterializable");
309 hasDelaySlot = R->getValueAsBit("hasDelaySlot");
310 usesCustomInserter = R->getValueAsBit("usesCustomInserter");
311 hasCtrlDep = R->getValueAsBit("hasCtrlDep");
312 isNotDuplicable = R->getValueAsBit("isNotDuplicable");
313 hasSideEffects = R->getValueAsBit("hasSideEffects");
314 neverHasSideEffects = R->getValueAsBit("neverHasSideEffects");
315 isAsCheapAsAMove = R->getValueAsBit("isAsCheapAsAMove");
316 hasExtraSrcRegAllocReq = R->getValueAsBit("hasExtraSrcRegAllocReq");
317 hasExtraDefRegAllocReq = R->getValueAsBit("hasExtraDefRegAllocReq");
Jim Grosbach4c08a9f2011-07-07 00:48:02 +0000318 isCodeGenOnly = R->getValueAsBit("isCodeGenOnly");
Jim Grosbachf3fd36e2011-07-06 21:33:38 +0000319 isPseudo = R->getValueAsBit("isPseudo");
Chris Lattnerd8adec72010-11-01 04:03:32 +0000320 ImplicitDefs = R->getValueAsListOfDefs("Defs");
321 ImplicitUses = R->getValueAsListOfDefs("Uses");
322
323 if (neverHasSideEffects + hasSideEffects > 1)
324 throw R->getName() + ": multiple conflicting side-effect flags set!";
325
326 // Parse Constraints.
327 ParseConstraints(R->getValueAsString("Constraints"), Operands);
328
329 // Parse the DisableEncoding field.
330 Operands.ProcessDisableEncoding(R->getValueAsString("DisableEncoding"));
331}
Chris Lattner7bc5d9b2010-03-27 20:09:24 +0000332
333/// HasOneImplicitDefWithKnownVT - If the instruction has at least one
334/// implicit def and it has a known VT, return the VT, otherwise return
335/// MVT::Other.
336MVT::SimpleValueType CodeGenInstruction::
337HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const {
338 if (ImplicitDefs.empty()) return MVT::Other;
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000339
Chris Lattner7bc5d9b2010-03-27 20:09:24 +0000340 // Check to see if the first implicit def has a resolvable type.
341 Record *FirstImplicitDef = ImplicitDefs[0];
342 assert(FirstImplicitDef->isSubClassOf("Register"));
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000343 const std::vector<MVT::SimpleValueType> &RegVTs =
Chris Lattner7bc5d9b2010-03-27 20:09:24 +0000344 TargetInfo.getRegisterVTs(FirstImplicitDef);
345 if (RegVTs.size() == 1)
346 return RegVTs[0];
347 return MVT::Other;
348}
349
Chris Lattner25d9c7f2010-11-01 01:07:14 +0000350
351/// FlattenAsmStringVariants - Flatten the specified AsmString to only
352/// include text from the specified variant, returning the new string.
353std::string CodeGenInstruction::
354FlattenAsmStringVariants(StringRef Cur, unsigned Variant) {
355 std::string Res = "";
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000356
Chris Lattner25d9c7f2010-11-01 01:07:14 +0000357 for (;;) {
358 // Find the start of the next variant string.
359 size_t VariantsStart = 0;
360 for (size_t e = Cur.size(); VariantsStart != e; ++VariantsStart)
361 if (Cur[VariantsStart] == '{' &&
362 (VariantsStart == 0 || (Cur[VariantsStart-1] != '$' &&
363 Cur[VariantsStart-1] != '\\')))
364 break;
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000365
Chris Lattner25d9c7f2010-11-01 01:07:14 +0000366 // Add the prefix to the result.
367 Res += Cur.slice(0, VariantsStart);
368 if (VariantsStart == Cur.size())
369 break;
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000370
Chris Lattner25d9c7f2010-11-01 01:07:14 +0000371 ++VariantsStart; // Skip the '{'.
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000372
Chris Lattner25d9c7f2010-11-01 01:07:14 +0000373 // Scan to the end of the variants string.
374 size_t VariantsEnd = VariantsStart;
375 unsigned NestedBraces = 1;
376 for (size_t e = Cur.size(); VariantsEnd != e; ++VariantsEnd) {
377 if (Cur[VariantsEnd] == '}' && Cur[VariantsEnd-1] != '\\') {
378 if (--NestedBraces == 0)
379 break;
380 } else if (Cur[VariantsEnd] == '{')
381 ++NestedBraces;
382 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000383
Chris Lattner25d9c7f2010-11-01 01:07:14 +0000384 // Select the Nth variant (or empty).
385 StringRef Selection = Cur.slice(VariantsStart, VariantsEnd);
386 for (unsigned i = 0; i != Variant; ++i)
387 Selection = Selection.split('|').second;
388 Res += Selection.split('|').first;
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000389
Chris Lattner25d9c7f2010-11-01 01:07:14 +0000390 assert(VariantsEnd != Cur.size() &&
391 "Unterminated variants in assembly string!");
392 Cur = Cur.substr(VariantsEnd + 1);
393 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000394
Chris Lattner25d9c7f2010-11-01 01:07:14 +0000395 return Res;
396}
397
Chris Lattner488c2012010-11-01 04:05:41 +0000398
399//===----------------------------------------------------------------------===//
400/// CodeGenInstAlias Implementation
401//===----------------------------------------------------------------------===//
402
Bob Wilsonb9b24222011-01-26 19:44:55 +0000403/// tryAliasOpMatch - This is a helper function for the CodeGenInstAlias
404/// constructor. It checks if an argument in an InstAlias pattern matches
405/// the corresponding operand of the instruction. It returns true on a
406/// successful match, with ResOp set to the result operand to be used.
Eric Christopher71520a82011-07-11 23:06:52 +0000407bool CodeGenInstAlias::tryAliasOpMatch(DagInit *Result, unsigned AliasOpNo,
Bob Wilsonb9b24222011-01-26 19:44:55 +0000408 Record *InstOpRec, bool hasSubOps,
409 SMLoc Loc, CodeGenTarget &T,
410 ResultOperand &ResOp) {
Eric Christopher71520a82011-07-11 23:06:52 +0000411 Init *Arg = Result->getArg(AliasOpNo);
412 DefInit *ADI = dynamic_cast<DefInit*>(Arg);
Bob Wilsonb9b24222011-01-26 19:44:55 +0000413
414 if (ADI && ADI->getDef() == InstOpRec) {
415 // If the operand is a record, it must have a name, and the record type
416 // must match up with the instruction's argument type.
417 if (Result->getArgName(AliasOpNo).empty())
418 throw TGError(Loc, "result argument #" + utostr(AliasOpNo) +
419 " must have a name!");
420 ResOp = ResultOperand(Result->getArgName(AliasOpNo), ADI->getDef());
421 return true;
422 }
423
424 // Handle explicit registers.
425 if (ADI && ADI->getDef()->isSubClassOf("Register")) {
Owen Andersona84be6c2011-06-27 21:06:21 +0000426 if (InstOpRec->isSubClassOf("RegisterOperand"))
427 InstOpRec = InstOpRec->getValueAsDef("RegClass");
428
Bob Wilsonb9b24222011-01-26 19:44:55 +0000429 if (!InstOpRec->isSubClassOf("RegisterClass"))
430 return false;
431
Jakob Stoklund Olesend7bc5c22011-06-15 04:50:36 +0000432 if (!T.getRegisterClass(InstOpRec)
433 .contains(T.getRegBank().getReg(ADI->getDef())))
Bob Wilsonb9b24222011-01-26 19:44:55 +0000434 throw TGError(Loc, "fixed register " +ADI->getDef()->getName()
435 + " is not a member of the " + InstOpRec->getName() +
436 " register class!");
437
438 if (!Result->getArgName(AliasOpNo).empty())
439 throw TGError(Loc, "result fixed register argument must "
440 "not have a name!");
441
442 ResOp = ResultOperand(ADI->getDef());
443 return true;
444 }
445
446 // Handle "zero_reg" for optional def operands.
447 if (ADI && ADI->getDef()->getName() == "zero_reg") {
448
449 // Check if this is an optional def.
450 if (!InstOpRec->isSubClassOf("OptionalDefOperand"))
451 throw TGError(Loc, "reg0 used for result that is not an "
452 "OptionalDefOperand!");
453
454 ResOp = ResultOperand(static_cast<Record*>(0));
455 return true;
456 }
457
Eric Christopher71520a82011-07-11 23:06:52 +0000458 if (IntInit *II = dynamic_cast<IntInit*>(Arg)) {
Bob Wilsonb9b24222011-01-26 19:44:55 +0000459 if (hasSubOps || !InstOpRec->isSubClassOf("Operand"))
460 return false;
461 // Integer arguments can't have names.
462 if (!Result->getArgName(AliasOpNo).empty())
463 throw TGError(Loc, "result argument #" + utostr(AliasOpNo) +
464 " must not have a name!");
465 ResOp = ResultOperand(II->getValue());
466 return true;
467 }
468
469 return false;
470}
471
Chris Lattnerfecdad62010-11-06 07:14:44 +0000472CodeGenInstAlias::CodeGenInstAlias(Record *R, CodeGenTarget &T) : TheDef(R) {
Chris Lattner488c2012010-11-01 04:05:41 +0000473 AsmString = R->getValueAsString("AsmString");
Chris Lattnerdd3b09c2010-11-01 05:34:34 +0000474 Result = R->getValueAsDag("ResultInst");
Chris Lattner8ffd1292010-11-06 06:39:47 +0000475
476 // Verify that the root of the result is an instruction.
Eric Christopher71520a82011-07-11 23:06:52 +0000477 DefInit *DI = dynamic_cast<DefInit*>(Result->getOperator());
Chris Lattner8ffd1292010-11-06 06:39:47 +0000478 if (DI == 0 || !DI->getDef()->isSubClassOf("Instruction"))
479 throw TGError(R->getLoc(), "result of inst alias should be an instruction");
480
481 ResultInst = &T.getInstruction(DI->getDef());
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000482
Chris Lattnerb625dd22010-11-06 07:06:09 +0000483 // NameClass - If argument names are repeated, we need to verify they have
484 // the same class.
485 StringMap<Record*> NameClass;
Bob Wilsona48eacc2011-01-20 18:38:10 +0000486 for (unsigned i = 0, e = Result->getNumArgs(); i != e; ++i) {
Eric Christopher71520a82011-07-11 23:06:52 +0000487 DefInit *ADI = dynamic_cast<DefInit*>(Result->getArg(i));
Bob Wilsona48eacc2011-01-20 18:38:10 +0000488 if (!ADI || Result->getArgName(i).empty())
489 continue;
490 // Verify we don't have something like: (someinst GR16:$foo, GR32:$foo)
491 // $foo can exist multiple times in the result list, but it must have the
492 // same type.
493 Record *&Entry = NameClass[Result->getArgName(i)];
494 if (Entry && Entry != ADI->getDef())
495 throw TGError(R->getLoc(), "result value $" + Result->getArgName(i) +
496 " is both " + Entry->getName() + " and " +
497 ADI->getDef()->getName() + "!");
498 Entry = ADI->getDef();
499 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000500
Chris Lattner76030522010-11-06 06:54:38 +0000501 // Decode and validate the arguments of the result.
Chris Lattner8188fb22010-11-06 07:31:43 +0000502 unsigned AliasOpNo = 0;
503 for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) {
Bob Wilsonb9b24222011-01-26 19:44:55 +0000504
Chris Lattner8188fb22010-11-06 07:31:43 +0000505 // Tied registers don't have an entry in the result dag.
506 if (ResultInst->Operands[i].getTiedRegister() != -1)
507 continue;
508
509 if (AliasOpNo >= Result->getNumArgs())
Bob Wilsonb9b24222011-01-26 19:44:55 +0000510 throw TGError(R->getLoc(), "not enough arguments for instruction!");
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000511
Bob Wilsonb9b24222011-01-26 19:44:55 +0000512 Record *InstOpRec = ResultInst->Operands[i].Rec;
513 unsigned NumSubOps = ResultInst->Operands[i].MINumOperands;
514 ResultOperand ResOp(static_cast<int64_t>(0));
515 if (tryAliasOpMatch(Result, AliasOpNo, InstOpRec, (NumSubOps > 1),
516 R->getLoc(), T, ResOp)) {
517 ResultOperands.push_back(ResOp);
518 ResultInstOperandIndex.push_back(std::make_pair(i, -1));
Chris Lattner8188fb22010-11-06 07:31:43 +0000519 ++AliasOpNo;
Chris Lattner76030522010-11-06 06:54:38 +0000520 continue;
521 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000522
Bob Wilsonb9b24222011-01-26 19:44:55 +0000523 // If the argument did not match the instruction operand, and the operand
524 // is composed of multiple suboperands, try matching the suboperands.
525 if (NumSubOps > 1) {
Eric Christopher71520a82011-07-11 23:06:52 +0000526 DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo;
Bob Wilsonb9b24222011-01-26 19:44:55 +0000527 for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) {
528 if (AliasOpNo >= Result->getNumArgs())
529 throw TGError(R->getLoc(), "not enough arguments for instruction!");
Eric Christopher71520a82011-07-11 23:06:52 +0000530 Record *SubRec = dynamic_cast<DefInit*>(MIOI->getArg(SubOp))->getDef();
Bob Wilsonb9b24222011-01-26 19:44:55 +0000531 if (tryAliasOpMatch(Result, AliasOpNo, SubRec, false,
532 R->getLoc(), T, ResOp)) {
533 ResultOperands.push_back(ResOp);
534 ResultInstOperandIndex.push_back(std::make_pair(i, SubOp));
535 ++AliasOpNo;
536 } else {
537 throw TGError(R->getLoc(), "result argument #" + utostr(AliasOpNo) +
538 " does not match instruction operand class " +
539 (SubOp == 0 ? InstOpRec->getName() :SubRec->getName()));
540 }
541 }
Chris Lattnerb6f8e822010-11-06 19:25:43 +0000542 continue;
543 }
Bob Wilsonb9b24222011-01-26 19:44:55 +0000544 throw TGError(R->getLoc(), "result argument #" + utostr(AliasOpNo) +
545 " does not match instruction operand class " +
546 InstOpRec->getName());
Chris Lattner76030522010-11-06 06:54:38 +0000547 }
NAKAMURA Takumi9d29eff2011-01-26 02:03:37 +0000548
Chris Lattner8188fb22010-11-06 07:31:43 +0000549 if (AliasOpNo != Result->getNumArgs())
Bob Wilsonb9b24222011-01-26 19:44:55 +0000550 throw TGError(R->getLoc(), "too many operands for instruction!");
Chris Lattner488c2012010-11-01 04:05:41 +0000551}