Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 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 <set> |
| 18 | using namespace llvm; |
| 19 | |
| 20 | static void ParseConstraint(const std::string &CStr, CodeGenInstruction *I) { |
Jim Grosbach | 0680172 | 2009-12-16 19:43:02 +0000 | [diff] [blame^] | 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. |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 43 | std::string::size_type pos = CStr.find_first_of('='); |
| 44 | assert(pos != std::string::npos && "Unrecognized constraint"); |
Jim Grosbach | 0680172 | 2009-12-16 19:43:02 +0000 | [diff] [blame^] | 45 | start = CStr.find_first_not_of(" \t"); |
Bob Wilson | fd87e6a | 2009-08-26 22:50:39 +0000 | [diff] [blame] | 46 | std::string Name = CStr.substr(start, pos - start); |
Jim Grosbach | f0a4fad | 2009-12-15 19:28:13 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 48 | // TIED_TO: $src1 = $dst |
Jim Grosbach | 0680172 | 2009-12-16 19:43:02 +0000 | [diff] [blame^] | 49 | wpos = Name.find_first_of(" \t"); |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 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); |
Jim Grosbach | f0a4fad | 2009-12-15 19:28:13 +0000 | [diff] [blame] | 54 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 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 + "'"; |
Jim Grosbach | f0a4fad | 2009-12-15 19:28:13 +0000 | [diff] [blame] | 59 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 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 + "'"; |
Jim Grosbach | f0a4fad | 2009-12-15 19:28:13 +0000 | [diff] [blame] | 64 | |
| 65 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 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))"; |
Jim Grosbach | f0a4fad | 2009-12-15 19:28:13 +0000 | [diff] [blame] | 70 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 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 | |
| 76 | static 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 Grosbach | f0a4fad | 2009-12-15 19:28:13 +0000 | [diff] [blame] | 79 | for (unsigned i = 0, e = I->OperandList.size(); i != e; ++i) |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 80 | I->OperandList[i].Constraints.resize(I->OperandList[i].MINumOperands); |
Jim Grosbach | f0a4fad | 2009-12-15 19:28:13 +0000 | [diff] [blame] | 81 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 82 | if (CStr.empty()) return; |
Jim Grosbach | f0a4fad | 2009-12-15 19:28:13 +0000 | [diff] [blame] | 83 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 84 | const std::string delims(","); |
| 85 | std::string::size_type bidx, eidx; |
Jim Grosbach | f0a4fad | 2009-12-15 19:28:13 +0000 | [diff] [blame] | 86 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 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(); |
Jim Grosbach | f0a4fad | 2009-12-15 19:28:13 +0000 | [diff] [blame] | 92 | |
Bob Wilson | fd87e6a | 2009-08-26 22:50:39 +0000 | [diff] [blame] | 93 | ParseConstraint(CStr.substr(bidx, eidx - bidx), I); |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 94 | bidx = CStr.find_first_not_of(delims, eidx); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | CodeGenInstruction::CodeGenInstruction(Record *R, const std::string &AsmStr) |
| 99 | : TheDef(R), AsmString(AsmStr) { |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 100 | 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 Gohman | 15511cf | 2008-12-03 18:15:48 +0000 | [diff] [blame] | 107 | canFoldAsLoad = R->getValueAsBit("canFoldAsLoad"); |
Chris Lattner | dcc8b4f | 2008-01-08 18:05:21 +0000 | [diff] [blame] | 108 | mayLoad = R->getValueAsBit("mayLoad"); |
Chris Lattner | 2e48a70 | 2008-01-06 08:36:04 +0000 | [diff] [blame] | 109 | mayStore = R->getValueAsBit("mayStore"); |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 110 | 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 Gohman | 533297b | 2009-10-29 18:10:34 +0000 | [diff] [blame] | 117 | usesCustomInserter = R->getValueAsBit("usesCustomInserter"); |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 118 | hasCtrlDep = R->getValueAsBit("hasCtrlDep"); |
| 119 | isNotDuplicable = R->getValueAsBit("isNotDuplicable"); |
Chris Lattner | ba7e756 | 2008-01-10 07:59:24 +0000 | [diff] [blame] | 120 | hasSideEffects = R->getValueAsBit("hasSideEffects"); |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 121 | mayHaveSideEffects = R->getValueAsBit("mayHaveSideEffects"); |
| 122 | neverHasSideEffects = R->getValueAsBit("neverHasSideEffects"); |
Bill Wendling | 8370d38 | 2008-05-28 22:54:52 +0000 | [diff] [blame] | 123 | isAsCheapAsAMove = R->getValueAsBit("isAsCheapAsAMove"); |
Evan Cheng | 799d697 | 2009-10-01 08:21:18 +0000 | [diff] [blame] | 124 | hasExtraSrcRegAllocReq = R->getValueAsBit("hasExtraSrcRegAllocReq"); |
| 125 | hasExtraDefRegAllocReq = R->getValueAsBit("hasExtraDefRegAllocReq"); |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 126 | hasOptionalDef = false; |
Chris Lattner | 8f707e1 | 2008-01-07 05:19:29 +0000 | [diff] [blame] | 127 | isVariadic = false; |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 128 | |
Chris Lattner | ba7e756 | 2008-01-10 07:59:24 +0000 | [diff] [blame] | 129 | if (mayHaveSideEffects + neverHasSideEffects + hasSideEffects > 1) |
| 130 | throw R->getName() + ": multiple conflicting side-effect flags set!"; |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 131 | |
| 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 Greene | e8cf21e | 2009-04-23 21:25:15 +0000 | [diff] [blame] | 152 | DI = (DagInit*)(new BinOpInit(BinOpInit::CONCAT, DI, IDI, new DagRecTy))->Fold(R, 0); |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 153 | |
| 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 Grosbach | f0a4fad | 2009-12-15 19:28:13 +0000 | [diff] [blame] | 168 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 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") { |
Chris Lattner | 8f707e1 | 2008-01-07 05:19:29 +0000 | [diff] [blame] | 186 | isVariadic = true; |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 187 | continue; |
Jim Grosbach | f0a4fad | 2009-12-15 19:28:13 +0000 | [diff] [blame] | 188 | } else if (!Rec->isSubClassOf("RegisterClass") && |
Dan Gohman | e4c67cd | 2008-05-31 02:11:25 +0000 | [diff] [blame] | 189 | Rec->getName() != "ptr_rc" && Rec->getName() != "unknown") |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 190 | throw "Unknown operand class '" + Rec->getName() + |
Matthijs Kooijman | 677fbfa | 2008-10-27 15:59:43 +0000 | [diff] [blame] | 191 | "' in '" + R->getName() + "' instruction!"; |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 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!"; |
Jim Grosbach | f0a4fad | 2009-12-15 19:28:13 +0000 | [diff] [blame] | 200 | |
| 201 | OperandList.push_back(OperandInfo(Rec, DI->getArgName(i), PrintMethod, |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 202 | MIOperandNo, NumOps, MIOpInfo)); |
| 203 | MIOperandNo += NumOps; |
| 204 | } |
| 205 | |
| 206 | // Parse Constraints. |
| 207 | ParseConstraints(R->getValueAsString("Constraints"), this); |
Jim Grosbach | f0a4fad | 2009-12-15 19:28:13 +0000 | [diff] [blame] | 208 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 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 | } |
Jim Grosbach | f0a4fad | 2009-12-15 19:28:13 +0000 | [diff] [blame] | 217 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 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"; |
Jim Grosbach | f0a4fad | 2009-12-15 19:28:13 +0000 | [diff] [blame] | 223 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 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); |
| 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 Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 240 | /// 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 | /// |
| 244 | unsigned 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 Grosbach | f0a4fad | 2009-12-15 19:28:13 +0000 | [diff] [blame] | 252 | std::pair<unsigned,unsigned> |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 253 | CodeGenInstruction::ParseOperandName(const std::string &Op, |
| 254 | bool AllowWholeOp) { |
| 255 | if (Op.empty() || Op[0] != '$') |
| 256 | throw TheDef->getName() + ": Illegal operand name: '" + Op + "'"; |
Jim Grosbach | f0a4fad | 2009-12-15 19:28:13 +0000 | [diff] [blame] | 257 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 258 | std::string OpName = Op.substr(1); |
| 259 | std::string SubOpName; |
Jim Grosbach | f0a4fad | 2009-12-15 19:28:13 +0000 | [diff] [blame] | 260 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 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 | } |
Jim Grosbach | f0a4fad | 2009-12-15 19:28:13 +0000 | [diff] [blame] | 269 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 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 + "'"; |
Jim Grosbach | f0a4fad | 2009-12-15 19:28:13 +0000 | [diff] [blame] | 278 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 279 | // Otherwise, return the operand. |
| 280 | return std::make_pair(OpIdx, 0U); |
| 281 | } |
Jim Grosbach | f0a4fad | 2009-12-15 19:28:13 +0000 | [diff] [blame] | 282 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 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 + "'"; |
Jim Grosbach | f0a4fad | 2009-12-15 19:28:13 +0000 | [diff] [blame] | 287 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 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 | } |