rewrite this to not artificially force concat the ins/outs list.
[oota-llvm.git] / utils / TableGen / CodeGenInstruction.cpp
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 //===----------------------------------------------------------------------===//
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 "llvm/ADT/STLExtras.h"
18 #include <set>
19 using namespace llvm;
20
21 static void ParseConstraint(const std::string &CStr, CodeGenInstruction *I) {
22   // 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     if (!I->OperandList[Op.first].Constraints[Op.second].isNone())
37       throw "Operand '" + Name + "' cannot have multiple constraints!";
38     I->OperandList[Op.first].Constraints[Op.second] =
39       CodeGenInstruction::ConstraintInfo::getEarlyClobber();
40     return;
41   }
42
43   // Only other constraint is "TIED_TO" for now.
44   std::string::size_type pos = CStr.find_first_of('=');
45   assert(pos != std::string::npos && "Unrecognized constraint");
46   start = CStr.find_first_not_of(" \t");
47   std::string Name = CStr.substr(start, pos - start);
48
49   // TIED_TO: $src1 = $dst
50   wpos = Name.find_first_of(" \t");
51   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);
55
56   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 + "'";
60
61   std::pair<unsigned,unsigned> SrcOp =
62   I->ParseOperandName(Name.substr(wpos), false);
63   if (SrcOp > DestOp)
64     throw "Illegal tied-to operand constraint '" + CStr + "'";
65
66
67   unsigned FlatOpNo = I->getFlattenedOperandNumber(SrcOp);
68
69   if (!I->OperandList[DestOp.first].Constraints[DestOp.second].isNone())
70     throw "Operand '" + DestOpName + "' cannot have multiple constraints!";
71   I->OperandList[DestOp.first].Constraints[DestOp.second] =
72     CodeGenInstruction::ConstraintInfo::getTied(FlatOpNo);
73 }
74
75 static void ParseConstraints(const std::string &CStr, CodeGenInstruction *I) {
76   // Make sure the constraints list for each operand is large enough to hold
77   // constraint info, even if none is present.
78   for (unsigned i = 0, e = I->OperandList.size(); i != e; ++i)
79     I->OperandList[i].Constraints.resize(I->OperandList[i].MINumOperands);
80
81   if (CStr.empty()) return;
82
83   const std::string delims(",");
84   std::string::size_type bidx, eidx;
85
86   bidx = CStr.find_first_not_of(delims);
87   while (bidx != std::string::npos) {
88     eidx = CStr.find_first_of(delims, bidx);
89     if (eidx == std::string::npos)
90       eidx = CStr.length();
91
92     ParseConstraint(CStr.substr(bidx, eidx - bidx), I);
93     bidx = CStr.find_first_not_of(delims, eidx);
94   }
95 }
96
97 CodeGenInstruction::CodeGenInstruction(Record *R, const std::string &AsmStr)
98   : TheDef(R), AsmString(AsmStr) {
99   Namespace = R->getValueAsString("Namespace");
100
101   isReturn     = R->getValueAsBit("isReturn");
102   isBranch     = R->getValueAsBit("isBranch");
103   isIndirectBranch = R->getValueAsBit("isIndirectBranch");
104   isBarrier    = R->getValueAsBit("isBarrier");
105   isCall       = R->getValueAsBit("isCall");
106   canFoldAsLoad = R->getValueAsBit("canFoldAsLoad");
107   mayLoad      = R->getValueAsBit("mayLoad");
108   mayStore     = R->getValueAsBit("mayStore");
109   bool isTwoAddress = R->getValueAsBit("isTwoAddress");
110   isPredicable = R->getValueAsBit("isPredicable");
111   isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress");
112   isCommutable = R->getValueAsBit("isCommutable");
113   isTerminator = R->getValueAsBit("isTerminator");
114   isReMaterializable = R->getValueAsBit("isReMaterializable");
115   hasDelaySlot = R->getValueAsBit("hasDelaySlot");
116   usesCustomInserter = R->getValueAsBit("usesCustomInserter");
117   hasCtrlDep   = R->getValueAsBit("hasCtrlDep");
118   isNotDuplicable = R->getValueAsBit("isNotDuplicable");
119   hasSideEffects = R->getValueAsBit("hasSideEffects");
120   neverHasSideEffects = R->getValueAsBit("neverHasSideEffects");
121   isAsCheapAsAMove = R->getValueAsBit("isAsCheapAsAMove");
122   hasExtraSrcRegAllocReq = R->getValueAsBit("hasExtraSrcRegAllocReq");
123   hasExtraDefRegAllocReq = R->getValueAsBit("hasExtraDefRegAllocReq");
124   hasOptionalDef = false;
125   isVariadic = false;
126
127   if (neverHasSideEffects + hasSideEffects > 1)
128     throw R->getName() + ": multiple conflicting side-effect flags set!";
129
130   DagInit *OutDI = R->getValueAsDag("OutOperandList");
131
132   if (DefInit *Init = dynamic_cast<DefInit*>(OutDI->getOperator())) {
133     if (Init->getDef()->getName() != "outs")
134       throw R->getName() + ": invalid def name for output list: use 'outs'";
135   } else
136     throw R->getName() + ": invalid output list: use 'outs'";
137     
138   NumDefs = OutDI->getNumArgs();
139     
140   DagInit *InDI = R->getValueAsDag("InOperandList");
141   if (DefInit *Init = dynamic_cast<DefInit*>(InDI->getOperator())) {
142     if (Init->getDef()->getName() != "ins")
143       throw R->getName() + ": invalid def name for input list: use 'ins'";
144   } else
145     throw R->getName() + ": invalid input list: use 'ins'";
146     
147   unsigned MIOperandNo = 0;
148   std::set<std::string> OperandNames;
149   for (unsigned i = 0, e = InDI->getNumArgs()+OutDI->getNumArgs(); i != e; ++i){
150     Init *ArgInit;
151     std::string ArgName;
152     if (i < NumDefs) {
153       ArgInit = OutDI->getArg(i);
154       ArgName = OutDI->getArgName(i);
155     } else {
156       ArgInit = InDI->getArg(i-NumDefs);
157       ArgName = InDI->getArgName(i-NumDefs);
158     }
159     
160     DefInit *Arg = dynamic_cast<DefInit*>(ArgInit);
161     if (!Arg)
162       throw "Illegal operand for the '" + R->getName() + "' instruction!";
163
164     Record *Rec = Arg->getDef();
165     std::string PrintMethod = "printOperand";
166     unsigned NumOps = 1;
167     DagInit *MIOpInfo = 0;
168     if (Rec->isSubClassOf("Operand")) {
169       PrintMethod = Rec->getValueAsString("PrintMethod");
170       MIOpInfo = Rec->getValueAsDag("MIOperandInfo");
171
172       // Verify that MIOpInfo has an 'ops' root value.
173       if (!dynamic_cast<DefInit*>(MIOpInfo->getOperator()) ||
174           dynamic_cast<DefInit*>(MIOpInfo->getOperator())
175                ->getDef()->getName() != "ops")
176         throw "Bad value for MIOperandInfo in operand '" + Rec->getName() +
177               "'\n";
178
179       // If we have MIOpInfo, then we have #operands equal to number of entries
180       // in MIOperandInfo.
181       if (unsigned NumArgs = MIOpInfo->getNumArgs())
182         NumOps = NumArgs;
183
184       if (Rec->isSubClassOf("PredicateOperand"))
185         isPredicable = true;
186       else if (Rec->isSubClassOf("OptionalDefOperand"))
187         hasOptionalDef = true;
188     } else if (Rec->getName() == "variable_ops") {
189       isVariadic = true;
190       continue;
191     } else if (!Rec->isSubClassOf("RegisterClass") &&
192                Rec->getName() != "ptr_rc" && Rec->getName() != "unknown")
193       throw "Unknown operand class '" + Rec->getName() +
194             "' in '" + R->getName() + "' instruction!";
195
196     // Check that the operand has a name and that it's unique.
197     if (ArgName.empty())
198       throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
199         " has no name!";
200     if (!OperandNames.insert(ArgName).second)
201       throw "In instruction '" + R->getName() + "', operand #" + utostr(i) +
202         " has the same name as a previous operand!";
203
204     OperandList.push_back(OperandInfo(Rec, ArgName, PrintMethod,
205                                       MIOperandNo, NumOps, MIOpInfo));
206     MIOperandNo += NumOps;
207   }
208
209   // Parse Constraints.
210   ParseConstraints(R->getValueAsString("Constraints"), this);
211
212   // For backward compatibility: isTwoAddress means operand 1 is tied to
213   // operand 0.
214   if (isTwoAddress) {
215     if (!OperandList[1].Constraints[0].isNone())
216       throw R->getName() + ": cannot use isTwoAddress property: instruction "
217             "already has constraint set!";
218     OperandList[1].Constraints[0] =
219       CodeGenInstruction::ConstraintInfo::getTied(0);
220   }
221
222   // Parse the DisableEncoding field.
223   std::string DisableEncoding = R->getValueAsString("DisableEncoding");
224   while (1) {
225     std::string OpName;
226     tie(OpName, DisableEncoding) = getToken(DisableEncoding, " ,\t");
227     if (OpName.empty()) break;
228
229     // Figure out which operand this is.
230     std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false);
231
232     // Mark the operand as not-to-be encoded.
233     if (Op.second >= OperandList[Op.first].DoNotEncode.size())
234       OperandList[Op.first].DoNotEncode.resize(Op.second+1);
235     OperandList[Op.first].DoNotEncode[Op.second] = true;
236   }
237 }
238
239 /// getOperandNamed - Return the index of the operand with the specified
240 /// non-empty name.  If the instruction does not have an operand with the
241 /// specified name, throw an exception.
242 ///
243 unsigned CodeGenInstruction::getOperandNamed(const std::string &Name) const {
244   assert(!Name.empty() && "Cannot search for operand with no name!");
245   for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
246     if (OperandList[i].Name == Name) return i;
247   throw "Instruction '" + TheDef->getName() +
248         "' does not have an operand named '$" + Name + "'!";
249 }
250
251 std::pair<unsigned,unsigned>
252 CodeGenInstruction::ParseOperandName(const std::string &Op,
253                                      bool AllowWholeOp) {
254   if (Op.empty() || Op[0] != '$')
255     throw TheDef->getName() + ": Illegal operand name: '" + Op + "'";
256
257   std::string OpName = Op.substr(1);
258   std::string SubOpName;
259
260   // Check to see if this is $foo.bar.
261   std::string::size_type DotIdx = OpName.find_first_of(".");
262   if (DotIdx != std::string::npos) {
263     SubOpName = OpName.substr(DotIdx+1);
264     if (SubOpName.empty())
265       throw TheDef->getName() + ": illegal empty suboperand name in '" +Op +"'";
266     OpName = OpName.substr(0, DotIdx);
267   }
268
269   unsigned OpIdx = getOperandNamed(OpName);
270
271   if (SubOpName.empty()) {  // If no suboperand name was specified:
272     // If one was needed, throw.
273     if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp &&
274         SubOpName.empty())
275       throw TheDef->getName() + ": Illegal to refer to"
276             " whole operand part of complex operand '" + Op + "'";
277
278     // Otherwise, return the operand.
279     return std::make_pair(OpIdx, 0U);
280   }
281
282   // Find the suboperand number involved.
283   DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo;
284   if (MIOpInfo == 0)
285     throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
286
287   // Find the operand with the right name.
288   for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i)
289     if (MIOpInfo->getArgName(i) == SubOpName)
290       return std::make_pair(OpIdx, i);
291
292   // Otherwise, didn't find it!
293   throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'";
294 }