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" |
Peter Collingbourne | 7c78888 | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 16 | #include "llvm/TableGen/Error.h" |
| 17 | #include "llvm/TableGen/Record.h" |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 3f2c8e4 | 2010-11-06 07:06:09 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringMap.h" |
Benjamin Kramer | d4f1959 | 2010-01-11 18:03:24 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 21 | #include <set> |
| 22 | using namespace llvm; |
| 23 | |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===// |
| 25 | // CGIOperandList Implementation |
| 26 | //===----------------------------------------------------------------------===// |
Jim Grosbach | 0680172 | 2009-12-16 19:43:02 +0000 | [diff] [blame] | 27 | |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 28 | CGIOperandList::CGIOperandList(Record *R) : TheDef(R) { |
| 29 | isPredicable = false; |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 30 | hasOptionalDef = false; |
Chris Lattner | 8f707e1 | 2008-01-07 05:19:29 +0000 | [diff] [blame] | 31 | isVariadic = false; |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 32 | |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 33 | DagInit *OutDI = R->getValueAsDag("OutOperandList"); |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 34 | |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 35 | if (DefInit *Init = dynamic_cast<DefInit*>(OutDI->getOperator())) { |
Chris Lattner | b0be4d2 | 2010-03-18 20:56:35 +0000 | [diff] [blame] | 36 | if (Init->getDef()->getName() != "outs") |
Chris Lattner | cedef1c | 2010-03-18 20:50:52 +0000 | [diff] [blame] | 37 | throw R->getName() + ": invalid def name for output list: use 'outs'"; |
| 38 | } else |
| 39 | throw R->getName() + ": invalid output list: use 'outs'"; |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 40 | |
Chris Lattner | f55eed2 | 2010-03-18 21:07:39 +0000 | [diff] [blame] | 41 | NumDefs = OutDI->getNumArgs(); |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 42 | |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 43 | DagInit *InDI = R->getValueAsDag("InOperandList"); |
| 44 | if (DefInit *Init = dynamic_cast<DefInit*>(InDI->getOperator())) { |
Chris Lattner | b0be4d2 | 2010-03-18 20:56:35 +0000 | [diff] [blame] | 45 | if (Init->getDef()->getName() != "ins") |
Chris Lattner | cedef1c | 2010-03-18 20:50:52 +0000 | [diff] [blame] | 46 | throw R->getName() + ": invalid def name for input list: use 'ins'"; |
| 47 | } else |
| 48 | throw R->getName() + ": invalid input list: use 'ins'"; |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 49 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 50 | unsigned MIOperandNo = 0; |
| 51 | std::set<std::string> OperandNames; |
Chris Lattner | f55eed2 | 2010-03-18 21:07:39 +0000 | [diff] [blame] | 52 | for (unsigned i = 0, e = InDI->getNumArgs()+OutDI->getNumArgs(); i != e; ++i){ |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 53 | Init *ArgInit; |
Chris Lattner | f55eed2 | 2010-03-18 21:07:39 +0000 | [diff] [blame] | 54 | std::string ArgName; |
| 55 | if (i < NumDefs) { |
| 56 | ArgInit = OutDI->getArg(i); |
| 57 | ArgName = OutDI->getArgName(i); |
| 58 | } else { |
| 59 | ArgInit = InDI->getArg(i-NumDefs); |
| 60 | ArgName = InDI->getArgName(i-NumDefs); |
| 61 | } |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 62 | |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 63 | DefInit *Arg = dynamic_cast<DefInit*>(ArgInit); |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 64 | if (!Arg) |
| 65 | throw "Illegal operand for the '" + R->getName() + "' instruction!"; |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 66 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 67 | Record *Rec = Arg->getDef(); |
| 68 | std::string PrintMethod = "printOperand"; |
Jim Grosbach | 5013f74 | 2010-10-12 22:21:57 +0000 | [diff] [blame] | 69 | std::string EncoderMethod; |
Benjamin Kramer | 5196c12 | 2011-07-14 21:47:18 +0000 | [diff] [blame] | 70 | std::string OperandType = "OPERAND_UNKNOWN"; |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 71 | unsigned NumOps = 1; |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 72 | DagInit *MIOpInfo = 0; |
Owen Anderson | bea6f61 | 2011-06-27 21:06:21 +0000 | [diff] [blame] | 73 | if (Rec->isSubClassOf("RegisterOperand")) { |
| 74 | PrintMethod = Rec->getValueAsString("PrintMethod"); |
| 75 | } else if (Rec->isSubClassOf("Operand")) { |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 76 | PrintMethod = Rec->getValueAsString("PrintMethod"); |
Benjamin Kramer | 5196c12 | 2011-07-14 21:47:18 +0000 | [diff] [blame] | 77 | OperandType = Rec->getValueAsString("OperandType"); |
Jim Grosbach | 5013f74 | 2010-10-12 22:21:57 +0000 | [diff] [blame] | 78 | // If there is an explicit encoder method, use it. |
Chris Lattner | 2ac1902 | 2010-11-15 05:19:05 +0000 | [diff] [blame] | 79 | EncoderMethod = Rec->getValueAsString("EncoderMethod"); |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 80 | MIOpInfo = Rec->getValueAsDag("MIOperandInfo"); |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 81 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 82 | // Verify that MIOpInfo has an 'ops' root value. |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 83 | if (!dynamic_cast<DefInit*>(MIOpInfo->getOperator()) || |
| 84 | dynamic_cast<DefInit*>(MIOpInfo->getOperator()) |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 85 | ->getDef()->getName() != "ops") |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 86 | throw "Bad value for MIOperandInfo in operand '" + Rec->getName() + |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 87 | "'\n"; |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 88 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 89 | // If we have MIOpInfo, then we have #operands equal to number of entries |
| 90 | // in MIOperandInfo. |
| 91 | if (unsigned NumArgs = MIOpInfo->getNumArgs()) |
| 92 | NumOps = NumArgs; |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 93 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 94 | if (Rec->isSubClassOf("PredicateOperand")) |
| 95 | isPredicable = true; |
| 96 | else if (Rec->isSubClassOf("OptionalDefOperand")) |
| 97 | hasOptionalDef = true; |
| 98 | } else if (Rec->getName() == "variable_ops") { |
Chris Lattner | 8f707e1 | 2008-01-07 05:19:29 +0000 | [diff] [blame] | 99 | isVariadic = true; |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 100 | continue; |
Benjamin Kramer | 5196c12 | 2011-07-14 21:47:18 +0000 | [diff] [blame] | 101 | } else if (Rec->isSubClassOf("RegisterClass")) { |
| 102 | OperandType = "OPERAND_REGISTER"; |
| 103 | } else if (!Rec->isSubClassOf("PointerLikeRegClass") && |
NAKAMURA Takumi | 36c3bc4 | 2011-01-26 02:03:48 +0000 | [diff] [blame] | 104 | Rec->getName() != "unknown") |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 105 | throw "Unknown operand class '" + Rec->getName() + |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 106 | "' in '" + R->getName() + "' instruction!"; |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 107 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 108 | // Check that the operand has a name and that it's unique. |
Chris Lattner | f55eed2 | 2010-03-18 21:07:39 +0000 | [diff] [blame] | 109 | if (ArgName.empty()) |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 110 | throw "In instruction '" + R->getName() + "', operand #" + utostr(i) + |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 111 | " has no name!"; |
Chris Lattner | f55eed2 | 2010-03-18 21:07:39 +0000 | [diff] [blame] | 112 | if (!OperandNames.insert(ArgName).second) |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 113 | throw "In instruction '" + R->getName() + "', operand #" + utostr(i) + |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 114 | " has the same name as a previous operand!"; |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 115 | |
Jim Grosbach | 5013f74 | 2010-10-12 22:21:57 +0000 | [diff] [blame] | 116 | OperandList.push_back(OperandInfo(Rec, ArgName, PrintMethod, EncoderMethod, |
Benjamin Kramer | 5196c12 | 2011-07-14 21:47:18 +0000 | [diff] [blame] | 117 | OperandType, MIOperandNo, NumOps, |
| 118 | MIOpInfo)); |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 119 | MIOperandNo += NumOps; |
| 120 | } |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 121 | |
| 122 | |
Chris Lattner | b501d4f | 2010-11-01 05:34:34 +0000 | [diff] [blame] | 123 | // Make sure the constraints list for each operand is large enough to hold |
| 124 | // constraint info, even if none is present. |
| 125 | for (unsigned i = 0, e = OperandList.size(); i != e; ++i) |
| 126 | OperandList[i].Constraints.resize(OperandList[i].MINumOperands); |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 129 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 130 | /// getOperandNamed - Return the index of the operand with the specified |
| 131 | /// non-empty name. If the instruction does not have an operand with the |
| 132 | /// specified name, throw an exception. |
| 133 | /// |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 134 | unsigned CGIOperandList::getOperandNamed(StringRef Name) const { |
Jim Grosbach | 0185507 | 2010-10-11 18:25:51 +0000 | [diff] [blame] | 135 | unsigned OpIdx; |
| 136 | if (hasOperandNamed(Name, OpIdx)) return OpIdx; |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 137 | throw "'" + TheDef->getName() + "' does not have an operand named '$" + |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 138 | Name.str() + "'!"; |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Jim Grosbach | 0185507 | 2010-10-11 18:25:51 +0000 | [diff] [blame] | 141 | /// hasOperandNamed - Query whether the instruction has an operand of the |
| 142 | /// given name. If so, return true and set OpIdx to the index of the |
| 143 | /// operand. Otherwise, return false. |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 144 | bool CGIOperandList::hasOperandNamed(StringRef Name, unsigned &OpIdx) const { |
Jim Grosbach | 0185507 | 2010-10-11 18:25:51 +0000 | [diff] [blame] | 145 | assert(!Name.empty() && "Cannot search for operand with no name!"); |
| 146 | for (unsigned i = 0, e = OperandList.size(); i != e; ++i) |
| 147 | if (OperandList[i].Name == Name) { |
| 148 | OpIdx = i; |
| 149 | return true; |
| 150 | } |
| 151 | return false; |
| 152 | } |
| 153 | |
Jim Grosbach | f0a4fad | 2009-12-15 19:28:13 +0000 | [diff] [blame] | 154 | std::pair<unsigned,unsigned> |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 155 | CGIOperandList::ParseOperandName(const std::string &Op, bool AllowWholeOp) { |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 156 | if (Op.empty() || Op[0] != '$') |
| 157 | throw TheDef->getName() + ": Illegal operand name: '" + Op + "'"; |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 158 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 159 | std::string OpName = Op.substr(1); |
| 160 | std::string SubOpName; |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 161 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 162 | // Check to see if this is $foo.bar. |
| 163 | std::string::size_type DotIdx = OpName.find_first_of("."); |
| 164 | if (DotIdx != std::string::npos) { |
| 165 | SubOpName = OpName.substr(DotIdx+1); |
| 166 | if (SubOpName.empty()) |
| 167 | throw TheDef->getName() + ": illegal empty suboperand name in '" +Op +"'"; |
| 168 | OpName = OpName.substr(0, DotIdx); |
| 169 | } |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 170 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 171 | unsigned OpIdx = getOperandNamed(OpName); |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 172 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 173 | if (SubOpName.empty()) { // If no suboperand name was specified: |
| 174 | // If one was needed, throw. |
| 175 | if (OperandList[OpIdx].MINumOperands > 1 && !AllowWholeOp && |
| 176 | SubOpName.empty()) |
| 177 | throw TheDef->getName() + ": Illegal to refer to" |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 178 | " whole operand part of complex operand '" + Op + "'"; |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 179 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 180 | // Otherwise, return the operand. |
| 181 | return std::make_pair(OpIdx, 0U); |
| 182 | } |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 183 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 184 | // Find the suboperand number involved. |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 185 | DagInit *MIOpInfo = OperandList[OpIdx].MIOperandInfo; |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 186 | if (MIOpInfo == 0) |
| 187 | throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'"; |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 188 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 189 | // Find the operand with the right name. |
| 190 | for (unsigned i = 0, e = MIOpInfo->getNumArgs(); i != e; ++i) |
| 191 | if (MIOpInfo->getArgName(i) == SubOpName) |
| 192 | return std::make_pair(OpIdx, i); |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 193 | |
Chris Lattner | 6cc654b | 2008-01-06 01:35:39 +0000 | [diff] [blame] | 194 | // Otherwise, didn't find it! |
| 195 | throw TheDef->getName() + ": unknown suboperand name in '" + Op + "'"; |
| 196 | } |
Chris Lattner | 9414ae5 | 2010-03-27 20:09:24 +0000 | [diff] [blame] | 197 | |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 198 | static void ParseConstraint(const std::string &CStr, CGIOperandList &Ops) { |
| 199 | // EARLY_CLOBBER: @early $reg |
| 200 | std::string::size_type wpos = CStr.find_first_of(" \t"); |
| 201 | std::string::size_type start = CStr.find_first_not_of(" \t"); |
| 202 | std::string Tok = CStr.substr(start, wpos - start); |
| 203 | if (Tok == "@earlyclobber") { |
| 204 | std::string Name = CStr.substr(wpos+1); |
| 205 | wpos = Name.find_first_not_of(" \t"); |
| 206 | if (wpos == std::string::npos) |
| 207 | throw "Illegal format for @earlyclobber constraint: '" + CStr + "'"; |
| 208 | Name = Name.substr(wpos); |
| 209 | std::pair<unsigned,unsigned> Op = Ops.ParseOperandName(Name, false); |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 210 | |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 211 | // Build the string for the operand |
| 212 | if (!Ops[Op.first].Constraints[Op.second].isNone()) |
| 213 | throw "Operand '" + Name + "' cannot have multiple constraints!"; |
| 214 | Ops[Op.first].Constraints[Op.second] = |
| 215 | CGIOperandList::ConstraintInfo::getEarlyClobber(); |
| 216 | return; |
| 217 | } |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 218 | |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 219 | // Only other constraint is "TIED_TO" for now. |
| 220 | std::string::size_type pos = CStr.find_first_of('='); |
| 221 | assert(pos != std::string::npos && "Unrecognized constraint"); |
| 222 | start = CStr.find_first_not_of(" \t"); |
| 223 | std::string Name = CStr.substr(start, pos - start); |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 224 | |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 225 | // TIED_TO: $src1 = $dst |
| 226 | wpos = Name.find_first_of(" \t"); |
| 227 | if (wpos == std::string::npos) |
| 228 | throw "Illegal format for tied-to constraint: '" + CStr + "'"; |
| 229 | std::string DestOpName = Name.substr(0, wpos); |
| 230 | std::pair<unsigned,unsigned> DestOp = Ops.ParseOperandName(DestOpName, false); |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 231 | |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 232 | Name = CStr.substr(pos+1); |
| 233 | wpos = Name.find_first_not_of(" \t"); |
| 234 | if (wpos == std::string::npos) |
| 235 | throw "Illegal format for tied-to constraint: '" + CStr + "'"; |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 236 | |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 237 | std::pair<unsigned,unsigned> SrcOp = |
| 238 | Ops.ParseOperandName(Name.substr(wpos), false); |
| 239 | if (SrcOp > DestOp) |
| 240 | throw "Illegal tied-to operand constraint '" + CStr + "'"; |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 241 | |
| 242 | |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 243 | unsigned FlatOpNo = Ops.getFlattenedOperandNumber(SrcOp); |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 244 | |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 245 | if (!Ops[DestOp.first].Constraints[DestOp.second].isNone()) |
| 246 | throw "Operand '" + DestOpName + "' cannot have multiple constraints!"; |
| 247 | Ops[DestOp.first].Constraints[DestOp.second] = |
Jim Grosbach | 2a8cd57 | 2011-11-15 01:05:12 +0000 | [diff] [blame] | 248 | CGIOperandList::ConstraintInfo::getTied(FlatOpNo); |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | static void ParseConstraints(const std::string &CStr, CGIOperandList &Ops) { |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 252 | if (CStr.empty()) return; |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 253 | |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 254 | const std::string delims(","); |
| 255 | std::string::size_type bidx, eidx; |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 256 | |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 257 | bidx = CStr.find_first_not_of(delims); |
| 258 | while (bidx != std::string::npos) { |
| 259 | eidx = CStr.find_first_of(delims, bidx); |
| 260 | if (eidx == std::string::npos) |
| 261 | eidx = CStr.length(); |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 262 | |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 263 | ParseConstraint(CStr.substr(bidx, eidx - bidx), Ops); |
| 264 | bidx = CStr.find_first_not_of(delims, eidx); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | void CGIOperandList::ProcessDisableEncoding(std::string DisableEncoding) { |
| 269 | while (1) { |
Chris Lattner | c30a38f | 2011-07-21 06:21:31 +0000 | [diff] [blame] | 270 | std::pair<StringRef, StringRef> P = getToken(DisableEncoding, " ,\t"); |
| 271 | std::string OpName = P.first; |
| 272 | DisableEncoding = P.second; |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 273 | if (OpName.empty()) break; |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 274 | |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 275 | // Figure out which operand this is. |
| 276 | std::pair<unsigned,unsigned> Op = ParseOperandName(OpName, false); |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 277 | |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 278 | // Mark the operand as not-to-be encoded. |
| 279 | if (Op.second >= OperandList[Op.first].DoNotEncode.size()) |
| 280 | OperandList[Op.first].DoNotEncode.resize(Op.second+1); |
| 281 | OperandList[Op.first].DoNotEncode[Op.second] = true; |
| 282 | } |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 283 | |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | //===----------------------------------------------------------------------===// |
| 287 | // CodeGenInstruction Implementation |
| 288 | //===----------------------------------------------------------------------===// |
| 289 | |
| 290 | CodeGenInstruction::CodeGenInstruction(Record *R) : TheDef(R), Operands(R) { |
| 291 | Namespace = R->getValueAsString("Namespace"); |
| 292 | AsmString = R->getValueAsString("AsmString"); |
| 293 | |
| 294 | isReturn = R->getValueAsBit("isReturn"); |
| 295 | isBranch = R->getValueAsBit("isBranch"); |
| 296 | isIndirectBranch = R->getValueAsBit("isIndirectBranch"); |
| 297 | isCompare = R->getValueAsBit("isCompare"); |
Evan Cheng | c4af463 | 2010-11-17 20:13:28 +0000 | [diff] [blame] | 298 | isMoveImm = R->getValueAsBit("isMoveImm"); |
Evan Cheng | 0f040a2 | 2011-03-15 05:09:26 +0000 | [diff] [blame] | 299 | isBitcast = R->getValueAsBit("isBitcast"); |
Jakob Stoklund Olesen | f2c64ef | 2012-08-16 23:11:47 +0000 | [diff] [blame^] | 300 | isSelect = R->getValueAsBit("isSelect"); |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 301 | isBarrier = R->getValueAsBit("isBarrier"); |
| 302 | isCall = R->getValueAsBit("isCall"); |
| 303 | canFoldAsLoad = R->getValueAsBit("canFoldAsLoad"); |
| 304 | mayLoad = R->getValueAsBit("mayLoad"); |
| 305 | mayStore = R->getValueAsBit("mayStore"); |
| 306 | isPredicable = Operands.isPredicable || R->getValueAsBit("isPredicable"); |
| 307 | isConvertibleToThreeAddress = R->getValueAsBit("isConvertibleToThreeAddress"); |
| 308 | isCommutable = R->getValueAsBit("isCommutable"); |
| 309 | isTerminator = R->getValueAsBit("isTerminator"); |
| 310 | isReMaterializable = R->getValueAsBit("isReMaterializable"); |
| 311 | hasDelaySlot = R->getValueAsBit("hasDelaySlot"); |
| 312 | usesCustomInserter = R->getValueAsBit("usesCustomInserter"); |
Andrew Trick | 83a8031 | 2011-09-20 18:22:31 +0000 | [diff] [blame] | 313 | hasPostISelHook = R->getValueAsBit("hasPostISelHook"); |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 314 | hasCtrlDep = R->getValueAsBit("hasCtrlDep"); |
| 315 | isNotDuplicable = R->getValueAsBit("isNotDuplicable"); |
| 316 | hasSideEffects = R->getValueAsBit("hasSideEffects"); |
| 317 | neverHasSideEffects = R->getValueAsBit("neverHasSideEffects"); |
| 318 | isAsCheapAsAMove = R->getValueAsBit("isAsCheapAsAMove"); |
| 319 | hasExtraSrcRegAllocReq = R->getValueAsBit("hasExtraSrcRegAllocReq"); |
| 320 | hasExtraDefRegAllocReq = R->getValueAsBit("hasExtraDefRegAllocReq"); |
Jim Grosbach | e727d67 | 2011-07-07 00:48:02 +0000 | [diff] [blame] | 321 | isCodeGenOnly = R->getValueAsBit("isCodeGenOnly"); |
Jim Grosbach | 806fcc0 | 2011-07-06 21:33:38 +0000 | [diff] [blame] | 322 | isPseudo = R->getValueAsBit("isPseudo"); |
Chris Lattner | c240bb0 | 2010-11-01 04:03:32 +0000 | [diff] [blame] | 323 | ImplicitDefs = R->getValueAsListOfDefs("Defs"); |
| 324 | ImplicitUses = R->getValueAsListOfDefs("Uses"); |
| 325 | |
| 326 | if (neverHasSideEffects + hasSideEffects > 1) |
| 327 | throw R->getName() + ": multiple conflicting side-effect flags set!"; |
| 328 | |
| 329 | // Parse Constraints. |
| 330 | ParseConstraints(R->getValueAsString("Constraints"), Operands); |
| 331 | |
| 332 | // Parse the DisableEncoding field. |
| 333 | Operands.ProcessDisableEncoding(R->getValueAsString("DisableEncoding")); |
| 334 | } |
Chris Lattner | 9414ae5 | 2010-03-27 20:09:24 +0000 | [diff] [blame] | 335 | |
| 336 | /// HasOneImplicitDefWithKnownVT - If the instruction has at least one |
| 337 | /// implicit def and it has a known VT, return the VT, otherwise return |
| 338 | /// MVT::Other. |
| 339 | MVT::SimpleValueType CodeGenInstruction:: |
| 340 | HasOneImplicitDefWithKnownVT(const CodeGenTarget &TargetInfo) const { |
| 341 | if (ImplicitDefs.empty()) return MVT::Other; |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 342 | |
Chris Lattner | 9414ae5 | 2010-03-27 20:09:24 +0000 | [diff] [blame] | 343 | // Check to see if the first implicit def has a resolvable type. |
| 344 | Record *FirstImplicitDef = ImplicitDefs[0]; |
| 345 | assert(FirstImplicitDef->isSubClassOf("Register")); |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 346 | const std::vector<MVT::SimpleValueType> &RegVTs = |
Chris Lattner | 9414ae5 | 2010-03-27 20:09:24 +0000 | [diff] [blame] | 347 | TargetInfo.getRegisterVTs(FirstImplicitDef); |
| 348 | if (RegVTs.size() == 1) |
| 349 | return RegVTs[0]; |
| 350 | return MVT::Other; |
| 351 | } |
| 352 | |
Chris Lattner | 4d43d0f | 2010-11-01 01:07:14 +0000 | [diff] [blame] | 353 | |
| 354 | /// FlattenAsmStringVariants - Flatten the specified AsmString to only |
| 355 | /// include text from the specified variant, returning the new string. |
| 356 | std::string CodeGenInstruction:: |
| 357 | FlattenAsmStringVariants(StringRef Cur, unsigned Variant) { |
| 358 | std::string Res = ""; |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 359 | |
Chris Lattner | 4d43d0f | 2010-11-01 01:07:14 +0000 | [diff] [blame] | 360 | for (;;) { |
| 361 | // Find the start of the next variant string. |
| 362 | size_t VariantsStart = 0; |
| 363 | for (size_t e = Cur.size(); VariantsStart != e; ++VariantsStart) |
| 364 | if (Cur[VariantsStart] == '{' && |
| 365 | (VariantsStart == 0 || (Cur[VariantsStart-1] != '$' && |
| 366 | Cur[VariantsStart-1] != '\\'))) |
| 367 | break; |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 368 | |
Chris Lattner | 4d43d0f | 2010-11-01 01:07:14 +0000 | [diff] [blame] | 369 | // Add the prefix to the result. |
| 370 | Res += Cur.slice(0, VariantsStart); |
| 371 | if (VariantsStart == Cur.size()) |
| 372 | break; |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 373 | |
Chris Lattner | 4d43d0f | 2010-11-01 01:07:14 +0000 | [diff] [blame] | 374 | ++VariantsStart; // Skip the '{'. |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 375 | |
Chris Lattner | 4d43d0f | 2010-11-01 01:07:14 +0000 | [diff] [blame] | 376 | // Scan to the end of the variants string. |
| 377 | size_t VariantsEnd = VariantsStart; |
| 378 | unsigned NestedBraces = 1; |
| 379 | for (size_t e = Cur.size(); VariantsEnd != e; ++VariantsEnd) { |
| 380 | if (Cur[VariantsEnd] == '}' && Cur[VariantsEnd-1] != '\\') { |
| 381 | if (--NestedBraces == 0) |
| 382 | break; |
| 383 | } else if (Cur[VariantsEnd] == '{') |
| 384 | ++NestedBraces; |
| 385 | } |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 386 | |
Chris Lattner | 4d43d0f | 2010-11-01 01:07:14 +0000 | [diff] [blame] | 387 | // Select the Nth variant (or empty). |
| 388 | StringRef Selection = Cur.slice(VariantsStart, VariantsEnd); |
| 389 | for (unsigned i = 0; i != Variant; ++i) |
| 390 | Selection = Selection.split('|').second; |
| 391 | Res += Selection.split('|').first; |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 392 | |
Chris Lattner | 4d43d0f | 2010-11-01 01:07:14 +0000 | [diff] [blame] | 393 | assert(VariantsEnd != Cur.size() && |
| 394 | "Unterminated variants in assembly string!"); |
| 395 | Cur = Cur.substr(VariantsEnd + 1); |
| 396 | } |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 397 | |
Chris Lattner | 4d43d0f | 2010-11-01 01:07:14 +0000 | [diff] [blame] | 398 | return Res; |
| 399 | } |
| 400 | |
Chris Lattner | c76e80d | 2010-11-01 04:05:41 +0000 | [diff] [blame] | 401 | |
| 402 | //===----------------------------------------------------------------------===// |
| 403 | /// CodeGenInstAlias Implementation |
| 404 | //===----------------------------------------------------------------------===// |
| 405 | |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 406 | /// tryAliasOpMatch - This is a helper function for the CodeGenInstAlias |
| 407 | /// constructor. It checks if an argument in an InstAlias pattern matches |
| 408 | /// the corresponding operand of the instruction. It returns true on a |
| 409 | /// successful match, with ResOp set to the result operand to be used. |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 410 | bool CodeGenInstAlias::tryAliasOpMatch(DagInit *Result, unsigned AliasOpNo, |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 411 | Record *InstOpRec, bool hasSubOps, |
| 412 | SMLoc Loc, CodeGenTarget &T, |
| 413 | ResultOperand &ResOp) { |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 414 | Init *Arg = Result->getArg(AliasOpNo); |
| 415 | DefInit *ADI = dynamic_cast<DefInit*>(Arg); |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 416 | |
| 417 | if (ADI && ADI->getDef() == InstOpRec) { |
| 418 | // If the operand is a record, it must have a name, and the record type |
| 419 | // must match up with the instruction's argument type. |
| 420 | if (Result->getArgName(AliasOpNo).empty()) |
| 421 | throw TGError(Loc, "result argument #" + utostr(AliasOpNo) + |
| 422 | " must have a name!"); |
| 423 | ResOp = ResultOperand(Result->getArgName(AliasOpNo), ADI->getDef()); |
| 424 | return true; |
| 425 | } |
| 426 | |
Jim Grosbach | be5d6bc | 2011-10-28 16:43:40 +0000 | [diff] [blame] | 427 | // For register operands, the source register class can be a subclass |
| 428 | // of the instruction register class, not just an exact match. |
| 429 | if (ADI && ADI->getDef()->isSubClassOf("RegisterClass")) { |
| 430 | if (!InstOpRec->isSubClassOf("RegisterClass")) |
| 431 | return false; |
Jim Grosbach | 48c1f84 | 2011-10-28 22:32:53 +0000 | [diff] [blame] | 432 | if (!T.getRegisterClass(InstOpRec) |
| 433 | .hasSubClass(&T.getRegisterClass(ADI->getDef()))) |
| 434 | return false; |
| 435 | ResOp = ResultOperand(Result->getArgName(AliasOpNo), ADI->getDef()); |
| 436 | return true; |
Jim Grosbach | be5d6bc | 2011-10-28 16:43:40 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 439 | // Handle explicit registers. |
| 440 | if (ADI && ADI->getDef()->isSubClassOf("Register")) { |
Jim Grosbach | c68e927 | 2011-08-19 20:33:06 +0000 | [diff] [blame] | 441 | if (InstOpRec->isSubClassOf("OptionalDefOperand")) { |
| 442 | DagInit *DI = InstOpRec->getValueAsDag("MIOperandInfo"); |
| 443 | // The operand info should only have a single (register) entry. We |
| 444 | // want the register class of it. |
| 445 | InstOpRec = dynamic_cast<DefInit*>(DI->getArg(0))->getDef(); |
| 446 | } |
| 447 | |
Owen Anderson | bea6f61 | 2011-06-27 21:06:21 +0000 | [diff] [blame] | 448 | if (InstOpRec->isSubClassOf("RegisterOperand")) |
| 449 | InstOpRec = InstOpRec->getValueAsDef("RegClass"); |
| 450 | |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 451 | if (!InstOpRec->isSubClassOf("RegisterClass")) |
| 452 | return false; |
| 453 | |
Jakob Stoklund Olesen | ae1920b | 2011-06-15 04:50:36 +0000 | [diff] [blame] | 454 | if (!T.getRegisterClass(InstOpRec) |
| 455 | .contains(T.getRegBank().getReg(ADI->getDef()))) |
Jim Grosbach | f2764c8 | 2011-08-19 19:53:51 +0000 | [diff] [blame] | 456 | throw TGError(Loc, "fixed register " + ADI->getDef()->getName() + |
| 457 | " is not a member of the " + InstOpRec->getName() + |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 458 | " register class!"); |
| 459 | |
| 460 | if (!Result->getArgName(AliasOpNo).empty()) |
| 461 | throw TGError(Loc, "result fixed register argument must " |
| 462 | "not have a name!"); |
| 463 | |
| 464 | ResOp = ResultOperand(ADI->getDef()); |
| 465 | return true; |
| 466 | } |
| 467 | |
| 468 | // Handle "zero_reg" for optional def operands. |
| 469 | if (ADI && ADI->getDef()->getName() == "zero_reg") { |
| 470 | |
| 471 | // Check if this is an optional def. |
Jim Grosbach | bfc9429 | 2011-11-15 01:46:57 +0000 | [diff] [blame] | 472 | // Tied operands where the source is a sub-operand of a complex operand |
| 473 | // need to represent both operands in the alias destination instruction. |
| 474 | // Allow zero_reg for the tied portion. This can and should go away once |
| 475 | // the MC representation of things doesn't use tied operands at all. |
| 476 | //if (!InstOpRec->isSubClassOf("OptionalDefOperand")) |
| 477 | // throw TGError(Loc, "reg0 used for result that is not an " |
| 478 | // "OptionalDefOperand!"); |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 479 | |
| 480 | ResOp = ResultOperand(static_cast<Record*>(0)); |
| 481 | return true; |
| 482 | } |
| 483 | |
Jim Grosbach | 48c1f84 | 2011-10-28 22:32:53 +0000 | [diff] [blame] | 484 | // Literal integers. |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 485 | if (IntInit *II = dynamic_cast<IntInit*>(Arg)) { |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 486 | if (hasSubOps || !InstOpRec->isSubClassOf("Operand")) |
| 487 | return false; |
| 488 | // Integer arguments can't have names. |
| 489 | if (!Result->getArgName(AliasOpNo).empty()) |
| 490 | throw TGError(Loc, "result argument #" + utostr(AliasOpNo) + |
| 491 | " must not have a name!"); |
| 492 | ResOp = ResultOperand(II->getValue()); |
| 493 | return true; |
| 494 | } |
| 495 | |
Jim Grosbach | 48c1f84 | 2011-10-28 22:32:53 +0000 | [diff] [blame] | 496 | // If both are Operands with the same MVT, allow the conversion. It's |
| 497 | // up to the user to make sure the values are appropriate, just like |
| 498 | // for isel Pat's. |
| 499 | if (InstOpRec->isSubClassOf("Operand") && |
| 500 | ADI->getDef()->isSubClassOf("Operand")) { |
| 501 | // FIXME: What other attributes should we check here? Identical |
| 502 | // MIOperandInfo perhaps? |
| 503 | if (InstOpRec->getValueInit("Type") != ADI->getDef()->getValueInit("Type")) |
| 504 | return false; |
| 505 | ResOp = ResultOperand(Result->getArgName(AliasOpNo), ADI->getDef()); |
| 506 | return true; |
| 507 | } |
| 508 | |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 509 | return false; |
| 510 | } |
| 511 | |
Chris Lattner | 662e5a3 | 2010-11-06 07:14:44 +0000 | [diff] [blame] | 512 | CodeGenInstAlias::CodeGenInstAlias(Record *R, CodeGenTarget &T) : TheDef(R) { |
Chris Lattner | c76e80d | 2010-11-01 04:05:41 +0000 | [diff] [blame] | 513 | AsmString = R->getValueAsString("AsmString"); |
Chris Lattner | b501d4f | 2010-11-01 05:34:34 +0000 | [diff] [blame] | 514 | Result = R->getValueAsDag("ResultInst"); |
Chris Lattner | 225549f | 2010-11-06 06:39:47 +0000 | [diff] [blame] | 515 | |
| 516 | // Verify that the root of the result is an instruction. |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 517 | DefInit *DI = dynamic_cast<DefInit*>(Result->getOperator()); |
Chris Lattner | 225549f | 2010-11-06 06:39:47 +0000 | [diff] [blame] | 518 | if (DI == 0 || !DI->getDef()->isSubClassOf("Instruction")) |
| 519 | throw TGError(R->getLoc(), "result of inst alias should be an instruction"); |
| 520 | |
| 521 | ResultInst = &T.getInstruction(DI->getDef()); |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 522 | |
Chris Lattner | 3f2c8e4 | 2010-11-06 07:06:09 +0000 | [diff] [blame] | 523 | // NameClass - If argument names are repeated, we need to verify they have |
| 524 | // the same class. |
| 525 | StringMap<Record*> NameClass; |
Bob Wilson | 55931ab | 2011-01-20 18:38:10 +0000 | [diff] [blame] | 526 | for (unsigned i = 0, e = Result->getNumArgs(); i != e; ++i) { |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 527 | DefInit *ADI = dynamic_cast<DefInit*>(Result->getArg(i)); |
Bob Wilson | 55931ab | 2011-01-20 18:38:10 +0000 | [diff] [blame] | 528 | if (!ADI || Result->getArgName(i).empty()) |
| 529 | continue; |
| 530 | // Verify we don't have something like: (someinst GR16:$foo, GR32:$foo) |
| 531 | // $foo can exist multiple times in the result list, but it must have the |
| 532 | // same type. |
| 533 | Record *&Entry = NameClass[Result->getArgName(i)]; |
| 534 | if (Entry && Entry != ADI->getDef()) |
| 535 | throw TGError(R->getLoc(), "result value $" + Result->getArgName(i) + |
| 536 | " is both " + Entry->getName() + " and " + |
| 537 | ADI->getDef()->getName() + "!"); |
| 538 | Entry = ADI->getDef(); |
| 539 | } |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 540 | |
Chris Lattner | d0f225c | 2010-11-06 06:54:38 +0000 | [diff] [blame] | 541 | // Decode and validate the arguments of the result. |
Chris Lattner | 4140985 | 2010-11-06 07:31:43 +0000 | [diff] [blame] | 542 | unsigned AliasOpNo = 0; |
| 543 | for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) { |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 544 | |
Jim Grosbach | bfc9429 | 2011-11-15 01:46:57 +0000 | [diff] [blame] | 545 | // Tied registers don't have an entry in the result dag unless they're part |
| 546 | // of a complex operand, in which case we include them anyways, as we |
| 547 | // don't have any other way to specify the whole operand. |
| 548 | if (ResultInst->Operands[i].MINumOperands == 1 && |
| 549 | ResultInst->Operands[i].getTiedRegister() != -1) |
Chris Lattner | 4140985 | 2010-11-06 07:31:43 +0000 | [diff] [blame] | 550 | continue; |
| 551 | |
| 552 | if (AliasOpNo >= Result->getNumArgs()) |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 553 | throw TGError(R->getLoc(), "not enough arguments for instruction!"); |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 554 | |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 555 | Record *InstOpRec = ResultInst->Operands[i].Rec; |
| 556 | unsigned NumSubOps = ResultInst->Operands[i].MINumOperands; |
| 557 | ResultOperand ResOp(static_cast<int64_t>(0)); |
| 558 | if (tryAliasOpMatch(Result, AliasOpNo, InstOpRec, (NumSubOps > 1), |
| 559 | R->getLoc(), T, ResOp)) { |
Owen Anderson | 7e8921b | 2012-06-08 00:25:03 +0000 | [diff] [blame] | 560 | // If this is a simple operand, or a complex operand with a custom match |
| 561 | // class, then we can match is verbatim. |
| 562 | if (NumSubOps == 1 || |
| 563 | (InstOpRec->getValue("ParserMatchClass") && |
| 564 | InstOpRec->getValueAsDef("ParserMatchClass") |
| 565 | ->getValueAsString("Name") != "Imm")) { |
| 566 | ResultOperands.push_back(ResOp); |
| 567 | ResultInstOperandIndex.push_back(std::make_pair(i, -1)); |
| 568 | ++AliasOpNo; |
| 569 | |
| 570 | // Otherwise, we need to match each of the suboperands individually. |
| 571 | } else { |
| 572 | DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo; |
| 573 | for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) { |
| 574 | Record *SubRec = dynamic_cast<DefInit*>(MIOI->getArg(SubOp))->getDef(); |
| 575 | |
| 576 | // Take care to instantiate each of the suboperands with the correct |
| 577 | // nomenclature: $foo.bar |
| 578 | ResultOperands.push_back( |
| 579 | ResultOperand(Result->getArgName(AliasOpNo) + "." + |
| 580 | MIOI->getArgName(SubOp), SubRec)); |
| 581 | ResultInstOperandIndex.push_back(std::make_pair(i, SubOp)); |
| 582 | } |
| 583 | ++AliasOpNo; |
| 584 | } |
Chris Lattner | d0f225c | 2010-11-06 06:54:38 +0000 | [diff] [blame] | 585 | continue; |
| 586 | } |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 587 | |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 588 | // If the argument did not match the instruction operand, and the operand |
| 589 | // is composed of multiple suboperands, try matching the suboperands. |
| 590 | if (NumSubOps > 1) { |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 591 | DagInit *MIOI = ResultInst->Operands[i].MIOperandInfo; |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 592 | for (unsigned SubOp = 0; SubOp != NumSubOps; ++SubOp) { |
| 593 | if (AliasOpNo >= Result->getNumArgs()) |
| 594 | throw TGError(R->getLoc(), "not enough arguments for instruction!"); |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 595 | Record *SubRec = dynamic_cast<DefInit*>(MIOI->getArg(SubOp))->getDef(); |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 596 | if (tryAliasOpMatch(Result, AliasOpNo, SubRec, false, |
| 597 | R->getLoc(), T, ResOp)) { |
| 598 | ResultOperands.push_back(ResOp); |
| 599 | ResultInstOperandIndex.push_back(std::make_pair(i, SubOp)); |
| 600 | ++AliasOpNo; |
| 601 | } else { |
| 602 | throw TGError(R->getLoc(), "result argument #" + utostr(AliasOpNo) + |
| 603 | " does not match instruction operand class " + |
| 604 | (SubOp == 0 ? InstOpRec->getName() :SubRec->getName())); |
| 605 | } |
| 606 | } |
Chris Lattner | 98c870f | 2010-11-06 19:25:43 +0000 | [diff] [blame] | 607 | continue; |
| 608 | } |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 609 | throw TGError(R->getLoc(), "result argument #" + utostr(AliasOpNo) + |
| 610 | " does not match instruction operand class " + |
| 611 | InstOpRec->getName()); |
Chris Lattner | d0f225c | 2010-11-06 06:54:38 +0000 | [diff] [blame] | 612 | } |
NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 613 | |
Chris Lattner | 4140985 | 2010-11-06 07:31:43 +0000 | [diff] [blame] | 614 | if (AliasOpNo != Result->getNumArgs()) |
Bob Wilson | a49c7df | 2011-01-26 19:44:55 +0000 | [diff] [blame] | 615 | throw TGError(R->getLoc(), "too many operands for instruction!"); |
Chris Lattner | c76e80d | 2010-11-01 04:05:41 +0000 | [diff] [blame] | 616 | } |