| 1 | //===- CallingConvEmitter.cpp - Generate calling conventions --------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This tablegen backend is responsible for emitting descriptions of the calling |
| 10 | // conventions supported by this target. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Common/CodeGenRegisters.h" |
| 15 | #include "Common/CodeGenTarget.h" |
| 16 | #include "llvm/Support/FormatVariadic.h" |
| 17 | #include "llvm/Support/InterleavedRange.h" |
| 18 | #include "llvm/TableGen/CodeGenHelpers.h" |
| 19 | #include "llvm/TableGen/Error.h" |
| 20 | #include "llvm/TableGen/Record.h" |
| 21 | #include "llvm/TableGen/TGTimer.h" |
| 22 | #include "llvm/TableGen/TableGenBackend.h" |
| 23 | #include <deque> |
| 24 | #include <set> |
| 25 | |
| 26 | using namespace llvm; |
| 27 | |
| 28 | namespace { |
| 29 | class CallingConvEmitter { |
| 30 | const RecordKeeper &Records; |
| 31 | const CodeGenTarget Target; |
| 32 | unsigned Counter = 0u; |
| 33 | std::string CurrentAction; |
| 34 | bool SwiftAction = false; |
| 35 | |
| 36 | std::map<std::string, std::set<std::string>> AssignedRegsMap; |
| 37 | std::map<std::string, std::set<std::string>> AssignedSwiftRegsMap; |
| 38 | std::map<std::string, std::set<std::string>> DelegateToMap; |
| 39 | |
| 40 | public: |
| 41 | explicit CallingConvEmitter(const RecordKeeper &R) : Records(R), Target(R) { |
| 42 | for (const CodeGenRegister &Reg : Target.getRegBank().getRegisters()) |
| 43 | RegistersByDefName.try_emplace(Key: Reg.getName(), Args: &Reg); |
| 44 | } |
| 45 | |
| 46 | void run(raw_ostream &O); |
| 47 | |
| 48 | private: |
| 49 | void emitCallingConv(const Record *CC, raw_ostream &O); |
| 50 | void emitAction(const Record *Action, indent Indent, raw_ostream &O); |
| 51 | void emitArgRegisterLists(raw_ostream &O); |
| 52 | |
| 53 | StringMap<const CodeGenRegister *> RegistersByDefName; |
| 54 | std::string getQualifiedRegisterName(const Init *I); |
| 55 | }; |
| 56 | } // End anonymous namespace |
| 57 | |
| 58 | static void (raw_ostream &O, const Record *CC, StringRef Suffix) { |
| 59 | unsigned Pad = CC->getName().size(); |
| 60 | if (CC->getValueAsBit(FieldName: "Entry" )) { |
| 61 | O << "bool llvm::" ; |
| 62 | Pad += 12; |
| 63 | } else { |
| 64 | O << "static bool " ; |
| 65 | Pad += 13; |
| 66 | } |
| 67 | O << CC->getName() << "(unsigned ValNo, MVT ValVT,\n" |
| 68 | << indent(Pad) << "MVT LocVT, CCValAssign::LocInfo LocInfo,\n" |
| 69 | << indent(Pad) << "ISD::ArgFlagsTy ArgFlags, Type *OrigTy, CCState &State)" |
| 70 | << Suffix; |
| 71 | } |
| 72 | |
| 73 | void CallingConvEmitter::run(raw_ostream &O) { |
| 74 | emitSourceFileHeader(Desc: "Calling Convention Implementation Fragment" , OS&: O); |
| 75 | |
| 76 | ArrayRef<const Record *> CCs = |
| 77 | Records.getAllDerivedDefinitions(ClassName: "CallingConv" ); |
| 78 | |
| 79 | // Emit prototypes for all of the non-custom CC's so that they can forward ref |
| 80 | // each other. |
| 81 | Records.getTimer().startTimer(Name: "Emit prototypes" ); |
| 82 | IfGuardEmitter IfGuard(O, "!defined(GET_CC_REGISTER_LISTS)" ); |
| 83 | for (const Record *CC : CCs) { |
| 84 | if (!CC->getValueAsBit(FieldName: "Custom" )) |
| 85 | emitCCHeader(O, CC, Suffix: ";\n" ); |
| 86 | } |
| 87 | |
| 88 | // Emit each non-custom calling convention description in full. |
| 89 | Records.getTimer().startTimer(Name: "Emit full descriptions" ); |
| 90 | for (const Record *CC : CCs) { |
| 91 | if (!CC->getValueAsBit(FieldName: "Custom" )) |
| 92 | emitCallingConv(CC, O); |
| 93 | } |
| 94 | |
| 95 | emitArgRegisterLists(O); |
| 96 | } |
| 97 | |
| 98 | void CallingConvEmitter::emitCallingConv(const Record *CC, raw_ostream &O) { |
| 99 | const ListInit *CCActions = CC->getValueAsListInit(FieldName: "Actions" ); |
| 100 | Counter = 0; |
| 101 | |
| 102 | CurrentAction = CC->getName().str(); |
| 103 | // Call upon the creation of a map entry from the void! |
| 104 | // We want an entry in AssignedRegsMap for every action, even if that |
| 105 | // entry is empty. |
| 106 | AssignedRegsMap[CurrentAction] = {}; |
| 107 | |
| 108 | O << "\n\n" ; |
| 109 | emitCCHeader(O, CC, Suffix: " {\n" ); |
| 110 | // Emit all of the actions, in order. |
| 111 | for (unsigned I = 0, E = CCActions->size(); I != E; ++I) { |
| 112 | const Record *Action = CCActions->getElementAsRecord(Idx: I); |
| 113 | SwiftAction = |
| 114 | llvm::any_of(Range: Action->getSuperClasses(), P: [](const Record *Class) { |
| 115 | std::string Name = Class->getNameInitAsString(); |
| 116 | return StringRef(Name).starts_with(Prefix: "CCIfSwift" ); |
| 117 | }); |
| 118 | |
| 119 | O << "\n" ; |
| 120 | emitAction(Action, Indent: indent(2), O); |
| 121 | } |
| 122 | |
| 123 | O << "\n return true; // CC didn't match.\n" ; |
| 124 | O << "}\n" ; |
| 125 | } |
| 126 | |
| 127 | // Return the name of the specified Init (DefInit or StringInit), with a |
| 128 | // namespace qualifier if the corresponding record contains one. |
| 129 | std::string CallingConvEmitter::getQualifiedRegisterName(const Init *I) { |
| 130 | if (const auto *DI = dyn_cast<DefInit>(Val: I)) |
| 131 | return getQualifiedName(R: DI->getDef()); |
| 132 | |
| 133 | const auto *SI = cast<StringInit>(Val: I); |
| 134 | if (const CodeGenRegister *CGR = RegistersByDefName.lookup(Key: SI->getValue())) |
| 135 | return getQualifiedName(R: CGR->TheDef); |
| 136 | |
| 137 | PrintFatalError(Msg: "register not defined: " + SI->getAsString()); |
| 138 | return "" ; |
| 139 | } |
| 140 | |
| 141 | void CallingConvEmitter::emitAction(const Record *Action, indent Indent, |
| 142 | raw_ostream &O) { |
| 143 | |
| 144 | auto EmitRegList = [&](const ListInit *RL, const StringRef RLName) { |
| 145 | O << Indent << "static const MCPhysReg " << RLName << "[] = {\n" ; |
| 146 | O << Indent << " " ; |
| 147 | ListSeparator LS; |
| 148 | for (const Init *V : RL->getElements()) |
| 149 | O << LS << getQualifiedRegisterName(I: V); |
| 150 | O << "\n" << Indent << "};\n" ; |
| 151 | }; |
| 152 | |
| 153 | auto EmitAllocateReg = [&](ArrayRef<const ListInit *> RegLists, |
| 154 | ArrayRef<std::string> RLNames) { |
| 155 | SmallVector<std::string> Parms; |
| 156 | if (RegLists[0]->size() == 1) { |
| 157 | for (const ListInit *LI : RegLists) |
| 158 | Parms.push_back(Elt: getQualifiedRegisterName(I: LI->getElement(Idx: 0))); |
| 159 | } else { |
| 160 | for (const std::string &S : RLNames) |
| 161 | Parms.push_back(Elt: S + utostr(X: ++Counter)); |
| 162 | for (const auto [Idx, LI] : enumerate(First&: RegLists)) |
| 163 | EmitRegList(LI, Parms[Idx]); |
| 164 | } |
| 165 | O << formatv(Fmt: "{0}if (MCRegister Reg = State.AllocateReg({1})) {{\n" , Vals&: Indent, |
| 166 | Vals: make_range(x: Parms.begin(), y: Parms.end())); |
| 167 | O << Indent << " State.addLoc(CCValAssign::getReg(ValNo, ValVT, " |
| 168 | << "Reg, LocVT, LocInfo));\n" ; |
| 169 | }; |
| 170 | |
| 171 | auto EmitAllocateStack = [&](bool EmitOffset = false) { |
| 172 | int Size = Action->getValueAsInt(FieldName: "Size" ); |
| 173 | int Align = Action->getValueAsInt(FieldName: "Align" ); |
| 174 | if (EmitOffset) |
| 175 | O << Indent << "int64_t Offset" << ++Counter << " = " ; |
| 176 | else |
| 177 | O << Indent << " (void)" ; |
| 178 | O << "State.AllocateStack(" ; |
| 179 | |
| 180 | const char *Fmt = " State.getMachineFunction().getDataLayout()." |
| 181 | "{0}(EVT(LocVT).getTypeForEVT(State.getContext()))" ; |
| 182 | if (Size) |
| 183 | O << Size << ", " ; |
| 184 | else |
| 185 | O << "\n" << Indent << formatv(Fmt, Vals: "getTypeAllocSize" ) << ", " ; |
| 186 | if (Align) |
| 187 | O << "Align(" << Align << ")" ; |
| 188 | else |
| 189 | O << "\n" << Indent << formatv(Fmt, Vals: "getABITypeAlign" ); |
| 190 | O << ");\n" ; |
| 191 | }; |
| 192 | |
| 193 | if (Action->isSubClassOf(Name: "CCPredicateAction" )) { |
| 194 | O << Indent << "if (" ; |
| 195 | |
| 196 | if (Action->isSubClassOf(Name: "CCIfType" )) { |
| 197 | const ListInit *VTs = Action->getValueAsListInit(FieldName: "VTs" ); |
| 198 | for (unsigned I = 0, E = VTs->size(); I != E; ++I) { |
| 199 | const Record *VT = VTs->getElementAsRecord(Idx: I); |
| 200 | if (I != 0) |
| 201 | O << " ||\n " << Indent; |
| 202 | O << "LocVT == " << getEnumName(T: getValueType(Rec: VT)); |
| 203 | } |
| 204 | |
| 205 | } else if (Action->isSubClassOf(Name: "CCIf" )) { |
| 206 | O << Action->getValueAsString(FieldName: "Predicate" ); |
| 207 | } else { |
| 208 | errs() << *Action; |
| 209 | PrintFatalError(ErrorLoc: Action->getLoc(), Msg: "Unknown CCPredicateAction!" ); |
| 210 | } |
| 211 | |
| 212 | O << ") {\n" ; |
| 213 | emitAction(Action: Action->getValueAsDef(FieldName: "SubAction" ), Indent: Indent + 2, O); |
| 214 | O << Indent << "}\n" ; |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | if (Action->isSubClassOf(Name: "CCDelegateTo" )) { |
| 219 | const Record *CC = Action->getValueAsDef(FieldName: "CC" ); |
| 220 | O << Indent << "if (!" << CC->getName() |
| 221 | << "(ValNo, ValVT, LocVT, LocInfo, ArgFlags, OrigTy, State))\n" |
| 222 | << Indent + 2 << "return false;\n" ; |
| 223 | DelegateToMap[CurrentAction].insert(x: CC->getName().str()); |
| 224 | } else if (Action->isSubClassOf(Name: "CCAssignToReg" ) || |
| 225 | Action->isSubClassOf(Name: "CCAssignToRegTuple" ) || |
| 226 | Action->isSubClassOf(Name: "CCAssignToRegAndStack" )) { |
| 227 | const ListInit *RegList = Action->getValueAsListInit(FieldName: "RegList" ); |
| 228 | for (unsigned I = 0, E = RegList->size(); I != E; ++I) { |
| 229 | std::string Name = getQualifiedRegisterName(I: RegList->getElement(Idx: I)); |
| 230 | if (SwiftAction) |
| 231 | AssignedSwiftRegsMap[CurrentAction].insert(x: std::move(Name)); |
| 232 | else |
| 233 | AssignedRegsMap[CurrentAction].insert(x: std::move(Name)); |
| 234 | } |
| 235 | EmitAllocateReg({RegList}, {"RegList" }); |
| 236 | |
| 237 | if (Action->isSubClassOf(Name: "CCAssignToRegAndStack" )) |
| 238 | EmitAllocateStack(); |
| 239 | |
| 240 | O << Indent << " return false;\n" ; |
| 241 | O << Indent << "}\n" ; |
| 242 | } else if (Action->isSubClassOf(Name: "CCAssignToRegWithShadow" )) { |
| 243 | const ListInit *RegList = Action->getValueAsListInit(FieldName: "RegList" ); |
| 244 | const ListInit *ShadowRegList = Action->getValueAsListInit(FieldName: "ShadowRegList" ); |
| 245 | if (!ShadowRegList->empty() && ShadowRegList->size() != RegList->size()) |
| 246 | PrintFatalError(ErrorLoc: Action->getLoc(), |
| 247 | Msg: "Invalid length of list of shadowed registers" ); |
| 248 | |
| 249 | EmitAllocateReg({RegList, ShadowRegList}, {"RegList" , "RegList" }); |
| 250 | |
| 251 | O << Indent << " return false;\n" ; |
| 252 | O << Indent << "}\n" ; |
| 253 | } else if (Action->isSubClassOf(Name: "CCAssignToStack" )) { |
| 254 | EmitAllocateStack(/*EmitOffset=*/true); |
| 255 | O << Indent << "State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset" |
| 256 | << Counter << ", LocVT, LocInfo));\n" ; |
| 257 | O << Indent << "return false;\n" ; |
| 258 | } else if (Action->isSubClassOf(Name: "CCAssignToStackWithShadow" )) { |
| 259 | int Size = Action->getValueAsInt(FieldName: "Size" ); |
| 260 | int Align = Action->getValueAsInt(FieldName: "Align" ); |
| 261 | const ListInit *ShadowRegList = Action->getValueAsListInit(FieldName: "ShadowRegList" ); |
| 262 | |
| 263 | unsigned ShadowRegListNumber = ++Counter; |
| 264 | EmitRegList(ShadowRegList, "ShadowRegList" + utostr(X: ShadowRegListNumber)); |
| 265 | |
| 266 | O << Indent << "int64_t Offset" << ++Counter << " = State.AllocateStack(" |
| 267 | << Size << ", Align(" << Align << "), " |
| 268 | << "ShadowRegList" << ShadowRegListNumber << ");\n" ; |
| 269 | O << Indent << "State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset" |
| 270 | << Counter << ", LocVT, LocInfo));\n" ; |
| 271 | O << Indent << "return false;\n" ; |
| 272 | } else if (Action->isSubClassOf(Name: "CCPromoteToType" )) { |
| 273 | const Record *DestTy = Action->getValueAsDef(FieldName: "DestTy" ); |
| 274 | MVT DestVT = getValueType(Rec: DestTy); |
| 275 | O << Indent << "LocVT = " << getEnumName(T: DestVT) << ";\n" ; |
| 276 | if (DestVT.isFloatingPoint()) { |
| 277 | O << Indent << "LocInfo = CCValAssign::FPExt;\n" ; |
| 278 | } else { |
| 279 | O << Indent << "if (ArgFlags.isSExt())\n" |
| 280 | << Indent << " LocInfo = CCValAssign::SExt;\n" |
| 281 | << Indent << "else if (ArgFlags.isZExt())\n" |
| 282 | << Indent << " LocInfo = CCValAssign::ZExt;\n" |
| 283 | << Indent << "else\n" |
| 284 | << Indent << " LocInfo = CCValAssign::AExt;\n" ; |
| 285 | } |
| 286 | } else if (Action->isSubClassOf(Name: "CCPromoteToUpperBitsInType" )) { |
| 287 | const Record *DestTy = Action->getValueAsDef(FieldName: "DestTy" ); |
| 288 | MVT DestVT = getValueType(Rec: DestTy); |
| 289 | O << Indent << "LocVT = " << getEnumName(T: DestVT) << ";\n" ; |
| 290 | if (DestVT.isFloatingPoint()) { |
| 291 | PrintFatalError(ErrorLoc: Action->getLoc(), |
| 292 | Msg: "CCPromoteToUpperBitsInType does not handle floating " |
| 293 | "point" ); |
| 294 | } else { |
| 295 | O << Indent << "if (ArgFlags.isSExt())\n" |
| 296 | << Indent << " LocInfo = CCValAssign::SExtUpper;\n" |
| 297 | << Indent << "else if (ArgFlags.isZExt())\n" |
| 298 | << Indent << " LocInfo = CCValAssign::ZExtUpper;\n" |
| 299 | << Indent << "else\n" |
| 300 | << Indent << " LocInfo = CCValAssign::AExtUpper;\n" ; |
| 301 | } |
| 302 | } else if (Action->isSubClassOf(Name: "CCBitConvertToType" )) { |
| 303 | const Record *DestTy = Action->getValueAsDef(FieldName: "DestTy" ); |
| 304 | O << Indent << "LocVT = " << getEnumName(T: getValueType(Rec: DestTy)) << ";\n" ; |
| 305 | O << Indent << "LocInfo = CCValAssign::BCvt;\n" ; |
| 306 | } else if (Action->isSubClassOf(Name: "CCTruncToType" )) { |
| 307 | const Record *DestTy = Action->getValueAsDef(FieldName: "DestTy" ); |
| 308 | O << Indent << "LocVT = " << getEnumName(T: getValueType(Rec: DestTy)) << ";\n" ; |
| 309 | O << Indent << "LocInfo = CCValAssign::Trunc;\n" ; |
| 310 | } else if (Action->isSubClassOf(Name: "CCPassIndirect" )) { |
| 311 | const Record *DestTy = Action->getValueAsDef(FieldName: "DestTy" ); |
| 312 | O << Indent << "LocVT = " << getEnumName(T: getValueType(Rec: DestTy)) << ";\n" ; |
| 313 | O << Indent << "LocInfo = CCValAssign::Indirect;\n" ; |
| 314 | } else if (Action->isSubClassOf(Name: "CCPassByVal" )) { |
| 315 | int Size = Action->getValueAsInt(FieldName: "Size" ); |
| 316 | int Align = Action->getValueAsInt(FieldName: "Align" ); |
| 317 | O << Indent << "State.HandleByVal(ValNo, ValVT, LocVT, LocInfo, " << Size |
| 318 | << ", Align(" << Align << "), ArgFlags);\n" ; |
| 319 | O << Indent << "return false;\n" ; |
| 320 | } else if (Action->isSubClassOf(Name: "CCCustom" )) { |
| 321 | O << Indent << "if (" << Action->getValueAsString(FieldName: "FuncName" ) |
| 322 | << "(ValNo, ValVT, " |
| 323 | << "LocVT, LocInfo, ArgFlags, State))\n" ; |
| 324 | O << Indent << " return false;\n" ; |
| 325 | } else { |
| 326 | errs() << *Action; |
| 327 | PrintFatalError(ErrorLoc: Action->getLoc(), Msg: "Unknown CCAction!" ); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | void CallingConvEmitter::emitArgRegisterLists(raw_ostream &O) { |
| 332 | // Transitively merge all delegated CCs into AssignedRegsMap. |
| 333 | using EntryTy = std::pair<std::string, std::set<std::string>>; |
| 334 | bool Redo; |
| 335 | do { |
| 336 | Redo = false; |
| 337 | std::deque<EntryTy> Worklist(DelegateToMap.begin(), DelegateToMap.end()); |
| 338 | |
| 339 | while (!Worklist.empty()) { |
| 340 | EntryTy Entry = Worklist.front(); |
| 341 | Worklist.pop_front(); |
| 342 | |
| 343 | const std::string &CCName = Entry.first; |
| 344 | std::set<std::string> &Registers = Entry.second; |
| 345 | if (!Registers.empty()) |
| 346 | continue; |
| 347 | |
| 348 | for (auto &InnerEntry : Worklist) { |
| 349 | const std::string &InnerCCName = InnerEntry.first; |
| 350 | std::set<std::string> &InnerRegisters = InnerEntry.second; |
| 351 | |
| 352 | auto It = InnerRegisters.find(x: CCName); |
| 353 | if (It != InnerRegisters.end()) { |
| 354 | const auto &Src = AssignedRegsMap[CCName]; |
| 355 | AssignedRegsMap[InnerCCName].insert(first: Src.begin(), last: Src.end()); |
| 356 | InnerRegisters.erase(position: It); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | DelegateToMap.erase(x: CCName); |
| 361 | Redo = true; |
| 362 | } |
| 363 | } while (Redo); |
| 364 | |
| 365 | if (AssignedRegsMap.empty()) |
| 366 | return; |
| 367 | |
| 368 | O << "\n#else\n\n" ; |
| 369 | |
| 370 | for (const auto &[RegName, Registers] : AssignedRegsMap) { |
| 371 | if (RegName.empty()) |
| 372 | continue; |
| 373 | |
| 374 | O << "const MCRegister " << RegName << "_ArgRegs[] = { " ; |
| 375 | |
| 376 | if (Registers.empty()) |
| 377 | O << "0" ; |
| 378 | else |
| 379 | O << llvm::interleaved(R: Registers); |
| 380 | |
| 381 | O << " };\n" ; |
| 382 | } |
| 383 | |
| 384 | if (AssignedSwiftRegsMap.empty()) |
| 385 | return; |
| 386 | |
| 387 | O << "\n// Registers used by Swift.\n" ; |
| 388 | for (const auto &[RegName, Registers] : AssignedSwiftRegsMap) |
| 389 | O << "const MCRegister " << RegName << "_Swift_ArgRegs[] = { " |
| 390 | << llvm::interleaved(R: Registers) << " };\n" ; |
| 391 | } |
| 392 | |
| 393 | static TableGen::Emitter::OptClass<CallingConvEmitter> |
| 394 | X("gen-callingconv" , "Generate calling convention descriptions" ); |
| 395 | |