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" |
Chris Lattner | 9414ae5 | 2010-03-27 20:09:24 +0000 | [diff] [blame] | 15 | #include "CodeGenTarget.h" |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 16 | #include "Record.h" |
| 17 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 3f2c8e4 | 2010-11-06 07:06:09 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringMap.h" |
Benjamin Kramer | d4f1959 | 2010-01-11 18:03:24 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 20 | #include <set> |
| 21 | using namespace llvm; |
| 22 | |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 23 | //===----------------------------------------------------------------------===// |
| 24 | // CGIOperandList Implementation |
| 25 | //===----------------------------------------------------------------------===// |
Jim Grosbach | 0680172 | 2009-12-16 19:43:02 +0000 | [diff] [blame] | 26 | |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 27 | CGIOperandList::CGIOperandList(Record *R) : TheDef(R) { |
| 28 | isPredicable = false; |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 29 | hasOptionalDef = false; |
Chris Lattner | 8f707e1 | 2008-01-07 05:19:29 +0000 | [diff] [blame] | 30 | isVariadic = false; |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 31 | |
Chris Lattner | f55eed2 | 2010-03-18 21:07:39 +0000 | [diff] [blame] | 32 | DagInit *OutDI = R->getValueAsDag("OutOperandList"); |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 33 | |
Chris Lattner | f55eed2 | 2010-03-18 21:07:39 +0000 | [diff] [blame] | 34 | if (DefInit *Init = dynamic_cast<DefInit*>(OutDI->getOperator())) { |
Chris Lattner | b0be4d2 | 2010-03-18 20:56:35 +0000 | [diff] [blame] | 35 | if (Init->getDef()->getName() != "outs") |
Chris Lattner | cedef1c | 2010-03-18 20:50:52 +0000 | [diff] [blame] | 36 | throw R->getName() + ": invalid def name for output list: use 'outs'"; |
| 37 | } else |
| 38 | throw R->getName() + ": invalid output list: use 'outs'"; |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 39 | |
Chris Lattner | f55eed2 | 2010-03-18 21:07:39 +0000 | [diff] [blame] | 40 | NumDefs = OutDI->getNumArgs(); |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 41 | |
Chris Lattner | f55eed2 | 2010-03-18 21:07:39 +0000 | [diff] [blame] | 42 | DagInit *InDI = R->getValueAsDag("InOperandList"); |
| 43 | if (DefInit *Init = dynamic_cast<DefInit*>(InDI->getOperator())) { |
Chris Lattner | b0be4d2 | 2010-03-18 20:56:35 +0000 | [diff] [blame] | 44 | if (Init->getDef()->getName() != "ins") |
Chris Lattner | cedef1c | 2010-03-18 20:50:52 +0000 | [diff] [blame] | 45 | throw R->getName() + ": invalid def name for input list: use 'ins'"; |
| 46 | } else |
| 47 | throw R->getName() + ": invalid input list: use 'ins'"; |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 48 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 49 | unsigned MIOperandNo = 0; |
| 50 | std::set<std::string> OperandNames; |
Chris Lattner | f55eed2 | 2010-03-18 21:07:39 +0000 | [diff] [blame] | 51 | for (unsigned i = 0, e = InDI->getNumArgs()+OutDI->getNumArgs(); i != e; ++i){ |
| 52 | Init *ArgInit; |
| 53 | std::string ArgName; |
| 54 | if (i < NumDefs) { |
| 55 | ArgInit = OutDI->getArg(i); |
| 56 | ArgName = OutDI->getArgName(i); |
| 57 | } else { |
| 58 | ArgInit = InDI->getArg(i-NumDefs); |
| 59 | ArgName = InDI->getArgName(i-NumDefs); |
| 60 | } |
| 61 | |
| 62 | DefInit *Arg = dynamic_cast<DefInit*>(ArgInit); |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 63 | if (!Arg) |
| 64 | throw "Illegal operand for the '" + R->getName() + "' instruction!"; |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 65 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 66 | Record *Rec = Arg->getDef(); |
| 67 | std::string PrintMethod = "printOperand"; |
Jim Grosbach | 5013f74 | 2010-10-12 22:21:57 +0000 | [diff] [blame] | 68 | std::string EncoderMethod; |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 69 | unsigned NumOps = 1; |
| 70 | DagInit *MIOpInfo = 0; |
| 71 | if (Rec->isSubClassOf("Operand")) { |
| 72 | PrintMethod = Rec->getValueAsString("PrintMethod"); |
Jim Grosbach | 5013f74 | 2010-10-12 22:21:57 +0000 | [diff] [blame] | 73 | // If there is an explicit encoder method, use it. |
Chris Lattner | 2ac1902 | 2010-11-15 05:19:05 +0000 | [diff] [blame] | 74 | EncoderMethod = Rec->getValueAsString("EncoderMethod"); |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 75 | MIOpInfo = Rec->getValueAsDag("MIOperandInfo"); |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 76 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 77 | // Verify that MIOpInfo has an 'ops' root value. |
| 78 | if (!dynamic_cast<DefInit*>(MIOpInfo->getOperator()) || |
| 79 | dynamic_cast<DefInit*>(MIOpInfo->getOperator()) |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 80 | ->getDef()->getName() != "ops") |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 81 | throw "Bad value for MIOperandInfo in operand '" + Rec->getName() + |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 82 | "'\n"; |
| 83 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 84 | // If we have MIOpInfo, then we have #operands equal to number of entries |
| 85 | // in MIOperandInfo. |
| 86 | if (unsigned NumArgs = MIOpInfo->getNumArgs()) |
| 87 | NumOps = NumArgs; |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 88 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 89 | if (Rec->isSubClassOf("PredicateOperand")) |
| 90 | isPredicable = true; |
| 91 | else if (Rec->isSubClassOf("OptionalDefOperand")) |
| 92 | hasOptionalDef = true; |
| 93 | } else if (Rec->getName() == "variable_ops") { |
Chris Lattner | 8f707e1 | 2008-01-07 05:19:29 +0000 | [diff] [blame] | 94 | isVariadic = true; |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 95 | continue; |
Jim Grosbach | f0a4fad | 2009-12-15 19:28:13 +0000 | [diff] [blame] | 96 | } else if (!Rec->isSubClassOf("RegisterClass") && |
Dan Gohman | e4c67cd | 2008-05-31 02:11:25 +0000 | [diff] [blame] | 97 | Rec->getName() != "ptr_rc" && Rec->getName() != "unknown") |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 98 | throw "Unknown operand class '" + Rec->getName() + |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 99 | "' in '" + R->getName() + "' instruction!"; |
| 100 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 101 | // Check that the operand has a name and that it's unique. |
Chris Lattner | f55eed2 | 2010-03-18 21:07:39 +0000 | [diff] [blame] | 102 | if (ArgName.empty()) |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 103 | throw "In instruction '" + R->getName() + "', operand #" + utostr(i) + |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 104 | " has no name!"; |
Chris Lattner | f55eed2 | 2010-03-18 21:07:39 +0000 | [diff] [blame] | 105 | if (!OperandNames.insert(ArgName).second) |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 106 | throw "In instruction '" + R->getName() + "', operand #" + utostr(i) + |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 107 | " has the same name as a previous operand!"; |
| 108 | |
Jim Grosbach | 5013f74 | 2010-10-12 22:21:57 +0000 | [diff] [blame] | 109 | OperandList.push_back(OperandInfo(Rec, ArgName, PrintMethod, EncoderMethod, |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 110 | MIOperandNo, NumOps, MIOpInfo)); |
| 111 | MIOperandNo += NumOps; |
| 112 | } |
Chris Lattner | b501d4f | 2010-11-01 05:34:34 +0000 | [diff] [blame] | 113 | |
| 114 | |
| 115 | // Make sure the constraints list for each operand is large enough to hold |
| 116 | // constraint info, even if none is present. |
| 117 | for (unsigned i = 0, e = OperandList.size(); i != e; ++i) |
| 118 | OperandList[i].Constraints.resize(OperandList[i].MINumOperands); |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 121 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 122 | /// getOperandNamed - Return the index of the operand with the specified |
| 123 | /// non-empty name. If the instruction does not have an operand with the |
| 124 | /// specified name, throw an exception. |
| 125 | /// |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 126 | unsigned CGIOperandList::getOperandNamed(StringRef Name) const { |
Jim Grosbach | 0185507 | 2010-10-11 18:25:51 +0000 | [diff] [blame] | 127 | unsigned OpIdx; |
| 128 | if (hasOperandNamed(Name, OpIdx)) return OpIdx; |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 129 | throw "'" + TheDef->getName() + "' does not have an operand named '$" + |
| 130 | Name.str() + "'!"; |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Jim Grosbach | 0185507 | 2010-10-11 18:25:51 +0000 | [diff] [blame] | 133 | /// hasOperandNamed - Query whether the instruction has an operand of the |
| 134 | /// given name. If so, return true and set OpIdx to the index of the |
| 135 | /// operand. Otherwise, return false. |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 136 | bool CGIOperandList::hasOperandNamed(StringRef Name, unsigned &OpIdx) const { |
Jim Grosbach | 0185507 | 2010-10-11 18:25:51 +0000 | [diff] [blame] | 137 | assert(!Name.empty() && "Cannot search for operand with no name!"); |
| 138 | for (unsigned i = 0, e = OperandList.size(); i != e; ++i) |
| 139 | if (OperandList[i].Name == Name) { |
| 140 | OpIdx = i; |
| 141 | return true; |
| 142 | } |
| 143 | return false; |
| 144 | } |
| 145 | |
Jim Grosbach | f0a4fad | 2009-12-15 19:28:13 +0000 | [diff] [blame] | 146 | std::pair<unsigned,unsigned> |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 147 | CGIOperandList::ParseOperandName(const std::string &Op, bool AllowWholeOp) { |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 148 | if (Op.empty() || Op[0] != '$') |
| 149 | throw TheDef->getName() + ": Illegal operand name: '" + Op + "'"; |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 150 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 151 | std::string OpName = Op.substr(1); |
| 152 | std::string SubOpName; |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 153 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 154 | // Check to see if this is $foo.bar. |
| 155 | std::string::size_type DotIdx = OpName.find_first_of("."); |
| 156 | if (DotIdx != std::string::npos) { |
| 157 | SubOpName = OpName.substr(DotIdx+1); |
| 158 | if (SubOpName.empty()) |
| 159 | throw TheDef->getName() + ": illegal empty suboperand name in '" +Op +"'"; |
| 160 | OpName = OpName.substr(0, DotIdx); |
| 161 | } |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 162 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 163 | unsigned OpIdx = getOperandNamed(OpName); |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 164 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 165 | if (SubOpName.empty()) { // If no suboperand name was specified: |
| 166 | // If one was needed, throw. |
| 167 | if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp && |
| 168 | SubOpName.empty()) |
| 169 | throw TheDef->getName() + ": Illegal to refer to" |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 170 | " whole operand part of complex operand '" + Op + "'"; |
| 171 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 172 | // Otherwise, return the operand. |
| 173 | return std::make_pair(OpIdx, 0U); |
| 174 | } |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 175 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 176 | // Find the suboperand number involved. |
| 177 | DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo; |
| 178 | if (MIOpInfo == 0) |
| 179 | throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'"; |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 180 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 181 | // Find the operand with the right name. |
| 182 | for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i) |
| 183 | if (MIOpInfo->getArgName(i) == SubOpName) |
| 184 | return std::make_pair(OpIdx, i); |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 185 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 186 | // Otherwise, didn't find it! |
| 187 | throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'"; |
| 188 | } |
Chris Lattner | 9414ae5 | 2010-03-27 20:09:24 +0000 | [diff] [blame] | 189 | |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 190 | static void ParseConstraint(const std::string &CStr, CGIOperandList &Ops) { |
| 191 | // EARLY_CLOBBER: @early $reg |
| 192 | std::string::size_type wpos = CStr.find_first_of(" \t"); |
| 193 | std::string::size_type start = CStr.find_first_not_of(" \t"); |
| 194 | std::string Tok = CStr.substr(start, wpos - start); |
| 195 | if (Tok == "@earlyclobber") { |
| 196 | std::string Name = CStr.substr(wpos+1); |
| 197 | wpos = Name.find_first_not_of(" \t"); |
| 198 | if (wpos == std::string::npos) |
| 199 | throw "Illegal format for @earlyclobber constraint: '" + CStr + "'"; |
| 200 | Name = Name.substr(wpos); |
| 201 | std::pair<unsigned,unsigned> Op = Ops.ParseOperandName(Name, false); |
| 202 | |
| 203 | // Build the string for the operand |
| 204 | if (!Ops[Op.first].Constraints[Op.second].isNone()) |
| 205 | throw "Operand '" + Name + "' cannot have multiple constraints!"; |
| 206 | Ops[Op.first].Constraints[Op.second] = |
| 207 | CGIOperandList::ConstraintInfo::getEarlyClobber(); |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | // Only other constraint is "TIED_TO" for now. |
| 212 | std::string::size_type pos = CStr.find_first_of('='); |
| 213 | assert(pos != std::string::npos && "Unrecognized constraint"); |
| 214 | start = CStr.find_first_not_of(" \t"); |
| 215 | std::string Name = CStr.substr(start, pos - start); |
| 216 | |
| 217 | // TIED_TO: $src1 = $dst |
| 218 | wpos = Name.find_first_of(" \t"); |
| 219 | if (wpos == std::string::npos) |
| 220 | throw "Illegal format for tied-to constraint: '" + CStr + "'"; |
| 221 | std::string DestOpName = Name.substr(0, wpos); |
| 222 | std::pair<unsigned,unsigned> DestOp = Ops.ParseOperandName(DestOpName, false); |
| 223 | |
| 224 | Name = CStr.substr(pos+1); |
| 225 | wpos = Name.find_first_not_of(" \t"); |
| 226 | if (wpos == std::string::npos) |
| 227 | throw "Illegal format for tied-to constraint: '" + CStr + "'"; |
| 228 | |
| 229 | std::pair<unsigned,unsigned> SrcOp = |
| 230 | Ops.ParseOperandName(Name.substr(wpos), false); |
| 231 | if (SrcOp > DestOp) |
| 232 | throw "Illegal tied-to operand constraint '" + CStr + "'"; |
| 233 | |
| 234 | |
| 235 | unsigned FlatOpNo = Ops.getFlattenedOperandNumber(SrcOp); |
| 236 | |
| 237 | if (!Ops[DestOp.first].Constraints[DestOp.second].isNone()) |
| 238 | throw "Operand '" + DestOpName + "' cannot have multiple constraints!"; |
| 239 | Ops[DestOp.first].Constraints[DestOp.second] = |
| 240 | CGIOperandList::ConstraintInfo::getTied(FlatOpNo); |
| 241 | } |
| 242 | |
| 243 | static void ParseConstraints(const std::string &CStr, CGIOperandList &Ops) { |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 244 | if (CStr.empty()) return; |
| 245 | |
| 246 | const std::string delims(","); |
| 247 | std::string::size_type bidx, eidx; |
| 248 | |
| 249 | bidx = CStr.find_first_not_of(delims); |
| 250 | while (bidx != std::string::npos) { |
| 251 | eidx = CStr.find_first_of(delims, bidx); |
| 252 | if (eidx == std::string::npos) |
| 253 | eidx = CStr.length(); |
| 254 | |
| 255 | ParseConstraint(CStr.substr(bidx, eidx - bidx), Ops); |
| 256 | bidx = CStr.find_first_not_of(delims, eidx); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | void CGIOperandList::ProcessDisableEncoding(std::string DisableEncoding) { |
| 261 | while (1) { |
| 262 | std::string OpName; |
| 263 | tie(OpName, DisableEncoding) = getToken(DisableEncoding, " ,\t"); |
| 264 | if (OpName.empty()) break; |
| 265 | |
| 266 | // Figure out which operand this is. |
| 267 | std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false); |
| 268 | |
| 269 | // Mark the operand as not-to-be encoded. |
| 270 | if (Op.second >= OperandList[Op.first].DoNotEncode.size()) |
| 271 | OperandList[Op.first].DoNotEncode.resize(Op.second+1); |
| 272 | OperandList[Op.first].DoNotEncode[Op.second] = true; |
| 273 | } |
| 274 | |
| 275 | } |
| 276 | |
| 277 | //===----------------------------------------------------------------------===// |
| 278 | // CodeGenInstruction Implementation |
| 279 | //===----------------------------------------------------------------------===// |
| 280 | |
| 281 | CodeGenInstruction::CodeGenInstruction(Record *R) : TheDef(R), Operands(R) { |
| 282 | Namespace = R->getValueAsString("Namespace"); |
| 283 | AsmString = R->getValueAsString("AsmString"); |
| 284 | |
| 285 | isReturn = R->getValueAsBit("isReturn"); |
| 286 | isBranch = R->getValueAsBit("isBranch"); |
| 287 | isIndirectBranch = R->getValueAsBit("isIndirectBranch"); |
| 288 | isCompare = R->getValueAsBit("isCompare"); |
Evan Cheng | c4af463 | 2010-11-17 20:13:28 +0000 | [diff] [blame] | 289 | isMoveImm = R->getValueAsBit("isMoveImm"); |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 290 | isBarrier = R->getValueAsBit("isBarrier"); |
| 291 | isCall = R->getValueAsBit("isCall"); |
| 292 | canFoldAsLoad = R->getValueAsBit("canFoldAsLoad"); |
| 293 | mayLoad = R->getValueAsBit("mayLoad"); |
| 294 | mayStore = R->getValueAsBit("mayStore"); |
| 295 | isPredicable = Operands.isPredicable || R->getValueAsBit("isPredicable"); |
| 296 | isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress"); |
| 297 | isCommutable = R->getValueAsBit("isCommutable"); |
| 298 | isTerminator = R->getValueAsBit("isTerminator"); |
| 299 | isReMaterializable = R->getValueAsBit("isReMaterializable"); |
| 300 | hasDelaySlot = R->getValueAsBit("hasDelaySlot"); |
| 301 | usesCustomInserter = R->getValueAsBit("usesCustomInserter"); |
| 302 | hasCtrlDep = R->getValueAsBit("hasCtrlDep"); |
| 303 | isNotDuplicable = R->getValueAsBit("isNotDuplicable"); |
| 304 | hasSideEffects = R->getValueAsBit("hasSideEffects"); |
| 305 | neverHasSideEffects = R->getValueAsBit("neverHasSideEffects"); |
| 306 | isAsCheapAsAMove = R->getValueAsBit("isAsCheapAsAMove"); |
| 307 | hasExtraSrcRegAllocReq = R->getValueAsBit("hasExtraSrcRegAllocReq"); |
| 308 | hasExtraDefRegAllocReq = R->getValueAsBit("hasExtraDefRegAllocReq"); |
| 309 | ImplicitDefs = R->getValueAsListOfDefs("Defs"); |
| 310 | ImplicitUses = R->getValueAsListOfDefs("Uses"); |
| 311 | |
| 312 | if (neverHasSideEffects + hasSideEffects > 1) |
| 313 | throw R->getName() + ": multiple conflicting side-effect flags set!"; |
| 314 | |
| 315 | // Parse Constraints. |
| 316 | ParseConstraints(R->getValueAsString("Constraints"), Operands); |
| 317 | |
| 318 | // Parse the DisableEncoding field. |
| 319 | Operands.ProcessDisableEncoding(R->getValueAsString("DisableEncoding")); |
| 320 | } |
Chris Lattner | 9414ae5 | 2010-03-27 20:09:24 +0000 | [diff] [blame] | 321 | |
| 322 | /// HasOneImplicitDefWithKnownVT - If the instruction has at least one |
| 323 | /// implicit def and it has a known VT, return the VT, otherwise return |
| 324 | /// MVT::Other. |
| 325 | MVT::SimpleValueType CodeGenInstruction:: |
| 326 | HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const { |
| 327 | if (ImplicitDefs.empty()) return MVT::Other; |
| 328 | |
| 329 | // Check to see if the first implicit def has a resolvable type. |
| 330 | Record *FirstImplicitDef = ImplicitDefs[0]; |
| 331 | assert(FirstImplicitDef->isSubClassOf("Register")); |
| 332 | const std::vector<MVT::SimpleValueType> &RegVTs = |
| 333 | TargetInfo.getRegisterVTs(FirstImplicitDef); |
| 334 | if (RegVTs.size() == 1) |
| 335 | return RegVTs[0]; |
| 336 | return MVT::Other; |
| 337 | } |
| 338 | |
Chris Lattner | 4d43d0f | 2010-11-01 01:07:14 +0000 | [diff] [blame] | 339 | |
| 340 | /// FlattenAsmStringVariants - Flatten the specified AsmString to only |
| 341 | /// include text from the specified variant, returning the new string. |
| 342 | std::string CodeGenInstruction:: |
| 343 | FlattenAsmStringVariants(StringRef Cur, unsigned Variant) { |
| 344 | std::string Res = ""; |
| 345 | |
| 346 | for (;;) { |
| 347 | // Find the start of the next variant string. |
| 348 | size_t VariantsStart = 0; |
| 349 | for (size_t e = Cur.size(); VariantsStart != e; ++VariantsStart) |
| 350 | if (Cur[VariantsStart] == '{' && |
| 351 | (VariantsStart == 0 || (Cur[VariantsStart-1] != '$' && |
| 352 | Cur[VariantsStart-1] != '\\'))) |
| 353 | break; |
| 354 | |
| 355 | // Add the prefix to the result. |
| 356 | Res += Cur.slice(0, VariantsStart); |
| 357 | if (VariantsStart == Cur.size()) |
| 358 | break; |
| 359 | |
| 360 | ++VariantsStart; // Skip the '{'. |
| 361 | |
| 362 | // Scan to the end of the variants string. |
| 363 | size_t VariantsEnd = VariantsStart; |
| 364 | unsigned NestedBraces = 1; |
| 365 | for (size_t e = Cur.size(); VariantsEnd != e; ++VariantsEnd) { |
| 366 | if (Cur[VariantsEnd] == '}' && Cur[VariantsEnd-1] != '\\') { |
| 367 | if (--NestedBraces == 0) |
| 368 | break; |
| 369 | } else if (Cur[VariantsEnd] == '{') |
| 370 | ++NestedBraces; |
| 371 | } |
| 372 | |
| 373 | // Select the Nth variant (or empty). |
| 374 | StringRef Selection = Cur.slice(VariantsStart, VariantsEnd); |
| 375 | for (unsigned i = 0; i != Variant; ++i) |
| 376 | Selection = Selection.split('|').second; |
| 377 | Res += Selection.split('|').first; |
| 378 | |
| 379 | assert(VariantsEnd != Cur.size() && |
| 380 | "Unterminated variants in assembly string!"); |
| 381 | Cur = Cur.substr(VariantsEnd + 1); |
| 382 | } |
| 383 | |
| 384 | return Res; |
| 385 | } |
| 386 | |
Chris Lattner | c76e80d | 2010-11-01 04:05:41 +0000 | [diff] [blame] | 387 | |
| 388 | //===----------------------------------------------------------------------===// |
| 389 | /// CodeGenInstAlias Implementation |
| 390 | //===----------------------------------------------------------------------===// |
| 391 | |
Chris Lattner | 662e5a3 | 2010-11-06 07:14:44 +0000 | [diff] [blame] | 392 | CodeGenInstAlias::CodeGenInstAlias(Record *R, CodeGenTarget &T) : TheDef(R) { |
Chris Lattner | c76e80d | 2010-11-01 04:05:41 +0000 | [diff] [blame] | 393 | AsmString = R->getValueAsString("AsmString"); |
Chris Lattner | b501d4f | 2010-11-01 05:34:34 +0000 | [diff] [blame] | 394 | Result = R->getValueAsDag("ResultInst"); |
Chris Lattner | 225549f | 2010-11-06 06:39:47 +0000 | [diff] [blame] | 395 | |
| 396 | // Verify that the root of the result is an instruction. |
| 397 | DefInit *DI = dynamic_cast<DefInit*>(Result->getOperator()); |
| 398 | if (DI == 0 || !DI->getDef()->isSubClassOf("Instruction")) |
| 399 | throw TGError(R->getLoc(), "result of inst alias should be an instruction"); |
| 400 | |
| 401 | ResultInst = &T.getInstruction(DI->getDef()); |
Chris Lattner | d0f225c | 2010-11-06 06:54:38 +0000 | [diff] [blame] | 402 | |
Chris Lattner | 3f2c8e4 | 2010-11-06 07:06:09 +0000 | [diff] [blame] | 403 | // NameClass - If argument names are repeated, we need to verify they have |
| 404 | // the same class. |
| 405 | StringMap<Record*> NameClass; |
Chris Lattner | d0f225c | 2010-11-06 06:54:38 +0000 | [diff] [blame] | 406 | |
| 407 | // Decode and validate the arguments of the result. |
Chris Lattner | 4140985 | 2010-11-06 07:31:43 +0000 | [diff] [blame] | 408 | unsigned AliasOpNo = 0; |
| 409 | for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) { |
| 410 | // Tied registers don't have an entry in the result dag. |
| 411 | if (ResultInst->Operands[i].getTiedRegister() != -1) |
| 412 | continue; |
| 413 | |
| 414 | if (AliasOpNo >= Result->getNumArgs()) |
| 415 | throw TGError(R->getLoc(), "result has " + utostr(Result->getNumArgs()) + |
| 416 | " arguments, but " + ResultInst->TheDef->getName() + |
| 417 | " instruction expects " + |
| 418 | utostr(ResultInst->Operands.size()) + " operands!"); |
| 419 | |
| 420 | |
| 421 | Init *Arg = Result->getArg(AliasOpNo); |
Chris Lattner | 90fd797 | 2010-11-06 19:57:21 +0000 | [diff] [blame] | 422 | Record *ResultOpRec = ResultInst->Operands[i].Rec; |
| 423 | |
| 424 | // Handle explicit registers. |
| 425 | if (DefInit *ADI = dynamic_cast<DefInit*>(Arg)) { |
| 426 | if (ADI->getDef()->isSubClassOf("Register")) { |
| 427 | if (!Result->getArgName(AliasOpNo).empty()) |
| 428 | throw TGError(R->getLoc(), "result fixed register argument must " |
| 429 | "not have a name!"); |
| 430 | |
| 431 | if (!ResultOpRec->isSubClassOf("RegisterClass")) |
| 432 | throw TGError(R->getLoc(), "result fixed register argument is not " |
| 433 | "passed to a RegisterClass operand!"); |
| 434 | |
| 435 | if (!T.getRegisterClass(ResultOpRec).containsRegister(ADI->getDef())) |
| 436 | throw TGError(R->getLoc(), "fixed register " +ADI->getDef()->getName() |
| 437 | + " is not a member of the " + ResultOpRec->getName() + |
| 438 | " register class!"); |
| 439 | |
| 440 | // Now that it is validated, add it. |
| 441 | ResultOperands.push_back(ResultOperand(ADI->getDef())); |
| 442 | ++AliasOpNo; |
| 443 | continue; |
| 444 | } |
| 445 | } |
Chris Lattner | d0f225c | 2010-11-06 06:54:38 +0000 | [diff] [blame] | 446 | |
| 447 | // If the operand is a record, it must have a name, and the record type must |
| 448 | // match up with the instruction's argument type. |
| 449 | if (DefInit *ADI = dynamic_cast<DefInit*>(Arg)) { |
Chris Lattner | 4140985 | 2010-11-06 07:31:43 +0000 | [diff] [blame] | 450 | if (Result->getArgName(AliasOpNo).empty()) |
| 451 | throw TGError(R->getLoc(), "result argument #" + utostr(AliasOpNo) + |
Chris Lattner | d0f225c | 2010-11-06 06:54:38 +0000 | [diff] [blame] | 452 | " must have a name!"); |
| 453 | |
Chris Lattner | 90fd797 | 2010-11-06 19:57:21 +0000 | [diff] [blame] | 454 | if (ADI->getDef() != ResultOpRec) |
Chris Lattner | 4140985 | 2010-11-06 07:31:43 +0000 | [diff] [blame] | 455 | throw TGError(R->getLoc(), "result argument #" + utostr(AliasOpNo) + |
Chris Lattner | d0f225c | 2010-11-06 06:54:38 +0000 | [diff] [blame] | 456 | " declared with class " + ADI->getDef()->getName() + |
| 457 | ", instruction operand is class " + |
Chris Lattner | 90fd797 | 2010-11-06 19:57:21 +0000 | [diff] [blame] | 458 | ResultOpRec->getName()); |
Chris Lattner | d0f225c | 2010-11-06 06:54:38 +0000 | [diff] [blame] | 459 | |
Chris Lattner | 3f2c8e4 | 2010-11-06 07:06:09 +0000 | [diff] [blame] | 460 | // Verify we don't have something like: (someinst GR16:$foo, GR32:$foo) |
| 461 | // $foo can exist multiple times in the result list, but it must have the |
| 462 | // same type. |
Chris Lattner | 4140985 | 2010-11-06 07:31:43 +0000 | [diff] [blame] | 463 | Record *&Entry = NameClass[Result->getArgName(AliasOpNo)]; |
Chris Lattner | 3f2c8e4 | 2010-11-06 07:06:09 +0000 | [diff] [blame] | 464 | if (Entry && Entry != ADI->getDef()) |
Chris Lattner | 4140985 | 2010-11-06 07:31:43 +0000 | [diff] [blame] | 465 | throw TGError(R->getLoc(), "result value $" + |
| 466 | Result->getArgName(AliasOpNo) + |
Chris Lattner | 3f2c8e4 | 2010-11-06 07:06:09 +0000 | [diff] [blame] | 467 | " is both " + Entry->getName() + " and " + |
| 468 | ADI->getDef()->getName() + "!"); |
| 469 | |
Chris Lattner | d0f225c | 2010-11-06 06:54:38 +0000 | [diff] [blame] | 470 | // Now that it is validated, add it. |
Chris Lattner | 4140985 | 2010-11-06 07:31:43 +0000 | [diff] [blame] | 471 | ResultOperands.push_back(ResultOperand(Result->getArgName(AliasOpNo), |
Chris Lattner | d0f225c | 2010-11-06 06:54:38 +0000 | [diff] [blame] | 472 | ADI->getDef())); |
Chris Lattner | 4140985 | 2010-11-06 07:31:43 +0000 | [diff] [blame] | 473 | ++AliasOpNo; |
Chris Lattner | d0f225c | 2010-11-06 06:54:38 +0000 | [diff] [blame] | 474 | continue; |
| 475 | } |
Chris Lattner | 98c870f | 2010-11-06 19:25:43 +0000 | [diff] [blame] | 476 | |
| 477 | if (IntInit *II = dynamic_cast<IntInit*>(Arg)) { |
| 478 | // Integer arguments can't have names. |
| 479 | if (!Result->getArgName(AliasOpNo).empty()) |
| 480 | throw TGError(R->getLoc(), "result argument #" + utostr(AliasOpNo) + |
| 481 | " must not have a name!"); |
| 482 | if (ResultInst->Operands[i].MINumOperands != 1 || |
Chris Lattner | 90fd797 | 2010-11-06 19:57:21 +0000 | [diff] [blame] | 483 | !ResultOpRec->isSubClassOf("Operand")) |
Chris Lattner | 98c870f | 2010-11-06 19:25:43 +0000 | [diff] [blame] | 484 | throw TGError(R->getLoc(), "invalid argument class " + |
Chris Lattner | 90fd797 | 2010-11-06 19:57:21 +0000 | [diff] [blame] | 485 | ResultOpRec->getName() + |
Chris Lattner | 98c870f | 2010-11-06 19:25:43 +0000 | [diff] [blame] | 486 | " for integer result operand!"); |
| 487 | ResultOperands.push_back(ResultOperand(II->getValue())); |
| 488 | ++AliasOpNo; |
| 489 | continue; |
| 490 | } |
Chris Lattner | d0f225c | 2010-11-06 06:54:38 +0000 | [diff] [blame] | 491 | |
| 492 | throw TGError(R->getLoc(), "result of inst alias has unknown operand type"); |
| 493 | } |
Chris Lattner | 4140985 | 2010-11-06 07:31:43 +0000 | [diff] [blame] | 494 | |
| 495 | if (AliasOpNo != Result->getNumArgs()) |
| 496 | throw TGError(R->getLoc(), "result has " + utostr(Result->getNumArgs()) + |
| 497 | " arguments, but " + ResultInst->TheDef->getName() + |
| 498 | " instruction expects " + utostr(ResultInst->Operands.size())+ |
| 499 | " operands!"); |
Chris Lattner | c76e80d | 2010-11-01 04:05:41 +0000 | [diff] [blame] | 500 | } |
Chris Lattner | 5bde734 | 2010-11-06 08:20:59 +0000 | [diff] [blame] | 501 | |
| 502 | /// getResultInstOperandIndexForResultOperandIndex - Given an index into the |
| 503 | /// ResultOperands array, translate it to a valid index in ResultInst's |
| 504 | /// operand list. |
| 505 | unsigned CodeGenInstAlias:: |
| 506 | getResultInstOperandIndexForResultOperandIndex(unsigned OpNo) const { |
| 507 | unsigned OpIdx = 0; |
| 508 | |
| 509 | for (unsigned i = 0;; ++i) { |
| 510 | assert(i != ResultInst->Operands.size() && "Didn't find entry"); |
| 511 | if (ResultInst->Operands[i].getTiedRegister() != -1) |
| 512 | continue; |
| 513 | |
| 514 | if (OpIdx == OpNo) return i; |
| 515 | |
| 516 | ++OpIdx; |
| 517 | } |
| 518 | } |