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"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/ADT/STLExtras.h"
21 static void ParseConstraint(const std::string &CStr, CodeGenInstruction *I) {
22 // EARLY_CLOBBER: @early $reg
23 std::string::size_type wpos = CStr.find_first_of(" \t");
24 std::string::size_type start = CStr.find_first_not_of(" \t");
25 std::string Tok = CStr.substr(start, wpos - start);
26 if (Tok == "@earlyclobber") {
27 std::string Name = CStr.substr(wpos+1);
28 wpos = Name.find_first_not_of(" \t");
29 if (wpos == std::string::npos)
30 throw "Illegal format for @earlyclobber constraint: '" + CStr + "'";
31 Name = Name.substr(wpos);
32 std::pair<unsigned,unsigned> Op =
33 I->ParseOperandName(Name, false);
35 // Build the string for the operand
36 if (!I->OperandList[Op.first].Constraints[Op.second].isNone())
37 throw "Operand '" + Name + "' cannot have multiple constraints!";
38 I->OperandList[Op.first].Constraints[Op.second] =
39 CodeGenInstruction::ConstraintInfo::getEarlyClobber();
43 // Only other constraint is "TIED_TO" for now.
44 std::string::size_type pos = CStr.find_first_of('=');
45 assert(pos != std::string::npos && "Unrecognized constraint");
46 start = CStr.find_first_not_of(" \t");
47 std::string Name = CStr.substr(start, pos - start);
49 // TIED_TO: $src1 = $dst
50 wpos = Name.find_first_of(" \t");
51 if (wpos == std::string::npos)
52 throw "Illegal format for tied-to constraint: '" + CStr + "'";
53 std::string DestOpName = Name.substr(0, wpos);
54 std::pair<unsigned,unsigned> DestOp = I->ParseOperandName(DestOpName, false);
56 Name = CStr.substr(pos+1);
57 wpos = Name.find_first_not_of(" \t");
58 if (wpos == std::string::npos)
59 throw "Illegal format for tied-to constraint: '" + CStr + "'";
61 std::pair<unsigned,unsigned> SrcOp =
62 I->ParseOperandName(Name.substr(wpos), false);
64 throw "Illegal tied-to operand constraint '" + CStr + "'";
67 unsigned FlatOpNo = I->getFlattenedOperandNumber(SrcOp);
69 if (!I->OperandList[DestOp.first].Constraints[DestOp.second].isNone())
70 throw "Operand '" + DestOpName + "' cannot have multiple constraints!";
71 I->OperandList[DestOp.first].Constraints[DestOp.second] =
72 CodeGenInstruction::ConstraintInfo::getTied(FlatOpNo);
75 static void ParseConstraints(const std::string &CStr, CodeGenInstruction *I) {
76 // Make sure the constraints list for each operand is large enough to hold
77 // constraint info, even if none is present.
78 for (unsigned i = 0, e = I->OperandList.size(); i != e; ++i)
79 I->OperandList[i].Constraints.resize(I->OperandList[i].MINumOperands);
81 if (CStr.empty()) return;
83 const std::string delims(",");
84 std::string::size_type bidx, eidx;
86 bidx = CStr.find_first_not_of(delims);
87 while (bidx != std::string::npos) {
88 eidx = CStr.find_first_of(delims, bidx);
89 if (eidx == std::string::npos)
92 ParseConstraint(CStr.substr(bidx, eidx - bidx), I);
93 bidx = CStr.find_first_not_of(delims, eidx);
97 CodeGenInstruction::CodeGenInstruction(Record *R, const std::string &AsmStr)
98 : TheDef(R), AsmString(AsmStr) {
99 Namespace = R->getValueAsString("Namespace");
101 isReturn = R->getValueAsBit("isReturn");
102 isBranch = R->getValueAsBit("isBranch");
103 isIndirectBranch = R->getValueAsBit("isIndirectBranch");
104 isBarrier = R->getValueAsBit("isBarrier");
105 isCall = R->getValueAsBit("isCall");
106 canFoldAsLoad = R->getValueAsBit("canFoldAsLoad");
107 mayLoad = R->getValueAsBit("mayLoad");
108 mayStore = R->getValueAsBit("mayStore");
109 bool isTwoAddress = R->getValueAsBit("isTwoAddress");
110 isPredicable = R->getValueAsBit("isPredicable");
111 isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
112 isCommutable = R->getValueAsBit("isCommutable");
113 isTerminator = R->getValueAsBit("isTerminator");
114 isReMaterializable = R->getValueAsBit("isReMaterializable");
115 hasDelaySlot = R->getValueAsBit("hasDelaySlot");
116 usesCustomInserter = R->getValueAsBit("usesCustomInserter");
117 hasCtrlDep = R->getValueAsBit("hasCtrlDep");
118 isNotDuplicable = R->getValueAsBit("isNotDuplicable");
119 hasSideEffects = R->getValueAsBit("hasSideEffects");
120 neverHasSideEffects = R->getValueAsBit("neverHasSideEffects");
121 isAsCheapAsAMove = R->getValueAsBit("isAsCheapAsAMove");
122 hasExtraSrcRegAllocReq = R->getValueAsBit("hasExtraSrcRegAllocReq");
123 hasExtraDefRegAllocReq = R->getValueAsBit("hasExtraDefRegAllocReq");
124 hasOptionalDef = false;
127 if (neverHasSideEffects + hasSideEffects > 1)
128 throw R->getName() + ": multiple conflicting side-effect flags set!";
130 DagInit *DI = R->getValueAsDag("OutOperandList");
132 if (DefInit *Init = dynamic_cast<DefInit*>(DI->getOperator())) {
133 if (Init->getDef()->getName() != "outs")
134 throw R->getName() + ": invalid def name for output list: use 'outs'";
136 throw R->getName() + ": invalid output list: use 'outs'";
138 NumDefs = DI->getNumArgs();
140 DagInit *IDI = R->getValueAsDag("InOperandList");
141 if (DefInit *Init = dynamic_cast<DefInit*>(IDI->getOperator())) {
142 if (Init->getDef()->getName() != "ins")
143 throw R->getName() + ": invalid def name for input list: use 'ins'";
145 throw R->getName() + ": invalid input list: use 'ins'";
147 DI = (DagInit*)(new BinOpInit(BinOpInit::CONCAT,
148 DI, IDI, new DagRecTy))->Fold(R, 0);
150 unsigned MIOperandNo = 0;
151 std::set<std::string> OperandNames;
152 for (unsigned i = 0, e = DI->getNumArgs(); i != e; ++i) {
153 DefInit *Arg = dynamic_cast<DefInit*>(DI->getArg(i));
155 throw "Illegal operand for the '" + R->getName() + "' instruction!";
157 Record *Rec = Arg->getDef();
158 std::string PrintMethod = "printOperand";
160 DagInit *MIOpInfo = 0;
161 if (Rec->isSubClassOf("Operand")) {
162 PrintMethod = Rec->getValueAsString("PrintMethod");
163 MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
165 // Verify that MIOpInfo has an 'ops' root value.
166 if (!dynamic_cast<DefInit*>(MIOpInfo->getOperator()) ||
167 dynamic_cast<DefInit*>(MIOpInfo->getOperator())
168 ->getDef()->getName() != "ops")
169 throw "Bad value for MIOperandInfo in operand '" + Rec->getName() +
172 // If we have MIOpInfo, then we have #operands equal to number of entries
174 if (unsigned NumArgs = MIOpInfo->getNumArgs())
177 if (Rec->isSubClassOf("PredicateOperand"))
179 else if (Rec->isSubClassOf("OptionalDefOperand"))
180 hasOptionalDef = true;
181 } else if (Rec->getName() == "variable_ops") {
184 } else if (!Rec->isSubClassOf("RegisterClass") &&
185 Rec->getName() != "ptr_rc" && Rec->getName() != "unknown")
186 throw "Unknown operand class '" + Rec->getName() +
187 "' in '" + R->getName() + "' instruction!";
189 // Check that the operand has a name and that it's unique.
190 if (DI->getArgName(i).empty())
191 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
193 if (!OperandNames.insert(DI->getArgName(i)).second)
194 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
195 " has the same name as a previous operand!";
197 OperandList.push_back(OperandInfo(Rec, DI->getArgName(i), PrintMethod,
198 MIOperandNo, NumOps, MIOpInfo));
199 MIOperandNo += NumOps;
202 // Parse Constraints.
203 ParseConstraints(R->getValueAsString("Constraints"), this);
205 // For backward compatibility: isTwoAddress means operand 1 is tied to
208 if (!OperandList[1].Constraints[0].isNone())
209 throw R->getName() + ": cannot use isTwoAddress property: instruction "
210 "already has constraint set!";
211 OperandList[1].Constraints[0] =
212 CodeGenInstruction::ConstraintInfo::getTied(0);
215 // Parse the DisableEncoding field.
216 std::string DisableEncoding = R->getValueAsString("DisableEncoding");
219 tie(OpName, DisableEncoding) = getToken(DisableEncoding, " ,\t");
220 if (OpName.empty()) break;
222 // Figure out which operand this is.
223 std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false);
225 // Mark the operand as not-to-be encoded.
226 if (Op.second >= OperandList[Op.first].DoNotEncode.size())
227 OperandList[Op.first].DoNotEncode.resize(Op.second+1);
228 OperandList[Op.first].DoNotEncode[Op.second] = true;
232 /// getOperandNamed - Return the index of the operand with the specified
233 /// non-empty name. If the instruction does not have an operand with the
234 /// specified name, throw an exception.
236 unsigned CodeGenInstruction::getOperandNamed(const std::string &Name) const {
237 assert(!Name.empty() && "Cannot search for operand with no name!");
238 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
239 if (OperandList[i].Name == Name) return i;
240 throw "Instruction '" + TheDef->getName() +
241 "' does not have an operand named '$" + Name + "'!";
244 std::pair<unsigned,unsigned>
245 CodeGenInstruction::ParseOperandName(const std::string &Op,
247 if (Op.empty() || Op[0] != '$')
248 throw TheDef->getName() + ": Illegal operand name: '" + Op + "'";
250 std::string OpName = Op.substr(1);
251 std::string SubOpName;
253 // Check to see if this is $foo.bar.
254 std::string::size_type DotIdx = OpName.find_first_of(".");
255 if (DotIdx != std::string::npos) {
256 SubOpName = OpName.substr(DotIdx+1);
257 if (SubOpName.empty())
258 throw TheDef->getName() + ": illegal empty suboperand name in '" +Op +"'";
259 OpName = OpName.substr(0, DotIdx);
262 unsigned OpIdx = getOperandNamed(OpName);
264 if (SubOpName.empty()) { // If no suboperand name was specified:
265 // If one was needed, throw.
266 if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp &&
268 throw TheDef->getName() + ": Illegal to refer to"
269 " whole operand part of complex operand '" + Op + "'";
271 // Otherwise, return the operand.
272 return std::make_pair(OpIdx, 0U);
275 // Find the suboperand number involved.
276 DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
278 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
280 // Find the operand with the right name.
281 for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i)
282 if (MIOpInfo->getArgName(i) == SubOpName)
283 return std::make_pair(OpIdx, i);
285 // Otherwise, didn't find it!
286 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";