blob: 684431a6f49f6d74dd1f5c4317eab5b5d4adfd32 [file] [log] [blame]
Chris Lattner6cc654b2008-01-06 01:35:39 +00001//===- 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//===----------------------------------------------------------------------===//
9//
10// This file implements the CodeGenInstruction class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenInstruction.h"
15#include "Record.h"
16#include "llvm/ADT/StringExtras.h"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000017#include "llvm/ADT/STLExtras.h"
Chris Lattner6cc654b2008-01-06 01:35:39 +000018#include <set>
19using namespace llvm;
20
21static void ParseConstraint(const std::string &CStr, CodeGenInstruction *I) {
Jim Grosbach06801722009-12-16 19:43:02 +000022 // 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);
34
35 // Build the string for the operand
36 std::string OpConstraint = "(1 << TOI::EARLY_CLOBBER)";
37 if (!I->OperandList[Op.first].Constraints[Op.second].empty())
38 throw "Operand '" + Name + "' cannot have multiple constraints!";
39 I->OperandList[Op.first].Constraints[Op.second] = OpConstraint;
40 return;
41 }
42
43 // Only other constraint is "TIED_TO" for now.
Chris Lattner6cc654b2008-01-06 01:35:39 +000044 std::string::size_type pos = CStr.find_first_of('=');
45 assert(pos != std::string::npos && "Unrecognized constraint");
Jim Grosbach06801722009-12-16 19:43:02 +000046 start = CStr.find_first_not_of(" \t");
Bob Wilsonfd87e6a2009-08-26 22:50:39 +000047 std::string Name = CStr.substr(start, pos - start);
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000048
Chris Lattner6cc654b2008-01-06 01:35:39 +000049 // TIED_TO: $src1 = $dst
Jim Grosbach06801722009-12-16 19:43:02 +000050 wpos = Name.find_first_of(" \t");
Chris Lattner6cc654b2008-01-06 01:35:39 +000051 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);
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000055
Chris Lattner6cc654b2008-01-06 01:35:39 +000056 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 + "'";
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000060
Chris Lattner6cc654b2008-01-06 01:35:39 +000061 std::pair<unsigned,unsigned> SrcOp =
62 I->ParseOperandName(Name.substr(wpos), false);
63 if (SrcOp > DestOp)
64 throw "Illegal tied-to operand constraint '" + CStr + "'";
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000065
66
Chris Lattner6cc654b2008-01-06 01:35:39 +000067 unsigned FlatOpNo = I->getFlattenedOperandNumber(SrcOp);
68 // Build the string for the operand.
69 std::string OpConstraint =
70 "((" + utostr(FlatOpNo) + " << 16) | (1 << TOI::TIED_TO))";
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000071
Chris Lattner6cc654b2008-01-06 01:35:39 +000072 if (!I->OperandList[DestOp.first].Constraints[DestOp.second].empty())
73 throw "Operand '" + DestOpName + "' cannot have multiple constraints!";
74 I->OperandList[DestOp.first].Constraints[DestOp.second] = OpConstraint;
75}
76
77static void ParseConstraints(const std::string &CStr, CodeGenInstruction *I) {
78 // Make sure the constraints list for each operand is large enough to hold
79 // constraint info, even if none is present.
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000080 for (unsigned i = 0, e = I->OperandList.size(); i != e; ++i)
Chris Lattner6cc654b2008-01-06 01:35:39 +000081 I->OperandList[i].Constraints.resize(I->OperandList[i].MINumOperands);
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000082
Chris Lattner6cc654b2008-01-06 01:35:39 +000083 if (CStr.empty()) return;
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000084
Chris Lattner6cc654b2008-01-06 01:35:39 +000085 const std::string delims(",");
86 std::string::size_type bidx, eidx;
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000087
Chris Lattner6cc654b2008-01-06 01:35:39 +000088 bidx = CStr.find_first_not_of(delims);
89 while (bidx != std::string::npos) {
90 eidx = CStr.find_first_of(delims, bidx);
91 if (eidx == std::string::npos)
92 eidx = CStr.length();
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000093
Bob Wilsonfd87e6a2009-08-26 22:50:39 +000094 ParseConstraint(CStr.substr(bidx, eidx - bidx), I);
Chris Lattner6cc654b2008-01-06 01:35:39 +000095 bidx = CStr.find_first_not_of(delims, eidx);
96 }
97}
98
99CodeGenInstruction::CodeGenInstruction(Record *R, const std::string &AsmStr)
100 : TheDef(R), AsmString(AsmStr) {
Chris Lattner6cc654b2008-01-06 01:35:39 +0000101 Namespace = R->getValueAsString("Namespace");
102
103 isReturn = R->getValueAsBit("isReturn");
104 isBranch = R->getValueAsBit("isBranch");
105 isIndirectBranch = R->getValueAsBit("isIndirectBranch");
106 isBarrier = R->getValueAsBit("isBarrier");
107 isCall = R->getValueAsBit("isCall");
Dan Gohman15511cf2008-12-03 18:15:48 +0000108 canFoldAsLoad = R->getValueAsBit("canFoldAsLoad");
Chris Lattnerdcc8b4f2008-01-08 18:05:21 +0000109 mayLoad = R->getValueAsBit("mayLoad");
Chris Lattner2e48a702008-01-06 08:36:04 +0000110 mayStore = R->getValueAsBit("mayStore");
Chris Lattner6cc654b2008-01-06 01:35:39 +0000111 bool isTwoAddress = R->getValueAsBit("isTwoAddress");
112 isPredicable = R->getValueAsBit("isPredicable");
113 isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
114 isCommutable = R->getValueAsBit("isCommutable");
115 isTerminator = R->getValueAsBit("isTerminator");
116 isReMaterializable = R->getValueAsBit("isReMaterializable");
117 hasDelaySlot = R->getValueAsBit("hasDelaySlot");
Dan Gohman533297b2009-10-29 18:10:34 +0000118 usesCustomInserter = R->getValueAsBit("usesCustomInserter");
Chris Lattner6cc654b2008-01-06 01:35:39 +0000119 hasCtrlDep = R->getValueAsBit("hasCtrlDep");
120 isNotDuplicable = R->getValueAsBit("isNotDuplicable");
Chris Lattnerba7e7562008-01-10 07:59:24 +0000121 hasSideEffects = R->getValueAsBit("hasSideEffects");
Chris Lattner6cc654b2008-01-06 01:35:39 +0000122 mayHaveSideEffects = R->getValueAsBit("mayHaveSideEffects");
123 neverHasSideEffects = R->getValueAsBit("neverHasSideEffects");
Bill Wendling8370d382008-05-28 22:54:52 +0000124 isAsCheapAsAMove = R->getValueAsBit("isAsCheapAsAMove");
Evan Cheng799d6972009-10-01 08:21:18 +0000125 hasExtraSrcRegAllocReq = R->getValueAsBit("hasExtraSrcRegAllocReq");
126 hasExtraDefRegAllocReq = R->getValueAsBit("hasExtraDefRegAllocReq");
Chris Lattner6cc654b2008-01-06 01:35:39 +0000127 hasOptionalDef = false;
Chris Lattner8f707e12008-01-07 05:19:29 +0000128 isVariadic = false;
Chris Lattner6cc654b2008-01-06 01:35:39 +0000129
Chris Lattnerba7e7562008-01-10 07:59:24 +0000130 if (mayHaveSideEffects + neverHasSideEffects + hasSideEffects > 1)
131 throw R->getName() + ": multiple conflicting side-effect flags set!";
Chris Lattner6cc654b2008-01-06 01:35:39 +0000132
133 DagInit *DI;
134 try {
135 DI = R->getValueAsDag("OutOperandList");
136 } catch (...) {
137 // Error getting operand list, just ignore it (sparcv9).
138 AsmString.clear();
139 OperandList.clear();
140 return;
141 }
142 NumDefs = DI->getNumArgs();
143
144 DagInit *IDI;
145 try {
146 IDI = R->getValueAsDag("InOperandList");
147 } catch (...) {
148 // Error getting operand list, just ignore it (sparcv9).
149 AsmString.clear();
150 OperandList.clear();
151 return;
152 }
David Greenee8cf21e2009-04-23 21:25:15 +0000153 DI = (DagInit*)(new BinOpInit(BinOpInit::CONCAT, DI, IDI, new DagRecTy))->Fold(R, 0);
Chris Lattner6cc654b2008-01-06 01:35:39 +0000154
155 unsigned MIOperandNo = 0;
156 std::set<std::string> OperandNames;
157 for (unsigned i = 0, e = DI->getNumArgs(); i != e; ++i) {
158 DefInit *Arg = dynamic_cast<DefInit*>(DI->getArg(i));
159 if (!Arg)
160 throw "Illegal operand for the '" + R->getName() + "' instruction!";
161
162 Record *Rec = Arg->getDef();
163 std::string PrintMethod = "printOperand";
164 unsigned NumOps = 1;
165 DagInit *MIOpInfo = 0;
166 if (Rec->isSubClassOf("Operand")) {
167 PrintMethod = Rec->getValueAsString("PrintMethod");
168 MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000169
Chris Lattner6cc654b2008-01-06 01:35:39 +0000170 // Verify that MIOpInfo has an 'ops' root value.
171 if (!dynamic_cast<DefInit*>(MIOpInfo->getOperator()) ||
172 dynamic_cast<DefInit*>(MIOpInfo->getOperator())
173 ->getDef()->getName() != "ops")
174 throw "Bad value for MIOperandInfo in operand '" + Rec->getName() +
175 "'\n";
176
177 // If we have MIOpInfo, then we have #operands equal to number of entries
178 // in MIOperandInfo.
179 if (unsigned NumArgs = MIOpInfo->getNumArgs())
180 NumOps = NumArgs;
181
182 if (Rec->isSubClassOf("PredicateOperand"))
183 isPredicable = true;
184 else if (Rec->isSubClassOf("OptionalDefOperand"))
185 hasOptionalDef = true;
186 } else if (Rec->getName() == "variable_ops") {
Chris Lattner8f707e12008-01-07 05:19:29 +0000187 isVariadic = true;
Chris Lattner6cc654b2008-01-06 01:35:39 +0000188 continue;
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000189 } else if (!Rec->isSubClassOf("RegisterClass") &&
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000190 Rec->getName() != "ptr_rc" && Rec->getName() != "unknown")
Chris Lattner6cc654b2008-01-06 01:35:39 +0000191 throw "Unknown operand class '" + Rec->getName() +
Matthijs Kooijman677fbfa2008-10-27 15:59:43 +0000192 "' in '" + R->getName() + "' instruction!";
Chris Lattner6cc654b2008-01-06 01:35:39 +0000193
194 // Check that the operand has a name and that it's unique.
195 if (DI->getArgName(i).empty())
196 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
197 " has no name!";
198 if (!OperandNames.insert(DI->getArgName(i)).second)
199 throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
200 " has the same name as a previous operand!";
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000201
202 OperandList.push_back(OperandInfo(Rec, DI->getArgName(i), PrintMethod,
Chris Lattner6cc654b2008-01-06 01:35:39 +0000203 MIOperandNo, NumOps, MIOpInfo));
204 MIOperandNo += NumOps;
205 }
206
207 // Parse Constraints.
208 ParseConstraints(R->getValueAsString("Constraints"), this);
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000209
Chris Lattner6cc654b2008-01-06 01:35:39 +0000210 // For backward compatibility: isTwoAddress means operand 1 is tied to
211 // operand 0.
212 if (isTwoAddress) {
213 if (!OperandList[1].Constraints[0].empty())
214 throw R->getName() + ": cannot use isTwoAddress property: instruction "
215 "already has constraint set!";
216 OperandList[1].Constraints[0] = "((0 << 16) | (1 << TOI::TIED_TO))";
217 }
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000218
Chris Lattner6cc654b2008-01-06 01:35:39 +0000219 // Any operands with unset constraints get 0 as their constraint.
220 for (unsigned op = 0, e = OperandList.size(); op != e; ++op)
221 for (unsigned j = 0, e = OperandList[op].MINumOperands; j != e; ++j)
222 if (OperandList[op].Constraints[j].empty())
223 OperandList[op].Constraints[j] = "0";
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000224
Chris Lattner6cc654b2008-01-06 01:35:39 +0000225 // Parse the DisableEncoding field.
226 std::string DisableEncoding = R->getValueAsString("DisableEncoding");
227 while (1) {
Benjamin Kramerd4f19592010-01-11 18:03:24 +0000228 std::string OpName;
229 tie(OpName, DisableEncoding) = getToken(DisableEncoding, " ,\t");
Chris Lattner6cc654b2008-01-06 01:35:39 +0000230 if (OpName.empty()) break;
231
232 // Figure out which operand this is.
233 std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false);
234
235 // Mark the operand as not-to-be encoded.
236 if (Op.second >= OperandList[Op.first].DoNotEncode.size())
237 OperandList[Op.first].DoNotEncode.resize(Op.second+1);
238 OperandList[Op.first].DoNotEncode[Op.second] = true;
239 }
240}
241
Chris Lattner6cc654b2008-01-06 01:35:39 +0000242/// getOperandNamed - Return the index of the operand with the specified
243/// non-empty name. If the instruction does not have an operand with the
244/// specified name, throw an exception.
245///
246unsigned CodeGenInstruction::getOperandNamed(const std::string &Name) const {
247 assert(!Name.empty() && "Cannot search for operand with no name!");
248 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
249 if (OperandList[i].Name == Name) return i;
250 throw "Instruction '" + TheDef->getName() +
251 "' does not have an operand named '$" + Name + "'!";
252}
253
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000254std::pair<unsigned,unsigned>
Chris Lattner6cc654b2008-01-06 01:35:39 +0000255CodeGenInstruction::ParseOperandName(const std::string &Op,
256 bool AllowWholeOp) {
257 if (Op.empty() || Op[0] != '$')
258 throw TheDef->getName() + ": Illegal operand name: '" + Op + "'";
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000259
Chris Lattner6cc654b2008-01-06 01:35:39 +0000260 std::string OpName = Op.substr(1);
261 std::string SubOpName;
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000262
Chris Lattner6cc654b2008-01-06 01:35:39 +0000263 // Check to see if this is $foo.bar.
264 std::string::size_type DotIdx = OpName.find_first_of(".");
265 if (DotIdx != std::string::npos) {
266 SubOpName = OpName.substr(DotIdx+1);
267 if (SubOpName.empty())
268 throw TheDef->getName() + ": illegal empty suboperand name in '" +Op +"'";
269 OpName = OpName.substr(0, DotIdx);
270 }
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000271
Chris Lattner6cc654b2008-01-06 01:35:39 +0000272 unsigned OpIdx = getOperandNamed(OpName);
273
274 if (SubOpName.empty()) { // If no suboperand name was specified:
275 // If one was needed, throw.
276 if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp &&
277 SubOpName.empty())
278 throw TheDef->getName() + ": Illegal to refer to"
279 " whole operand part of complex operand '" + Op + "'";
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000280
Chris Lattner6cc654b2008-01-06 01:35:39 +0000281 // Otherwise, return the operand.
282 return std::make_pair(OpIdx, 0U);
283 }
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000284
Chris Lattner6cc654b2008-01-06 01:35:39 +0000285 // Find the suboperand number involved.
286 DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
287 if (MIOpInfo == 0)
288 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000289
Chris Lattner6cc654b2008-01-06 01:35:39 +0000290 // Find the operand with the right name.
291 for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i)
292 if (MIOpInfo->getArgName(i) == SubOpName)
293 return std::make_pair(OpIdx, i);
294
295 // Otherwise, didn't find it!
296 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
297}