1 //===- CodeGenInstruction.cpp - CodeGen Instruction Class Wrapper ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the CodeGenInstruction class.
12 //===----------------------------------------------------------------------===//
14 #include "CodeGenInstruction.h"
15 #include "CodeGenTarget.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/TableGen/Error.h"
20 #include "llvm/TableGen/Record.h"
24 //===----------------------------------------------------------------------===//
25 // CGIOperandList Implementation
26 //===----------------------------------------------------------------------===//
28 CGIOperandList::CGIOperandList(Record *R) : TheDef(R) {
30 hasOptionalDef = false;
33 DagInit *OutDI = R->getValueAsDag("OutOperandList");
35 if (DefInit *Init = dyn_cast<DefInit>(OutDI->getOperator())) {
36 if (Init->getDef()->getName() != "outs")
37 PrintFatalError(R->getName() + ": invalid def name for output list: use 'outs'");
39 PrintFatalError(R->getName() + ": invalid output list: use 'outs'");
41 NumDefs = OutDI->getNumArgs();
43 DagInit *InDI = R->getValueAsDag("InOperandList");
44 if (DefInit *Init = dyn_cast<DefInit>(InDI->getOperator())) {
45 if (Init->getDef()->getName() != "ins")
46 PrintFatalError(R->getName() + ": invalid def name for input list: use 'ins'");
48 PrintFatalError(R->getName() + ": invalid input list: use 'ins'");
50 unsigned MIOperandNo = 0;
51 std::set<std::string> OperandNames;
52 unsigned e = InDI->getNumArgs() + OutDI->getNumArgs();
53 OperandList.reserve(e);
54 for (unsigned i = 0; i != e; ++i){
58 ArgInit = OutDI->getArg(i);
59 ArgName = OutDI->getArgNameStr(i);
61 ArgInit = InDI->getArg(i-NumDefs);
62 ArgName = InDI->getArgNameStr(i-NumDefs);
65 DefInit *Arg = dyn_cast<DefInit>(ArgInit);
67 PrintFatalError("Illegal operand for the '" + R->getName() + "' instruction!");
69 Record *Rec = Arg->getDef();
70 std::string PrintMethod = "printOperand";
71 std::string EncoderMethod;
72 std::string OperandType = "OPERAND_UNKNOWN";
73 std::string OperandNamespace = "MCOI";
75 DagInit *MIOpInfo = nullptr;
76 if (Rec->isSubClassOf("RegisterOperand")) {
77 PrintMethod = Rec->getValueAsString("PrintMethod");
78 OperandType = Rec->getValueAsString("OperandType");
79 OperandNamespace = Rec->getValueAsString("OperandNamespace");
80 EncoderMethod = Rec->getValueAsString("EncoderMethod");
81 } else if (Rec->isSubClassOf("Operand")) {
82 PrintMethod = Rec->getValueAsString("PrintMethod");
83 OperandType = Rec->getValueAsString("OperandType");
84 OperandNamespace = Rec->getValueAsString("OperandNamespace");
85 // If there is an explicit encoder method, use it.
86 EncoderMethod = Rec->getValueAsString("EncoderMethod");
87 MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
89 // Verify that MIOpInfo has an 'ops' root value.
90 if (!isa<DefInit>(MIOpInfo->getOperator()) ||
91 cast<DefInit>(MIOpInfo->getOperator())->getDef()->getName() != "ops")
92 PrintFatalError("Bad value for MIOperandInfo in operand '" + Rec->getName() +
95 // If we have MIOpInfo, then we have #operands equal to number of entries
97 if (unsigned NumArgs = MIOpInfo->getNumArgs())
100 if (Rec->isSubClassOf("PredicateOp"))
102 else if (Rec->isSubClassOf("OptionalDefOperand"))
103 hasOptionalDef = true;
104 } else if (Rec->getName() == "variable_ops") {
107 } else if (Rec->isSubClassOf("RegisterClass")) {
108 OperandType = "OPERAND_REGISTER";
109 } else if (!Rec->isSubClassOf("PointerLikeRegClass") &&
110 !Rec->isSubClassOf("unknown_class"))
111 PrintFatalError("Unknown operand class '" + Rec->getName() +
112 "' in '" + R->getName() + "' instruction!");
114 // Check that the operand has a name and that it's unique.
116 PrintFatalError("In instruction '" + R->getName() + "', operand #" +
117 Twine(i) + " has no name!");
118 if (!OperandNames.insert(ArgName).second)
119 PrintFatalError("In instruction '" + R->getName() + "', operand #" +
120 Twine(i) + " has the same name as a previous operand!");
122 OperandList.emplace_back(Rec, ArgName, PrintMethod, EncoderMethod,
123 OperandNamespace + "::" + OperandType, MIOperandNo,
125 MIOperandNo += NumOps;
129 // Make sure the constraints list for each operand is large enough to hold
130 // constraint info, even if none is present.
131 for (OperandInfo &OpInfo : OperandList)
132 OpInfo.Constraints.resize(OpInfo.MINumOperands);
136 /// getOperandNamed - Return the index of the operand with the specified
137 /// non-empty name. If the instruction does not have an operand with the
138 /// specified name, abort.
140 unsigned CGIOperandList::getOperandNamed(StringRef Name) const {
142 if (hasOperandNamed(Name, OpIdx)) return OpIdx;
143 PrintFatalError("'" + TheDef->getName() +
144 "' does not have an operand named '$" + Name + "'!");
147 /// hasOperandNamed - Query whether the instruction has an operand of the
148 /// given name. If so, return true and set OpIdx to the index of the
149 /// operand. Otherwise, return false.
150 bool CGIOperandList::hasOperandNamed(StringRef Name, unsigned &OpIdx) const {
151 assert(!Name.empty() && "Cannot search for operand with no name!");
152 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
153 if (OperandList[i].Name == Name) {
160 std::pair<unsigned,unsigned>
161 CGIOperandList::ParseOperandName(const std::string &Op, bool AllowWholeOp) {
162 if (Op.empty() || Op[0] != '$')
163 PrintFatalError(TheDef->getName() + ": Illegal operand name: '" + Op + "'");
165 std::string OpName = Op.substr(1);
166 std::string SubOpName;
168 // Check to see if this is $foo.bar.
169 std::string::size_type DotIdx = OpName.find_first_of('.');
170 if (DotIdx != std::string::npos) {
171 SubOpName = OpName.substr(DotIdx+1);
172 if (SubOpName.empty())
173 PrintFatalError(TheDef->getName() + ": illegal empty suboperand name in '" +Op +"'");
174 OpName = OpName.substr(0, DotIdx);
177 unsigned OpIdx = getOperandNamed(OpName);
179 if (SubOpName.empty()) { // If no suboperand name was specified:
180 // If one was needed, throw.
181 if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp &&
183 PrintFatalError(TheDef->getName() + ": Illegal to refer to"
184 " whole operand part of complex operand '" + Op + "'");
186 // Otherwise, return the operand.
187 return std::make_pair(OpIdx, 0U);
190 // Find the suboperand number involved.
191 DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
193 PrintFatalError(TheDef->getName() + ": unknown suboperand name in '" + Op + "'");
195 // Find the operand with the right name.
196 for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i)
197 if (MIOpInfo->getArgNameStr(i) == SubOpName)
198 return std::make_pair(OpIdx, i);
200 // Otherwise, didn't find it!
201 PrintFatalError(TheDef->getName() + ": unknown suboperand name in '" + Op + "'");
202 return std::make_pair(0U, 0U);
205 static void ParseConstraint(const std::string &CStr, CGIOperandList &Ops) {
206 // EARLY_CLOBBER: @early $reg
207 std::string::size_type wpos = CStr.find_first_of(" \t");
208 std::string::size_type start = CStr.find_first_not_of(" \t");
209 std::string Tok = CStr.substr(start, wpos - start);
210 if (Tok == "@earlyclobber") {
211 std::string Name = CStr.substr(wpos+1);
212 wpos = Name.find_first_not_of(" \t");
213 if (wpos == std::string::npos)
214 PrintFatalError("Illegal format for @earlyclobber constraint: '" + CStr + "'");
215 Name = Name.substr(wpos);
216 std::pair<unsigned,unsigned> Op = Ops.ParseOperandName(Name, false);
218 // Build the string for the operand
219 if (!Ops[Op.first].Constraints[Op.second].isNone())
220 PrintFatalError("Operand '" + Name + "' cannot have multiple constraints!");
221 Ops[Op.first].Constraints[Op.second] =
222 CGIOperandList::ConstraintInfo::getEarlyClobber();
226 // Only other constraint is "TIED_TO" for now.
227 std::string::size_type pos = CStr.find_first_of('=');
228 assert(pos != std::string::npos && "Unrecognized constraint");
229 start = CStr.find_first_not_of(" \t");
230 std::string Name = CStr.substr(start, pos - start);
232 // TIED_TO: $src1 = $dst
233 wpos = Name.find_first_of(" \t");
234 if (wpos == std::string::npos)
235 PrintFatalError("Illegal format for tied-to constraint: '" + CStr + "'");
236 std::string DestOpName = Name.substr(0, wpos);
237 std::pair<unsigned,unsigned> DestOp = Ops.ParseOperandName(DestOpName, false);
239 Name = CStr.substr(pos+1);
240 wpos = Name.find_first_not_of(" \t");
241 if (wpos == std::string::npos)
242 PrintFatalError("Illegal format for tied-to constraint: '" + CStr + "'");
244 std::string SrcOpName = Name.substr(wpos);
245 std::pair<unsigned,unsigned> SrcOp = Ops.ParseOperandName(SrcOpName, false);
246 if (SrcOp > DestOp) {
247 std::swap(SrcOp, DestOp);
248 std::swap(SrcOpName, DestOpName);
251 unsigned FlatOpNo = Ops.getFlattenedOperandNumber(SrcOp);
253 if (!Ops[DestOp.first].Constraints[DestOp.second].isNone())
254 PrintFatalError("Operand '" + DestOpName +
255 "' cannot have multiple constraints!");
256 Ops[DestOp.first].Constraints[DestOp.second] =
257 CGIOperandList::ConstraintInfo::getTied(FlatOpNo);
260 static void ParseConstraints(const std::string &CStr, CGIOperandList &Ops) {
261 if (CStr.empty()) return;
263 const std::string delims(",");
264 std::string::size_type bidx, eidx;
266 bidx = CStr.find_first_not_of(delims);
267 while (bidx != std::string::npos) {
268 eidx = CStr.find_first_of(delims, bidx);
269 if (eidx == std::string::npos)
270 eidx = CStr.length();
272 ParseConstraint(CStr.substr(bidx, eidx - bidx), Ops);
273 bidx = CStr.find_first_not_of(delims, eidx);
277 void CGIOperandList::ProcessDisableEncoding(std::string DisableEncoding) {
279 std::pair<StringRef, StringRef> P = getToken(DisableEncoding, " ,\t");
280 std::string OpName = P.first;
281 DisableEncoding = P.second;
282 if (OpName.empty()) break;
284 // Figure out which operand this is.
285 std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false);
287 // Mark the operand as not-to-be encoded.
288 if (Op.second >= OperandList[Op.first].DoNotEncode.size())
289 OperandList[Op.first].DoNotEncode.resize(Op.second+1);
290 OperandList[Op.first].DoNotEncode[Op.second] = true;
295 //===----------------------------------------------------------------------===//
296 // CodeGenInstruction Implementation
297 //===----------------------------------------------------------------------===//
299 CodeGenInstruction::CodeGenInstruction(Record *R)
300 : TheDef(R), Operands(R), InferredFrom(nullptr) {
301 Namespace = R->getValueAsString("Namespace");
302 AsmString = R->getValueAsString("AsmString");
304 isReturn = R->getValueAsBit("isReturn");
305 isBranch = R->getValueAsBit("isBranch");
306 isIndirectBranch = R->getValueAsBit("isIndirectBranch");
307 isCompare = R->getValueAsBit("isCompare");
308 isMoveImm = R->getValueAsBit("isMoveImm");
309 isMoveReg = R->getValueAsBit("isMoveReg");
310 isBitcast = R->getValueAsBit("isBitcast");
311 isSelect = R->getValueAsBit("isSelect");
312 isBarrier = R->getValueAsBit("isBarrier");
313 isCall = R->getValueAsBit("isCall");
314 isAdd = R->getValueAsBit("isAdd");
315 canFoldAsLoad = R->getValueAsBit("canFoldAsLoad");
316 isPredicable = Operands.isPredicable || R->getValueAsBit("isPredicable");
317 isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
318 isCommutable = R->getValueAsBit("isCommutable");
319 isTerminator = R->getValueAsBit("isTerminator");
320 isReMaterializable = R->getValueAsBit("isReMaterializable");
321 hasDelaySlot = R->getValueAsBit("hasDelaySlot");
322 usesCustomInserter = R->getValueAsBit("usesCustomInserter");
323 hasPostISelHook = R->getValueAsBit("hasPostISelHook");
324 hasCtrlDep = R->getValueAsBit("hasCtrlDep");
325 isNotDuplicable = R->getValueAsBit("isNotDuplicable");
326 isRegSequence = R->getValueAsBit("isRegSequence");
327 isExtractSubreg = R->getValueAsBit("isExtractSubreg");
328 isInsertSubreg = R->getValueAsBit("isInsertSubreg");
329 isConvergent = R->getValueAsBit("isConvergent");
330 hasNoSchedulingInfo = R->getValueAsBit("hasNoSchedulingInfo");
331 FastISelShouldIgnore = R->getValueAsBit("FastISelShouldIgnore");
334 mayLoad = R->getValueAsBitOrUnset("mayLoad", Unset);
335 mayLoad_Unset = Unset;
336 mayStore = R->getValueAsBitOrUnset("mayStore", Unset);
337 mayStore_Unset = Unset;
338 hasSideEffects = R->getValueAsBitOrUnset("hasSideEffects", Unset);
339 hasSideEffects_Unset = Unset;
341 isAsCheapAsAMove = R->getValueAsBit("isAsCheapAsAMove");
342 hasExtraSrcRegAllocReq = R->getValueAsBit("hasExtraSrcRegAllocReq");
343 hasExtraDefRegAllocReq = R->getValueAsBit("hasExtraDefRegAllocReq");
344 isCodeGenOnly = R->getValueAsBit("isCodeGenOnly");
345 isPseudo = R->getValueAsBit("isPseudo");
346 ImplicitDefs = R->getValueAsListOfDefs("Defs");
347 ImplicitUses = R->getValueAsListOfDefs("Uses");
349 // This flag is only inferred from the pattern.
351 hasChain_Inferred = false;
353 // Parse Constraints.
354 ParseConstraints(R->getValueAsString("Constraints"), Operands);
356 // Parse the DisableEncoding field.
357 Operands.ProcessDisableEncoding(R->getValueAsString("DisableEncoding"));
359 // First check for a ComplexDeprecationPredicate.
360 if (R->getValue("ComplexDeprecationPredicate")) {
361 HasComplexDeprecationPredicate = true;
362 DeprecatedReason = R->getValueAsString("ComplexDeprecationPredicate");
363 } else if (RecordVal *Dep = R->getValue("DeprecatedFeatureMask")) {
364 // Check if we have a Subtarget feature mask.
365 HasComplexDeprecationPredicate = false;
366 DeprecatedReason = Dep->getValue()->getAsString();
368 // This instruction isn't deprecated.
369 HasComplexDeprecationPredicate = false;
370 DeprecatedReason = "";
374 /// HasOneImplicitDefWithKnownVT - If the instruction has at least one
375 /// implicit def and it has a known VT, return the VT, otherwise return
377 MVT::SimpleValueType CodeGenInstruction::
378 HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const {
379 if (ImplicitDefs.empty()) return MVT::Other;
381 // Check to see if the first implicit def has a resolvable type.
382 Record *FirstImplicitDef = ImplicitDefs[0];
383 assert(FirstImplicitDef->isSubClassOf("Register"));
384 const std::vector<ValueTypeByHwMode> &RegVTs =
385 TargetInfo.getRegisterVTs(FirstImplicitDef);
386 if (RegVTs.size() == 1 && RegVTs[0].isSimple())
387 return RegVTs[0].getSimple().SimpleTy;
392 /// FlattenAsmStringVariants - Flatten the specified AsmString to only
393 /// include text from the specified variant, returning the new string.
394 std::string CodeGenInstruction::
395 FlattenAsmStringVariants(StringRef Cur, unsigned Variant) {
396 std::string Res = "";
399 // Find the start of the next variant string.
400 size_t VariantsStart = 0;
401 for (size_t e = Cur.size(); VariantsStart != e; ++VariantsStart)
402 if (Cur[VariantsStart] == '{' &&
403 (VariantsStart == 0 || (Cur[VariantsStart-1] != '$' &&
404 Cur[VariantsStart-1] != '\\')))
407 // Add the prefix to the result.
408 Res += Cur.slice(0, VariantsStart);
409 if (VariantsStart == Cur.size())
412 ++VariantsStart; // Skip the '{'.
414 // Scan to the end of the variants string.
415 size_t VariantsEnd = VariantsStart;
416 unsigned NestedBraces = 1;
417 for (size_t e = Cur.size(); VariantsEnd != e; ++VariantsEnd) {
418 if (Cur[VariantsEnd] == '}' && Cur[VariantsEnd-1] != '\\') {
419 if (--NestedBraces == 0)
421 } else if (Cur[VariantsEnd] == '{')
425 // Select the Nth variant (or empty).
426 StringRef Selection = Cur.slice(VariantsStart, VariantsEnd);
427 for (unsigned i = 0; i != Variant; ++i)
428 Selection = Selection.split('|').second;
429 Res += Selection.split('|').first;
431 assert(VariantsEnd != Cur.size() &&
432 "Unterminated variants in assembly string!");
433 Cur = Cur.substr(VariantsEnd + 1);
439 bool CodeGenInstruction::isOperandAPointer(unsigned i) const {
440 if (DagInit *ConstraintList = TheDef->getValueAsDag("InOperandList")) {
441 if (i < ConstraintList->getNumArgs()) {
442 if (DefInit *Constraint = dyn_cast<DefInit>(ConstraintList->getArg(i))) {
443 return Constraint->getDef()->isSubClassOf("TypedOperand") &&
444 Constraint->getDef()->getValueAsBit("IsPointer");
451 //===----------------------------------------------------------------------===//
452 /// CodeGenInstAlias Implementation
453 //===----------------------------------------------------------------------===//
455 /// tryAliasOpMatch - This is a helper function for the CodeGenInstAlias
456 /// constructor. It checks if an argument in an InstAlias pattern matches
457 /// the corresponding operand of the instruction. It returns true on a
458 /// successful match, with ResOp set to the result operand to be used.
459 bool CodeGenInstAlias::tryAliasOpMatch(DagInit *Result, unsigned AliasOpNo,
460 Record *InstOpRec, bool hasSubOps,
461 ArrayRef<SMLoc> Loc, CodeGenTarget &T,
462 ResultOperand &ResOp) {
463 Init *Arg = Result->getArg(AliasOpNo);
464 DefInit *ADI = dyn_cast<DefInit>(Arg);
465 Record *ResultRecord = ADI ? ADI->getDef() : nullptr;
467 if (ADI && ADI->getDef() == InstOpRec) {
468 // If the operand is a record, it must have a name, and the record type
469 // must match up with the instruction's argument type.
470 if (!Result->getArgName(AliasOpNo))
471 PrintFatalError(Loc, "result argument #" + Twine(AliasOpNo) +
472 " must have a name!");
473 ResOp = ResultOperand(Result->getArgNameStr(AliasOpNo), ResultRecord);
477 // For register operands, the source register class can be a subclass
478 // of the instruction register class, not just an exact match.
479 if (InstOpRec->isSubClassOf("RegisterOperand"))
480 InstOpRec = InstOpRec->getValueAsDef("RegClass");
482 if (ADI && ADI->getDef()->isSubClassOf("RegisterOperand"))
483 ADI = ADI->getDef()->getValueAsDef("RegClass")->getDefInit();
485 if (ADI && ADI->getDef()->isSubClassOf("RegisterClass")) {
486 if (!InstOpRec->isSubClassOf("RegisterClass"))
488 if (!T.getRegisterClass(InstOpRec)
489 .hasSubClass(&T.getRegisterClass(ADI->getDef())))
491 ResOp = ResultOperand(Result->getArgNameStr(AliasOpNo), ResultRecord);
495 // Handle explicit registers.
496 if (ADI && ADI->getDef()->isSubClassOf("Register")) {
497 if (InstOpRec->isSubClassOf("OptionalDefOperand")) {
498 DagInit *DI = InstOpRec->getValueAsDag("MIOperandInfo");
499 // The operand info should only have a single (register) entry. We
500 // want the register class of it.
501 InstOpRec = cast<DefInit>(DI->getArg(0))->getDef();
504 if (!InstOpRec->isSubClassOf("RegisterClass"))
507 if (!T.getRegisterClass(InstOpRec)
508 .contains(T.getRegBank().getReg(ADI->getDef())))
509 PrintFatalError(Loc, "fixed register " + ADI->getDef()->getName() +
510 " is not a member of the " + InstOpRec->getName() +
513 if (Result->getArgName(AliasOpNo))
514 PrintFatalError(Loc, "result fixed register argument must "
517 ResOp = ResultOperand(ResultRecord);
521 // Handle "zero_reg" for optional def operands.
522 if (ADI && ADI->getDef()->getName() == "zero_reg") {
524 // Check if this is an optional def.
525 // Tied operands where the source is a sub-operand of a complex operand
526 // need to represent both operands in the alias destination instruction.
527 // Allow zero_reg for the tied portion. This can and should go away once
528 // the MC representation of things doesn't use tied operands at all.
529 //if (!InstOpRec->isSubClassOf("OptionalDefOperand"))
530 // throw TGError(Loc, "reg0 used for result that is not an "
531 // "OptionalDefOperand!");
533 ResOp = ResultOperand(static_cast<Record*>(nullptr));
538 if (IntInit *II = dyn_cast<IntInit>(Arg)) {
539 if (hasSubOps || !InstOpRec->isSubClassOf("Operand"))
541 // Integer arguments can't have names.
542 if (Result->getArgName(AliasOpNo))
543 PrintFatalError(Loc, "result argument #" + Twine(AliasOpNo) +
544 " must not have a name!");
545 ResOp = ResultOperand(II->getValue());
549 // Bits<n> (also used for 0bxx literals)
550 if (BitsInit *BI = dyn_cast<BitsInit>(Arg)) {
551 if (hasSubOps || !InstOpRec->isSubClassOf("Operand"))
553 if (!BI->isComplete())
555 // Convert the bits init to an integer and use that for the result.
557 dyn_cast_or_null<IntInit>(BI->convertInitializerTo(IntRecTy::get()));
560 ResOp = ResultOperand(II->getValue());
564 // If both are Operands with the same MVT, allow the conversion. It's
565 // up to the user to make sure the values are appropriate, just like
567 if (InstOpRec->isSubClassOf("Operand") && ADI &&
568 ADI->getDef()->isSubClassOf("Operand")) {
569 // FIXME: What other attributes should we check here? Identical
570 // MIOperandInfo perhaps?
571 if (InstOpRec->getValueInit("Type") != ADI->getDef()->getValueInit("Type"))
573 ResOp = ResultOperand(Result->getArgNameStr(AliasOpNo), ADI->getDef());
580 unsigned CodeGenInstAlias::ResultOperand::getMINumOperands() const {
584 Record *Rec = getRecord();
585 if (!Rec->isSubClassOf("Operand"))
588 DagInit *MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
589 if (MIOpInfo->getNumArgs() == 0) {
590 // Unspecified, so it defaults to 1
594 return MIOpInfo->getNumArgs();
597 CodeGenInstAlias::CodeGenInstAlias(Record *R, CodeGenTarget &T)
599 Result = R->getValueAsDag("ResultInst");
600 AsmString = R->getValueAsString("AsmString");
603 // Verify that the root of the result is an instruction.
604 DefInit *DI = dyn_cast<DefInit>(Result->getOperator());
605 if (!DI || !DI->getDef()->isSubClassOf("Instruction"))
606 PrintFatalError(R->getLoc(),
607 "result of inst alias should be an instruction");
609 ResultInst = &T.getInstruction(DI->getDef());
611 // NameClass - If argument names are repeated, we need to verify they have
613 StringMap<Record*> NameClass;
614 for (unsigned i = 0, e = Result->getNumArgs(); i != e; ++i) {
615 DefInit *ADI = dyn_cast<DefInit>(Result->getArg(i));
616 if (!ADI || !Result->getArgName(i))
618 // Verify we don't have something like: (someinst GR16:$foo, GR32:$foo)
619 // $foo can exist multiple times in the result list, but it must have the
621 Record *&Entry = NameClass[Result->getArgNameStr(i)];
622 if (Entry && Entry != ADI->getDef())
623 PrintFatalError(R->getLoc(), "result value $" + Result->getArgNameStr(i) +
624 " is both " + Entry->getName() + " and " +
625 ADI->getDef()->getName() + "!");
626 Entry = ADI->getDef();
629 // Decode and validate the arguments of the result.
630 unsigned AliasOpNo = 0;
631 for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) {
633 // Tied registers don't have an entry in the result dag unless they're part
634 // of a complex operand, in which case we include them anyways, as we
635 // don't have any other way to specify the whole operand.
636 if (ResultInst->Operands[i].MINumOperands == 1 &&
637 ResultInst->Operands[i].getTiedRegister() != -1) {
638 // Tied operands of different RegisterClass should be explicit within an
639 // instruction's syntax and so cannot be skipped.
640 int TiedOpNum = ResultInst->Operands[i].getTiedRegister();
641 if (ResultInst->Operands[i].Rec->getName() ==
642 ResultInst->Operands[TiedOpNum].Rec->getName())
646 if (AliasOpNo >= Result->getNumArgs())
647 PrintFatalError(R->getLoc(), "not enough arguments for instruction!");
649 Record *InstOpRec = ResultInst->Operands[i].Rec;
650 unsigned NumSubOps = ResultInst->Operands[i].MINumOperands;
651 ResultOperand ResOp(static_cast<int64_t>(0));
652 if (tryAliasOpMatch(Result, AliasOpNo, InstOpRec, (NumSubOps > 1),
653 R->getLoc(), T, ResOp)) {
654 // If this is a simple operand, or a complex operand with a custom match
655 // class, then we can match is verbatim.
656 if (NumSubOps == 1 ||
657 (InstOpRec->getValue("ParserMatchClass") &&
658 InstOpRec->getValueAsDef("ParserMatchClass")
659 ->getValueAsString("Name") != "Imm")) {
660 ResultOperands.push_back(ResOp);
661 ResultInstOperandIndex.push_back(std::make_pair(i, -1));
664 // Otherwise, we need to match each of the suboperands individually.
666 DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo;
667 for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) {
668 Record *SubRec = cast<DefInit>(MIOI->getArg(SubOp))->getDef();
670 // Take care to instantiate each of the suboperands with the correct
671 // nomenclature: $foo.bar
672 ResultOperands.emplace_back(
673 Result->getArgName(AliasOpNo)->getAsUnquotedString() + "." +
674 MIOI->getArgName(SubOp)->getAsUnquotedString(), SubRec);
675 ResultInstOperandIndex.push_back(std::make_pair(i, SubOp));
682 // If the argument did not match the instruction operand, and the operand
683 // is composed of multiple suboperands, try matching the suboperands.
685 DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo;
686 for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) {
687 if (AliasOpNo >= Result->getNumArgs())
688 PrintFatalError(R->getLoc(), "not enough arguments for instruction!");
689 Record *SubRec = cast<DefInit>(MIOI->getArg(SubOp))->getDef();
690 if (tryAliasOpMatch(Result, AliasOpNo, SubRec, false,
691 R->getLoc(), T, ResOp)) {
692 ResultOperands.push_back(ResOp);
693 ResultInstOperandIndex.push_back(std::make_pair(i, SubOp));
696 PrintFatalError(R->getLoc(), "result argument #" + Twine(AliasOpNo) +
697 " does not match instruction operand class " +
698 (SubOp == 0 ? InstOpRec->getName() :SubRec->getName()));
703 PrintFatalError(R->getLoc(), "result argument #" + Twine(AliasOpNo) +
704 " does not match instruction operand class " +
705 InstOpRec->getName());
708 if (AliasOpNo != Result->getNumArgs())
709 PrintFatalError(R->getLoc(), "too many operands for instruction!");