blob: c69ce962ffcdde236d6129eba82485ef60d1801a [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"
17#include <set>
18using namespace llvm;
19
20static void ParseConstraint(const std::string &CStr, CodeGenInstruction *I) {
Jim Grosbach06801722009-12-16 19:43:02 +000021 // 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.
Chris Lattner6cc654b2008-01-06 01:35:39 +000043 std::string::size_type pos = CStr.find_first_of('=');
44 assert(pos != std::string::npos && "Unrecognized constraint");
Jim Grosbach06801722009-12-16 19:43:02 +000045 start = CStr.find_first_not_of(" \t");
Bob Wilsonfd87e6a2009-08-26 22:50:39 +000046 std::string Name = CStr.substr(start, pos - start);
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000047
Chris Lattner6cc654b2008-01-06 01:35:39 +000048 // TIED_TO: $src1 = $dst
Jim Grosbach06801722009-12-16 19:43:02 +000049 wpos = Name.find_first_of(" \t");
Chris Lattner6cc654b2008-01-06 01:35:39 +000050 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);
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000054
Chris Lattner6cc654b2008-01-06 01:35:39 +000055 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 + "'";
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000059
Chris Lattner6cc654b2008-01-06 01:35:39 +000060 std::pair<unsigned,unsigned> SrcOp =
61 I->ParseOperandName(Name.substr(wpos), false);
62 if (SrcOp > DestOp)
63 throw "Illegal tied-to operand constraint '" + CStr + "'";
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000064
65
Chris Lattner6cc654b2008-01-06 01:35:39 +000066 unsigned FlatOpNo = I->getFlattenedOperandNumber(SrcOp);
67 // Build the string for the operand.
68 std::string OpConstraint =
69 "((" + utostr(FlatOpNo) + " << 16) | (1 << TOI::TIED_TO))";
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000070
Chris Lattner6cc654b2008-01-06 01:35:39 +000071 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.
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000079 for (unsigned i = 0, e = I->OperandList.size(); i != e; ++i)
Chris Lattner6cc654b2008-01-06 01:35:39 +000080 I->OperandList[i].Constraints.resize(I->OperandList[i].MINumOperands);
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000081
Chris Lattner6cc654b2008-01-06 01:35:39 +000082 if (CStr.empty()) return;
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000083
Chris Lattner6cc654b2008-01-06 01:35:39 +000084 const std::string delims(",");
85 std::string::size_type bidx, eidx;
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000086
Chris Lattner6cc654b2008-01-06 01:35:39 +000087 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();
Jim Grosbachf0a4fad2009-12-15 19:28:13 +000092
Bob Wilsonfd87e6a2009-08-26 22:50:39 +000093 ParseConstraint(CStr.substr(bidx, eidx - bidx), I);
Chris Lattner6cc654b2008-01-06 01:35:39 +000094 bidx = CStr.find_first_not_of(delims, eidx);
95 }
96}
97
98CodeGenInstruction::CodeGenInstruction(Record *R, const std::string &AsmStr)
99 : TheDef(R), AsmString(AsmStr) {
Chris Lattner6cc654b2008-01-06 01:35:39 +0000100 Namespace = R->getValueAsString("Namespace");
101
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");
Dan Gohman15511cf2008-12-03 18:15:48 +0000107 canFoldAsLoad = R->getValueAsBit("canFoldAsLoad");
Chris Lattnerdcc8b4f2008-01-08 18:05:21 +0000108 mayLoad = R->getValueAsBit("mayLoad");
Chris Lattner2e48a702008-01-06 08:36:04 +0000109 mayStore = R->getValueAsBit("mayStore");
Chris Lattner6cc654b2008-01-06 01:35:39 +0000110 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");
Dan Gohman533297b2009-10-29 18:10:34 +0000117 usesCustomInserter = R->getValueAsBit("usesCustomInserter");
Chris Lattner6cc654b2008-01-06 01:35:39 +0000118 hasCtrlDep = R->getValueAsBit("hasCtrlDep");
119 isNotDuplicable = R->getValueAsBit("isNotDuplicable");
Chris Lattnerba7e7562008-01-10 07:59:24 +0000120 hasSideEffects = R->getValueAsBit("hasSideEffects");
Chris Lattner6cc654b2008-01-06 01:35:39 +0000121 mayHaveSideEffects = R->getValueAsBit("mayHaveSideEffects");
122 neverHasSideEffects = R->getValueAsBit("neverHasSideEffects");
Bill Wendling8370d382008-05-28 22:54:52 +0000123 isAsCheapAsAMove = R->getValueAsBit("isAsCheapAsAMove");
Evan Cheng799d6972009-10-01 08:21:18 +0000124 hasExtraSrcRegAllocReq = R->getValueAsBit("hasExtraSrcRegAllocReq");
125 hasExtraDefRegAllocReq = R->getValueAsBit("hasExtraDefRegAllocReq");
Chris Lattner6cc654b2008-01-06 01:35:39 +0000126 hasOptionalDef = false;
Chris Lattner8f707e12008-01-07 05:19:29 +0000127 isVariadic = false;
Chris Lattner6cc654b2008-01-06 01:35:39 +0000128
Chris Lattnerba7e7562008-01-10 07:59:24 +0000129 if (mayHaveSideEffects + neverHasSideEffects + hasSideEffects > 1)
130 throw R->getName() + ": multiple conflicting side-effect flags set!";
Chris Lattner6cc654b2008-01-06 01:35:39 +0000131
132 DagInit *DI;
133 try {
134 DI = R->getValueAsDag("OutOperandList");
135 } catch (...) {
136 // Error getting operand list, just ignore it (sparcv9).
137 AsmString.clear();
138 OperandList.clear();
139 return;
140 }
141 NumDefs = DI->getNumArgs();
142
143 DagInit *IDI;
144 try {
145 IDI = R->getValueAsDag("InOperandList");
146 } catch (...) {
147 // Error getting operand list, just ignore it (sparcv9).
148 AsmString.clear();
149 OperandList.clear();
150 return;
151 }
David Greenee8cf21e2009-04-23 21:25:15 +0000152 DI = (DagInit*)(new BinOpInit(BinOpInit::CONCAT, DI, IDI, new DagRecTy))->Fold(R, 0);
Chris Lattner6cc654b2008-01-06 01:35:39 +0000153
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));
158 if (!Arg)
159 throw "Illegal operand for the '" + R->getName() + "' instruction!";
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");
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000168
Chris Lattner6cc654b2008-01-06 01:35:39 +0000169 // 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") {
Chris Lattner8f707e12008-01-07 05:19:29 +0000186 isVariadic = true;
Chris Lattner6cc654b2008-01-06 01:35:39 +0000187 continue;
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000188 } else if (!Rec->isSubClassOf("RegisterClass") &&
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000189 Rec->getName() != "ptr_rc" && Rec->getName() != "unknown")
Chris Lattner6cc654b2008-01-06 01:35:39 +0000190 throw "Unknown operand class '" + Rec->getName() +
Matthijs Kooijman677fbfa2008-10-27 15:59:43 +0000191 "' in '" + R->getName() + "' instruction!";
Chris Lattner6cc654b2008-01-06 01:35:39 +0000192
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!";
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000200
201 OperandList.push_back(OperandInfo(Rec, DI->getArgName(i), PrintMethod,
Chris Lattner6cc654b2008-01-06 01:35:39 +0000202 MIOperandNo, NumOps, MIOpInfo));
203 MIOperandNo += NumOps;
204 }
205
206 // Parse Constraints.
207 ParseConstraints(R->getValueAsString("Constraints"), this);
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000208
Chris Lattner6cc654b2008-01-06 01:35:39 +0000209 // 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 }
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000217
Chris Lattner6cc654b2008-01-06 01:35:39 +0000218 // 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";
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000223
Chris Lattner6cc654b2008-01-06 01:35:39 +0000224 // 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);
232
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;
237 }
238}
239
Chris Lattner6cc654b2008-01-06 01:35:39 +0000240/// 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.
243///
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
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000252std::pair<unsigned,unsigned>
Chris Lattner6cc654b2008-01-06 01:35:39 +0000253CodeGenInstruction::ParseOperandName(const std::string &Op,
254 bool AllowWholeOp) {
255 if (Op.empty() || Op[0] != '$')
256 throw TheDef->getName() + ": Illegal operand name: '" + Op + "'";
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000257
Chris Lattner6cc654b2008-01-06 01:35:39 +0000258 std::string OpName = Op.substr(1);
259 std::string SubOpName;
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000260
Chris Lattner6cc654b2008-01-06 01:35:39 +0000261 // 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 }
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000269
Chris Lattner6cc654b2008-01-06 01:35:39 +0000270 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 + "'";
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000278
Chris Lattner6cc654b2008-01-06 01:35:39 +0000279 // Otherwise, return the operand.
280 return std::make_pair(OpIdx, 0U);
281 }
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000282
Chris Lattner6cc654b2008-01-06 01:35:39 +0000283 // Find the suboperand number involved.
284 DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
285 if (MIOpInfo == 0)
286 throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
Jim Grosbachf0a4fad2009-12-15 19:28:13 +0000287
Chris Lattner6cc654b2008-01-06 01:35:39 +0000288 // 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}