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