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"
20 static void ParseConstraint(const std::string &CStr, CodeGenInstruction *I) {
21 // EARLY_CLOBBER: @early $reg
22 std::string::size_type wpos = CStr.find_first_of(" \t");
23 std::string::size_type start = CStr.find_first_not_of(" \t");
24 std::string Tok = CStr.substr(start, wpos - start);
25 if (Tok == "@earlyclobber") {
26 std::string Name = CStr.substr(wpos+1);
27 wpos = Name.find_first_not_of(" \t");
28 if (wpos == std::string::npos)
29 throw "Illegal format for @earlyclobber constraint: '" + CStr + "'";
30 Name = Name.substr(wpos);
31 std::pair<unsigned,unsigned> Op =
32 I->ParseOperandName(Name, false);
34 // Build the string for the operand
35 std::string OpConstraint = "(1 << TOI::EARLY_CLOBBER)";
36 if (!I->OperandList[Op.first].Constraints[Op.second].empty())
37 throw "Operand '" + Name + "' cannot have multiple constraints!";
38 I->OperandList[Op.first].Constraints[Op.second] = OpConstraint;
42 // Only other constraint is "TIED_TO" for now.
43 std::string::size_type pos = CStr.find_first_of('=');
44 assert(pos != std::string::npos && "Unrecognized constraint");
45 start = CStr.find_first_not_of(" \t");
46 std::string Name = CStr.substr(start, pos - start);
48 // TIED_TO: $src1 = $dst
49 wpos = Name.find_first_of(" \t");
50 if (wpos == std::string::npos)
51 throw "Illegal format for tied-to constraint: '" + CStr + "'";
52 std::string DestOpName = Name.substr(0, wpos);
53 std::pair<unsigned,unsigned> DestOp = I->ParseOperandName(DestOpName, false);
55 Name = CStr.substr(pos+1);
56 wpos = Name.find_first_not_of(" \t");
57 if (wpos == std::string::npos)
58 throw "Illegal format for tied-to constraint: '" + CStr + "'";
60 std::pair<unsigned,unsigned> SrcOp =
61 I->ParseOperandName(Name.substr(wpos), false);
63 throw "Illegal tied-to operand constraint '" + CStr + "'";
66 unsigned FlatOpNo = I->getFlattenedOperandNumber(SrcOp);
67 // Build the string for the operand.
68 std::string OpConstraint =
69 "((" + utostr(FlatOpNo) + " << 16) | (1 << TOI::TIED_TO))";
71 if (!I->OperandList[DestOp.first].Constraints[DestOp.second].empty())
72 throw "Operand '" + DestOpName + "' cannot have multiple constraints!";
73 I->OperandList[DestOp.first].Constraints[DestOp.second] = OpConstraint;
76 static void ParseConstraints(const std::string &CStr, CodeGenInstruction *I) {
77 // Make sure the constraints list for each operand is large enough to hold
78 // constraint info, even if none is present.
79 for (unsigned i = 0, e = I->OperandList.size(); i != e; ++i)
80 I->OperandList[i].Constraints.resize(I->OperandList[i].MINumOperands);
82 if (CStr.empty()) return;
84 const std::string delims(",");
85 std::string::size_type bidx, eidx;
87 bidx = CStr.find_first_not_of(delims);
88 while (bidx != std::string::npos) {
89 eidx = CStr.find_first_of(delims, bidx);
90 if (eidx == std::string::npos)
93 ParseConstraint(CStr.substr(bidx, eidx - bidx), I);
94 bidx = CStr.find_first_not_of(delims, eidx);
98 CodeGenInstruction::CodeGenInstruction(Record *R, const std::string &AsmStr)
99 : TheDef(R), AsmString(AsmStr) {
100 Namespace = R->getValueAsString("Namespace");
102 isReturn = R->getValueAsBit("isReturn");
103 isBranch = R->getValueAsBit("isBranch");
104 isIndirectBranch = R->getValueAsBit("isIndirectBranch");
105 isBarrier = R->getValueAsBit("isBarrier");
106 isCall = R->getValueAsBit("isCall");
107 canFoldAsLoad = R->getValueAsBit("canFoldAsLoad");
108 mayLoad = R->getValueAsBit("mayLoad");
109 mayStore = R->getValueAsBit("mayStore");
110 bool isTwoAddress = R->getValueAsBit("isTwoAddress");
111 isPredicable = R->getValueAsBit("isPredicable");
112 isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
113 isCommutable = R->getValueAsBit("isCommutable");
114 isTerminator = R->getValueAsBit("isTerminator");
115 isReMaterializable = R->getValueAsBit("isReMaterializable");
116 hasDelaySlot = R->getValueAsBit("hasDelaySlot");
117 usesCustomInserter = R->getValueAsBit("usesCustomInserter");
118 hasCtrlDep = R->getValueAsBit("hasCtrlDep");
119 isNotDuplicable = R->getValueAsBit("isNotDuplicable");
120 hasSideEffects = R->getValueAsBit("hasSideEffects");
121 mayHaveSideEffects = R->getValueAsBit("mayHaveSideEffects");
122 neverHasSideEffects = R->getValueAsBit("neverHasSideEffects");
123 isAsCheapAsAMove = R->getValueAsBit("isAsCheapAsAMove");
124 hasExtraSrcRegAllocReq = R->getValueAsBit("hasExtraSrcRegAllocReq");
125 hasExtraDefRegAllocReq = R->getValueAsBit("hasExtraDefRegAllocReq");
126 hasOptionalDef = false;
129 if (mayHaveSideEffects + neverHasSideEffects + hasSideEffects > 1)
130 throw R->getName() + ": multiple conflicting side-effect flags set!";
134 DI = R->getValueAsDag("OutOperandList");
136 // Error getting operand list, just ignore it (sparcv9).
141 NumDefs = DI->getNumArgs();
145 IDI = R->getValueAsDag("InOperandList");
147 // Error getting operand list, just ignore it (sparcv9).
152 DI = (DagInit*)(new BinOpInit(BinOpInit::CONCAT, DI, IDI, new DagRecTy))->Fold(R, 0);
154 unsigned MIOperandNo = 0;
155 std::set<std::string> OperandNames;
156 for (unsigned i = 0, e = DI->getNumArgs(); i != e; ++i) {
157 DefInit *Arg = dynamic_cast<DefInit*>(DI->getArg(i));
159 throw "Illegal operand for the '" + R->getName() + "' instruction!";
161 Record *Rec = Arg->getDef();
162 std::string PrintMethod = "printOperand";
164 DagInit *MIOpInfo = 0;
165 if (Rec->isSubClassOf("Operand")) {
166 PrintMethod = Rec->getValueAsString("PrintMethod");
167 MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
169 // Verify that MIOpInfo has an 'ops' root value.
170 if (!dynamic_cast<DefInit*>(MIOpInfo->getOperator()) ||
171 dynamic_cast<DefInit*>(MIOpInfo->getOperator())
172 ->getDef()->getName() != "ops")
173 throw "Bad value for MIOperandInfo in operand '" + Rec->getName() +
176 // If we have MIOpInfo, then we have #operands equal to number of entries
178 if (unsigned NumArgs = MIOpInfo->getNumArgs())
181 if (Rec->isSubClassOf("PredicateOperand"))
183 else if (Rec->isSubClassOf("OptionalDefOperand"))
184 hasOptionalDef = true;
185 } else if (Rec->getName() == "variable_ops") {
188 } else if (!Rec->isSubClassOf("RegisterClass") &&
189 Rec->getName() != "ptr_rc" && Rec->getName() != "unknown")
190 throw "Unknown operand class '" + Rec->getName() +
191 "' in '" + R->getName() + "' instruction!";
193 // Check that the operand has a name and that it's unique.
194 if (DI->getArgName(i).empty())
195 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
197 if (!OperandNames.insert(DI->getArgName(i)).second)
198 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
199 " has the same name as a previous operand!";
201 OperandList.push_back(OperandInfo(Rec, DI->getArgName(i), PrintMethod,
202 MIOperandNo, NumOps, MIOpInfo));
203 MIOperandNo += NumOps;
206 // Parse Constraints.
207 ParseConstraints(R->getValueAsString("Constraints"), this);
209 // For backward compatibility: isTwoAddress means operand 1 is tied to
212 if (!OperandList[1].Constraints[0].empty())
213 throw R->getName() + ": cannot use isTwoAddress property: instruction "
214 "already has constraint set!";
215 OperandList[1].Constraints[0] = "((0 << 16) | (1 << TOI::TIED_TO))";
218 // Any operands with unset constraints get 0 as their constraint.
219 for (unsigned op = 0, e = OperandList.size(); op != e; ++op)
220 for (unsigned j = 0, e = OperandList[op].MINumOperands; j != e; ++j)
221 if (OperandList[op].Constraints[j].empty())
222 OperandList[op].Constraints[j] = "0";
224 // Parse the DisableEncoding field.
225 std::string DisableEncoding = R->getValueAsString("DisableEncoding");
227 std::string OpName = getToken(DisableEncoding, " ,\t");
228 if (OpName.empty()) break;
230 // Figure out which operand this is.
231 std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false);
233 // Mark the operand as not-to-be encoded.
234 if (Op.second >= OperandList[Op.first].DoNotEncode.size())
235 OperandList[Op.first].DoNotEncode.resize(Op.second+1);
236 OperandList[Op.first].DoNotEncode[Op.second] = true;
240 /// getOperandNamed - Return the index of the operand with the specified
241 /// non-empty name. If the instruction does not have an operand with the
242 /// specified name, throw an exception.
244 unsigned CodeGenInstruction::getOperandNamed(const std::string &Name) const {
245 assert(!Name.empty() && "Cannot search for operand with no name!");
246 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
247 if (OperandList[i].Name == Name) return i;
248 throw "Instruction '" + TheDef->getName() +
249 "' does not have an operand named '$" + Name + "'!";
252 std::pair<unsigned,unsigned>
253 CodeGenInstruction::ParseOperandName(const std::string &Op,
255 if (Op.empty() || Op[0] != '$')
256 throw TheDef->getName() + ": Illegal operand name: '" + Op + "'";
258 std::string OpName = Op.substr(1);
259 std::string SubOpName;
261 // Check to see if this is $foo.bar.
262 std::string::size_type DotIdx = OpName.find_first_of(".");
263 if (DotIdx != std::string::npos) {
264 SubOpName = OpName.substr(DotIdx+1);
265 if (SubOpName.empty())
266 throw TheDef->getName() + ": illegal empty suboperand name in '" +Op +"'";
267 OpName = OpName.substr(0, DotIdx);
270 unsigned OpIdx = getOperandNamed(OpName);
272 if (SubOpName.empty()) { // If no suboperand name was specified:
273 // If one was needed, throw.
274 if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp &&
276 throw TheDef->getName() + ": Illegal to refer to"
277 " whole operand part of complex operand '" + Op + "'";
279 // Otherwise, return the operand.
280 return std::make_pair(OpIdx, 0U);
283 // Find the suboperand number involved.
284 DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
286 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
288 // Find the operand with the right name.
289 for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i)
290 if (MIOpInfo->getArgName(i) == SubOpName)
291 return std::make_pair(OpIdx, i);
293 // Otherwise, didn't find it!
294 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";