Deleted Added
full compact
CodeGenInstruction.cpp (198892) CodeGenInstruction.cpp (201360)
1//===- 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//===----------------------------------------------------------------------===//

--- 4 unchanged lines hidden (view full) ---

13
14#include "CodeGenInstruction.h"
15#include "Record.h"
16#include "llvm/ADT/StringExtras.h"
17#include <set>
18using namespace llvm;
19
20static void ParseConstraint(const std::string &CStr, CodeGenInstruction *I) {
1//===- 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//===----------------------------------------------------------------------===//

--- 4 unchanged lines hidden (view full) ---

13
14#include "CodeGenInstruction.h"
15#include "Record.h"
16#include "llvm/ADT/StringExtras.h"
17#include <set>
18using namespace llvm;
19
20static void ParseConstraint(const std::string &CStr, CodeGenInstruction *I) {
21 // FIXME: Only supports TIED_TO for now.
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);
33
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;
39 return;
40 }
41
42 // Only other constraint is "TIED_TO" for now.
22 std::string::size_type pos = CStr.find_first_of('=');
23 assert(pos != std::string::npos && "Unrecognized constraint");
43 std::string::size_type pos = CStr.find_first_of('=');
44 assert(pos != std::string::npos && "Unrecognized constraint");
24 std::string::size_type start = CStr.find_first_not_of(" \t");
45 start = CStr.find_first_not_of(" \t");
25 std::string Name = CStr.substr(start, pos - start);
46 std::string Name = CStr.substr(start, pos - start);
26
47
27 // TIED_TO: $src1 = $dst
48 // TIED_TO: $src1 = $dst
28 std::string::size_type wpos = Name.find_first_of(" \t");
49 wpos = Name.find_first_of(" \t");
29 if (wpos == std::string::npos)
30 throw "Illegal format for tied-to constraint: '" + CStr + "'";
31 std::string DestOpName = Name.substr(0, wpos);
32 std::pair<unsigned,unsigned> DestOp = I->ParseOperandName(DestOpName, false);
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);
33
54
34 Name = CStr.substr(pos+1);
35 wpos = Name.find_first_not_of(" \t");
36 if (wpos == std::string::npos)
37 throw "Illegal format for tied-to constraint: '" + CStr + "'";
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 + "'";
38
59
39 std::pair<unsigned,unsigned> SrcOp =
40 I->ParseOperandName(Name.substr(wpos), false);
41 if (SrcOp > DestOp)
42 throw "Illegal tied-to operand constraint '" + CStr + "'";
60 std::pair<unsigned,unsigned> SrcOp =
61 I->ParseOperandName(Name.substr(wpos), false);
62 if (SrcOp > DestOp)
63 throw "Illegal tied-to operand constraint '" + CStr + "'";
43
44
64
65
45 unsigned FlatOpNo = I->getFlattenedOperandNumber(SrcOp);
46 // Build the string for the operand.
47 std::string OpConstraint =
48 "((" + utostr(FlatOpNo) + " << 16) | (1 << TOI::TIED_TO))";
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))";
49
50
70
51 if (!I->OperandList[DestOp.first].Constraints[DestOp.second].empty())
52 throw "Operand '" + DestOpName + "' cannot have multiple constraints!";
53 I->OperandList[DestOp.first].Constraints[DestOp.second] = OpConstraint;
54}
55
56static void ParseConstraints(const std::string &CStr, CodeGenInstruction *I) {
57 // Make sure the constraints list for each operand is large enough to hold
58 // constraint info, even if none is present.
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;
74}
75
76static 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.
59 for (unsigned i = 0, e = I->OperandList.size(); i != e; ++i)
79 for (unsigned i = 0, e = I->OperandList.size(); i != e; ++i)
60 I->OperandList[i].Constraints.resize(I->OperandList[i].MINumOperands);
80 I->OperandList[i].Constraints.resize(I->OperandList[i].MINumOperands);
61
81
62 if (CStr.empty()) return;
82 if (CStr.empty()) return;
63
83
64 const std::string delims(",");
65 std::string::size_type bidx, eidx;
84 const std::string delims(",");
85 std::string::size_type bidx, eidx;
66
86
67 bidx = CStr.find_first_not_of(delims);
68 while (bidx != std::string::npos) {
69 eidx = CStr.find_first_of(delims, bidx);
70 if (eidx == std::string::npos)
71 eidx = CStr.length();
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)
91 eidx = CStr.length();
72
92
73 ParseConstraint(CStr.substr(bidx, eidx - bidx), I);
74 bidx = CStr.find_first_not_of(delims, eidx);
75 }
76}
77
78CodeGenInstruction::CodeGenInstruction(Record *R, const std::string &AsmStr)
79 : TheDef(R), AsmString(AsmStr) {
80 Namespace = R->getValueAsString("Namespace");

--- 59 unchanged lines hidden (view full) ---

140
141 Record *Rec = Arg->getDef();
142 std::string PrintMethod = "printOperand";
143 unsigned NumOps = 1;
144 DagInit *MIOpInfo = 0;
145 if (Rec->isSubClassOf("Operand")) {
146 PrintMethod = Rec->getValueAsString("PrintMethod");
147 MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
93 ParseConstraint(CStr.substr(bidx, eidx - bidx), I);
94 bidx = CStr.find_first_not_of(delims, eidx);
95 }
96}
97
98CodeGenInstruction::CodeGenInstruction(Record *R, const std::string &AsmStr)
99 : TheDef(R), AsmString(AsmStr) {
100 Namespace = R->getValueAsString("Namespace");

--- 59 unchanged lines hidden (view full) ---

160
161 Record *Rec = Arg->getDef();
162 std::string PrintMethod = "printOperand";
163 unsigned NumOps = 1;
164 DagInit *MIOpInfo = 0;
165 if (Rec->isSubClassOf("Operand")) {
166 PrintMethod = Rec->getValueAsString("PrintMethod");
167 MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
148
168
149 // Verify that MIOpInfo has an 'ops' root value.
150 if (!dynamic_cast<DefInit*>(MIOpInfo->getOperator()) ||
151 dynamic_cast<DefInit*>(MIOpInfo->getOperator())
152 ->getDef()->getName() != "ops")
153 throw "Bad value for MIOperandInfo in operand '" + Rec->getName() +
154 "'\n";
155
156 // If we have MIOpInfo, then we have #operands equal to number of entries
157 // in MIOperandInfo.
158 if (unsigned NumArgs = MIOpInfo->getNumArgs())
159 NumOps = NumArgs;
160
161 if (Rec->isSubClassOf("PredicateOperand"))
162 isPredicable = true;
163 else if (Rec->isSubClassOf("OptionalDefOperand"))
164 hasOptionalDef = true;
165 } else if (Rec->getName() == "variable_ops") {
166 isVariadic = true;
167 continue;
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() +
174 "'\n";
175
176 // If we have MIOpInfo, then we have #operands equal to number of entries
177 // in MIOperandInfo.
178 if (unsigned NumArgs = MIOpInfo->getNumArgs())
179 NumOps = NumArgs;
180
181 if (Rec->isSubClassOf("PredicateOperand"))
182 isPredicable = true;
183 else if (Rec->isSubClassOf("OptionalDefOperand"))
184 hasOptionalDef = true;
185 } else if (Rec->getName() == "variable_ops") {
186 isVariadic = true;
187 continue;
168 } else if (!Rec->isSubClassOf("RegisterClass") &&
188 } else if (!Rec->isSubClassOf("RegisterClass") &&
169 Rec->getName() != "ptr_rc" && Rec->getName() != "unknown")
170 throw "Unknown operand class '" + Rec->getName() +
171 "' in '" + R->getName() + "' instruction!";
172
173 // Check that the operand has a name and that it's unique.
174 if (DI->getArgName(i).empty())
175 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
176 " has no name!";
177 if (!OperandNames.insert(DI->getArgName(i)).second)
178 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
179 " has the same name as a previous operand!";
189 Rec->getName() != "ptr_rc" && Rec->getName() != "unknown")
190 throw "Unknown operand class '" + Rec->getName() +
191 "' in '" + R->getName() + "' instruction!";
192
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) +
196 " has no name!";
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!";
180
181 OperandList.push_back(OperandInfo(Rec, DI->getArgName(i), PrintMethod,
200
201 OperandList.push_back(OperandInfo(Rec, DI->getArgName(i), PrintMethod,
182 MIOperandNo, NumOps, MIOpInfo));
183 MIOperandNo += NumOps;
184 }
185
186 // Parse Constraints.
187 ParseConstraints(R->getValueAsString("Constraints"), this);
202 MIOperandNo, NumOps, MIOpInfo));
203 MIOperandNo += NumOps;
204 }
205
206 // Parse Constraints.
207 ParseConstraints(R->getValueAsString("Constraints"), this);
188
208
189 // For backward compatibility: isTwoAddress means operand 1 is tied to
190 // operand 0.
191 if (isTwoAddress) {
192 if (!OperandList[1].Constraints[0].empty())
193 throw R->getName() + ": cannot use isTwoAddress property: instruction "
194 "already has constraint set!";
195 OperandList[1].Constraints[0] = "((0 << 16) | (1 << TOI::TIED_TO))";
196 }
209 // For backward compatibility: isTwoAddress means operand 1 is tied to
210 // operand 0.
211 if (isTwoAddress) {
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))";
216 }
197
217
198 // Any operands with unset constraints get 0 as their constraint.
199 for (unsigned op = 0, e = OperandList.size(); op != e; ++op)
200 for (unsigned j = 0, e = OperandList[op].MINumOperands; j != e; ++j)
201 if (OperandList[op].Constraints[j].empty())
202 OperandList[op].Constraints[j] = "0";
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";
203
223
204 // Parse the DisableEncoding field.
205 std::string DisableEncoding = R->getValueAsString("DisableEncoding");
206 while (1) {
207 std::string OpName = getToken(DisableEncoding, " ,\t");
208 if (OpName.empty()) break;
209
210 // Figure out which operand this is.
211 std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false);

--- 12 unchanged lines hidden (view full) ---

224unsigned CodeGenInstruction::getOperandNamed(const std::string &Name) const {
225 assert(!Name.empty() && "Cannot search for operand with no name!");
226 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
227 if (OperandList[i].Name == Name) return i;
228 throw "Instruction '" + TheDef->getName() +
229 "' does not have an operand named '$" + Name + "'!";
230}
231
224 // Parse the DisableEncoding field.
225 std::string DisableEncoding = R->getValueAsString("DisableEncoding");
226 while (1) {
227 std::string OpName = getToken(DisableEncoding, " ,\t");
228 if (OpName.empty()) break;
229
230 // Figure out which operand this is.
231 std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false);

--- 12 unchanged lines hidden (view full) ---

244unsigned 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 + "'!";
250}
251
232std::pair<unsigned,unsigned>
252std::pair
233CodeGenInstruction::ParseOperandName(const std::string &Op,
234 bool AllowWholeOp) {
235 if (Op.empty() || Op[0] != '$')
236 throw TheDef->getName() + ": Illegal operand name: '" + Op + "'";
253CodeGenInstruction::ParseOperandName(const std::string &Op,
254 bool AllowWholeOp) {
255 if (Op.empty() || Op[0] != '$')
256 throw TheDef->getName() + ": Illegal operand name: '" + Op + "'";
237
257
238 std::string OpName = Op.substr(1);
239 std::string SubOpName;
258 std::string OpName = Op.substr(1);
259 std::string SubOpName;
240
260
241 // Check to see if this is $foo.bar.
242 std::string::size_type DotIdx = OpName.find_first_of(".");
243 if (DotIdx != std::string::npos) {
244 SubOpName = OpName.substr(DotIdx+1);
245 if (SubOpName.empty())
246 throw TheDef->getName() + ": illegal empty suboperand name in '" +Op +"'";
247 OpName = OpName.substr(0, DotIdx);
248 }
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);
268 }
249
269
250 unsigned OpIdx = getOperandNamed(OpName);
251
252 if (SubOpName.empty()) { // If no suboperand name was specified:
253 // If one was needed, throw.
254 if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp &&
255 SubOpName.empty())
256 throw TheDef->getName() + ": Illegal to refer to"
257 " whole operand part of complex operand '" + Op + "'";
270 unsigned OpIdx = getOperandNamed(OpName);
271
272 if (SubOpName.empty()) { // If no suboperand name was specified:
273 // If one was needed, throw.
274 if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp &&
275 SubOpName.empty())
276 throw TheDef->getName() + ": Illegal to refer to"
277 " whole operand part of complex operand '" + Op + "'";
258
278
259 // Otherwise, return the operand.
260 return std::make_pair(OpIdx, 0U);
261 }
279 // Otherwise, return the operand.
280 return std::make_pair(OpIdx, 0U);
281 }
262
282
263 // Find the suboperand number involved.
264 DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
265 if (MIOpInfo == 0)
266 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
283 // Find the suboperand number involved.
284 DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
285 if (MIOpInfo == 0)
286 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
267
287
268 // Find the operand with the right name.
269 for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i)
270 if (MIOpInfo->getArgName(i) == SubOpName)
271 return std::make_pair(OpIdx, i);
272
273 // Otherwise, didn't find it!
274 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
275}
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);
292
293 // Otherwise, didn't find it!
294 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
295}