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 // FIXME: Only supports TIED_TO for now.
22 std::string::size_type pos
= CStr
.find_first_of('=');
23 assert(pos
!= std::string::npos
&& "Unrecognized constraint");
24 std::string::size_type start
= CStr
.find_first_not_of(" \t");
25 std::string Name
= CStr
.substr(start
, pos
- start
);
27 // TIED_TO: $src1 = $dst
28 std::string::size_type 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);
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
+ "'";
39 std::pair
<unsigned,unsigned> SrcOp
=
40 I
->ParseOperandName(Name
.substr(wpos
), false);
42 throw "Illegal tied-to operand constraint '" + CStr
+ "'";
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))";
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
;
56 static 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.
59 for (unsigned i
= 0, e
= I
->OperandList
.size(); i
!= e
; ++i
)
60 I
->OperandList
[i
].Constraints
.resize(I
->OperandList
[i
].MINumOperands
);
62 if (CStr
.empty()) return;
64 const std::string
delims(",");
65 std::string::size_type bidx
, eidx
;
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
)
73 ParseConstraint(CStr
.substr(bidx
, eidx
- bidx
), I
);
74 bidx
= CStr
.find_first_not_of(delims
, eidx
);
78 CodeGenInstruction::CodeGenInstruction(Record
*R
, const std::string
&AsmStr
)
79 : TheDef(R
), AsmString(AsmStr
) {
80 Namespace
= R
->getValueAsString("Namespace");
82 isReturn
= R
->getValueAsBit("isReturn");
83 isBranch
= R
->getValueAsBit("isBranch");
84 isIndirectBranch
= R
->getValueAsBit("isIndirectBranch");
85 isBarrier
= R
->getValueAsBit("isBarrier");
86 isCall
= R
->getValueAsBit("isCall");
87 canFoldAsLoad
= R
->getValueAsBit("canFoldAsLoad");
88 mayLoad
= R
->getValueAsBit("mayLoad");
89 mayStore
= R
->getValueAsBit("mayStore");
90 bool isTwoAddress
= R
->getValueAsBit("isTwoAddress");
91 isPredicable
= R
->getValueAsBit("isPredicable");
92 isConvertibleToThreeAddress
= R
->getValueAsBit("isConvertibleToThreeAddress");
93 isCommutable
= R
->getValueAsBit("isCommutable");
94 isTerminator
= R
->getValueAsBit("isTerminator");
95 isReMaterializable
= R
->getValueAsBit("isReMaterializable");
96 hasDelaySlot
= R
->getValueAsBit("hasDelaySlot");
97 usesCustomDAGSchedInserter
= R
->getValueAsBit("usesCustomDAGSchedInserter");
98 hasCtrlDep
= R
->getValueAsBit("hasCtrlDep");
99 isNotDuplicable
= R
->getValueAsBit("isNotDuplicable");
100 hasSideEffects
= R
->getValueAsBit("hasSideEffects");
101 mayHaveSideEffects
= R
->getValueAsBit("mayHaveSideEffects");
102 neverHasSideEffects
= R
->getValueAsBit("neverHasSideEffects");
103 isAsCheapAsAMove
= R
->getValueAsBit("isAsCheapAsAMove");
104 hasOptionalDef
= false;
107 if (mayHaveSideEffects
+ neverHasSideEffects
+ hasSideEffects
> 1)
108 throw R
->getName() + ": multiple conflicting side-effect flags set!";
112 DI
= R
->getValueAsDag("OutOperandList");
114 // Error getting operand list, just ignore it (sparcv9).
119 NumDefs
= DI
->getNumArgs();
123 IDI
= R
->getValueAsDag("InOperandList");
125 // Error getting operand list, just ignore it (sparcv9).
130 DI
= (DagInit
*)(new BinOpInit(BinOpInit::CONCAT
, DI
, IDI
, new DagRecTy
))->Fold(R
, 0);
132 unsigned MIOperandNo
= 0;
133 std::set
<std::string
> OperandNames
;
134 for (unsigned i
= 0, e
= DI
->getNumArgs(); i
!= e
; ++i
) {
135 DefInit
*Arg
= dynamic_cast<DefInit
*>(DI
->getArg(i
));
137 throw "Illegal operand for the '" + R
->getName() + "' instruction!";
139 Record
*Rec
= Arg
->getDef();
140 std::string PrintMethod
= "printOperand";
142 DagInit
*MIOpInfo
= 0;
143 if (Rec
->isSubClassOf("Operand")) {
144 PrintMethod
= Rec
->getValueAsString("PrintMethod");
145 MIOpInfo
= Rec
->getValueAsDag("MIOperandInfo");
147 // Verify that MIOpInfo has an 'ops' root value.
148 if (!dynamic_cast<DefInit
*>(MIOpInfo
->getOperator()) ||
149 dynamic_cast<DefInit
*>(MIOpInfo
->getOperator())
150 ->getDef()->getName() != "ops")
151 throw "Bad value for MIOperandInfo in operand '" + Rec
->getName() +
154 // If we have MIOpInfo, then we have #operands equal to number of entries
156 if (unsigned NumArgs
= MIOpInfo
->getNumArgs())
159 if (Rec
->isSubClassOf("PredicateOperand"))
161 else if (Rec
->isSubClassOf("OptionalDefOperand"))
162 hasOptionalDef
= true;
163 } else if (Rec
->getName() == "variable_ops") {
166 } else if (!Rec
->isSubClassOf("RegisterClass") &&
167 Rec
->getName() != "ptr_rc" && Rec
->getName() != "unknown")
168 throw "Unknown operand class '" + Rec
->getName() +
169 "' in '" + R
->getName() + "' instruction!";
171 // Check that the operand has a name and that it's unique.
172 if (DI
->getArgName(i
).empty())
173 throw "In instruction '" + R
->getName() + "', operand #" + utostr(i
) +
175 if (!OperandNames
.insert(DI
->getArgName(i
)).second
)
176 throw "In instruction '" + R
->getName() + "', operand #" + utostr(i
) +
177 " has the same name as a previous operand!";
179 OperandList
.push_back(OperandInfo(Rec
, DI
->getArgName(i
), PrintMethod
,
180 MIOperandNo
, NumOps
, MIOpInfo
));
181 MIOperandNo
+= NumOps
;
184 // Parse Constraints.
185 ParseConstraints(R
->getValueAsString("Constraints"), this);
187 // For backward compatibility: isTwoAddress means operand 1 is tied to
190 if (!OperandList
[1].Constraints
[0].empty())
191 throw R
->getName() + ": cannot use isTwoAddress property: instruction "
192 "already has constraint set!";
193 OperandList
[1].Constraints
[0] = "((0 << 16) | (1 << TOI::TIED_TO))";
196 // Any operands with unset constraints get 0 as their constraint.
197 for (unsigned op
= 0, e
= OperandList
.size(); op
!= e
; ++op
)
198 for (unsigned j
= 0, e
= OperandList
[op
].MINumOperands
; j
!= e
; ++j
)
199 if (OperandList
[op
].Constraints
[j
].empty())
200 OperandList
[op
].Constraints
[j
] = "0";
202 // Parse the DisableEncoding field.
203 std::string DisableEncoding
= R
->getValueAsString("DisableEncoding");
205 std::string OpName
= getToken(DisableEncoding
, " ,\t");
206 if (OpName
.empty()) break;
208 // Figure out which operand this is.
209 std::pair
<unsigned,unsigned> Op
= ParseOperandName(OpName
, false);
211 // Mark the operand as not-to-be encoded.
212 if (Op
.second
>= OperandList
[Op
.first
].DoNotEncode
.size())
213 OperandList
[Op
.first
].DoNotEncode
.resize(Op
.second
+1);
214 OperandList
[Op
.first
].DoNotEncode
[Op
.second
] = true;
218 /// getOperandNamed - Return the index of the operand with the specified
219 /// non-empty name. If the instruction does not have an operand with the
220 /// specified name, throw an exception.
222 unsigned CodeGenInstruction::getOperandNamed(const std::string
&Name
) const {
223 assert(!Name
.empty() && "Cannot search for operand with no name!");
224 for (unsigned i
= 0, e
= OperandList
.size(); i
!= e
; ++i
)
225 if (OperandList
[i
].Name
== Name
) return i
;
226 throw "Instruction '" + TheDef
->getName() +
227 "' does not have an operand named '$" + Name
+ "'!";
230 std::pair
<unsigned,unsigned>
231 CodeGenInstruction::ParseOperandName(const std::string
&Op
,
233 if (Op
.empty() || Op
[0] != '$')
234 throw TheDef
->getName() + ": Illegal operand name: '" + Op
+ "'";
236 std::string OpName
= Op
.substr(1);
237 std::string SubOpName
;
239 // Check to see if this is $foo.bar.
240 std::string::size_type DotIdx
= OpName
.find_first_of(".");
241 if (DotIdx
!= std::string::npos
) {
242 SubOpName
= OpName
.substr(DotIdx
+1);
243 if (SubOpName
.empty())
244 throw TheDef
->getName() + ": illegal empty suboperand name in '" +Op
+"'";
245 OpName
= OpName
.substr(0, DotIdx
);
248 unsigned OpIdx
= getOperandNamed(OpName
);
250 if (SubOpName
.empty()) { // If no suboperand name was specified:
251 // If one was needed, throw.
252 if (OperandList
[OpIdx
].MINumOperands
> 1 && !AllowWholeOp
&&
254 throw TheDef
->getName() + ": Illegal to refer to"
255 " whole operand part of complex operand '" + Op
+ "'";
257 // Otherwise, return the operand.
258 return std::make_pair(OpIdx
, 0U);
261 // Find the suboperand number involved.
262 DagInit
*MIOpInfo
= OperandList
[OpIdx
].MIOperandInfo
;
264 throw TheDef
->getName() + ": unknown suboperand name in '" + Op
+ "'";
266 // Find the operand with the right name.
267 for (unsigned i
= 0, e
= MIOpInfo
->getNumArgs(); i
!= e
; ++i
)
268 if (MIOpInfo
->getArgName(i
) == SubOpName
)
269 return std::make_pair(OpIdx
, i
);
271 // Otherwise, didn't find it!
272 throw TheDef
->getName() + ": unknown suboperand name in '" + Op
+ "'";