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 "CodeGenTarget.h" 15 #include "llvm/TableGen/Error.h" 16 #include "llvm/TableGen/Record.h" 17 #include "llvm/TableGen/TableGenBackend.h" 18 using namespace llvm; 19 20 namespace { 21 class CallingConvEmitter { 22 RecordKeeper &Records; 23 public: 24 explicit CallingConvEmitter(RecordKeeper &R) : Records(R) {} 25 26 void run(raw_ostream &o); 27 28 private: 29 void EmitCallingConv(Record *CC, raw_ostream &O); 30 void EmitAction(Record *Action, unsigned Indent, raw_ostream &O); 31 unsigned Counter; 32 }; 33 } // End anonymous namespace 34 35 void CallingConvEmitter::run(raw_ostream &O) { 36 std::vector<Record*> CCs = Records.getAllDerivedDefinitions("CallingConv"); 37 38 // Emit prototypes for all of the non-custom CC's so that they can forward ref 39 // each other. 40 Records.startTimer("Emit prototypes"); 41 for (Record *CC : CCs) { 42 if (!CC->getValueAsBit("Custom")) { 43 unsigned Pad = CC->getName().size(); 44 if (CC->getValueAsBit("Entry")) { 45 O << "bool llvm::"; 46 Pad += 12; 47 } else { 48 O << "static bool "; 49 Pad += 13; 50 } 51 O << CC->getName() << "(unsigned ValNo, MVT ValVT,\n" 52 << std::string(Pad, ' ') << "MVT LocVT, CCValAssign::LocInfo LocInfo,\n" 53 << std::string(Pad, ' ') 54 << "ISD::ArgFlagsTy ArgFlags, CCState &State);\n"; 55 } 56 } 57 58 // Emit each non-custom calling convention description in full. 59 Records.startTimer("Emit full descriptions"); 60 for (Record *CC : CCs) { 61 if (!CC->getValueAsBit("Custom")) 62 EmitCallingConv(CC, O); 63 } 64 } 65 66 67 void CallingConvEmitter::EmitCallingConv(Record *CC, raw_ostream &O) { 68 ListInit *CCActions = CC->getValueAsListInit("Actions"); 69 Counter = 0; 70 71 O << "\n\n"; 72 unsigned Pad = CC->getName().size(); 73 if (CC->getValueAsBit("Entry")) { 74 O << "bool llvm::"; 75 Pad += 12; 76 } else { 77 O << "static bool "; 78 Pad += 13; 79 } 80 O << CC->getName() << "(unsigned ValNo, MVT ValVT,\n" 81 << std::string(Pad, ' ') << "MVT LocVT, CCValAssign::LocInfo LocInfo,\n" 82 << std::string(Pad, ' ') << "ISD::ArgFlagsTy ArgFlags, CCState &State) {\n"; 83 // Emit all of the actions, in order. 84 for (unsigned i = 0, e = CCActions->size(); i != e; ++i) { 85 O << "\n"; 86 EmitAction(CCActions->getElementAsRecord(i), 2, O); 87 } 88 89 O << "\n return true; // CC didn't match.\n"; 90 O << "}\n"; 91 } 92 93 void CallingConvEmitter::EmitAction(Record *Action, 94 unsigned Indent, raw_ostream &O) { 95 std::string IndentStr = std::string(Indent, ' '); 96 97 if (Action->isSubClassOf("CCPredicateAction")) { 98 O << IndentStr << "if ("; 99 100 if (Action->isSubClassOf("CCIfType")) { 101 ListInit *VTs = Action->getValueAsListInit("VTs"); 102 for (unsigned i = 0, e = VTs->size(); i != e; ++i) { 103 Record *VT = VTs->getElementAsRecord(i); 104 if (i != 0) O << " ||\n " << IndentStr; 105 O << "LocVT == " << getEnumName(getValueType(VT)); 106 } 107 108 } else if (Action->isSubClassOf("CCIf")) { 109 O << Action->getValueAsString("Predicate"); 110 } else { 111 errs() << *Action; 112 PrintFatalError(Action->getLoc(), "Unknown CCPredicateAction!"); 113 } 114 115 O << ") {\n"; 116 EmitAction(Action->getValueAsDef("SubAction"), Indent+2, O); 117 O << IndentStr << "}\n"; 118 } else { 119 if (Action->isSubClassOf("CCDelegateTo")) { 120 Record *CC = Action->getValueAsDef("CC"); 121 O << IndentStr << "if (!" << CC->getName() 122 << "(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))\n" 123 << IndentStr << " return false;\n"; 124 } else if (Action->isSubClassOf("CCAssignToReg")) { 125 ListInit *RegList = Action->getValueAsListInit("RegList"); 126 if (RegList->size() == 1) { 127 O << IndentStr << "if (unsigned Reg = State.AllocateReg("; 128 O << getQualifiedName(RegList->getElementAsRecord(0)) << ")) {\n"; 129 } else { 130 O << IndentStr << "static const MCPhysReg RegList" << ++Counter 131 << "[] = {\n"; 132 O << IndentStr << " "; 133 ListSeparator LS; 134 for (unsigned i = 0, e = RegList->size(); i != e; ++i) 135 O << LS << getQualifiedName(RegList->getElementAsRecord(i)); 136 O << "\n" << IndentStr << "};\n"; 137 O << IndentStr << "if (unsigned Reg = State.AllocateReg(RegList" 138 << Counter << ")) {\n"; 139 } 140 O << IndentStr << " State.addLoc(CCValAssign::getReg(ValNo, ValVT, " 141 << "Reg, LocVT, LocInfo));\n"; 142 O << IndentStr << " return false;\n"; 143 O << IndentStr << "}\n"; 144 } else if (Action->isSubClassOf("CCAssignToRegWithShadow")) { 145 ListInit *RegList = Action->getValueAsListInit("RegList"); 146 ListInit *ShadowRegList = Action->getValueAsListInit("ShadowRegList"); 147 if (!ShadowRegList->empty() && ShadowRegList->size() != RegList->size()) 148 PrintFatalError(Action->getLoc(), 149 "Invalid length of list of shadowed registers"); 150 151 if (RegList->size() == 1) { 152 O << IndentStr << "if (unsigned Reg = State.AllocateReg("; 153 O << getQualifiedName(RegList->getElementAsRecord(0)); 154 O << ", " << getQualifiedName(ShadowRegList->getElementAsRecord(0)); 155 O << ")) {\n"; 156 } else { 157 unsigned RegListNumber = ++Counter; 158 unsigned ShadowRegListNumber = ++Counter; 159 160 O << IndentStr << "static const MCPhysReg RegList" << RegListNumber 161 << "[] = {\n"; 162 O << IndentStr << " "; 163 ListSeparator LS; 164 for (unsigned i = 0, e = RegList->size(); i != e; ++i) 165 O << LS << getQualifiedName(RegList->getElementAsRecord(i)); 166 O << "\n" << IndentStr << "};\n"; 167 168 O << IndentStr << "static const MCPhysReg RegList" 169 << ShadowRegListNumber << "[] = {\n"; 170 O << IndentStr << " "; 171 ListSeparator LSS; 172 for (unsigned i = 0, e = ShadowRegList->size(); i != e; ++i) 173 O << LSS << getQualifiedName(ShadowRegList->getElementAsRecord(i)); 174 O << "\n" << IndentStr << "};\n"; 175 176 O << IndentStr << "if (unsigned Reg = State.AllocateReg(RegList" 177 << RegListNumber << ", " << "RegList" << ShadowRegListNumber 178 << ")) {\n"; 179 } 180 O << IndentStr << " State.addLoc(CCValAssign::getReg(ValNo, ValVT, " 181 << "Reg, LocVT, LocInfo));\n"; 182 O << IndentStr << " return false;\n"; 183 O << IndentStr << "}\n"; 184 } else if (Action->isSubClassOf("CCAssignToStack")) { 185 int Size = Action->getValueAsInt("Size"); 186 int Align = Action->getValueAsInt("Align"); 187 188 O << IndentStr << "unsigned Offset" << ++Counter 189 << " = State.AllocateStack("; 190 if (Size) 191 O << Size << ", "; 192 else 193 O << "\n" << IndentStr 194 << " State.getMachineFunction().getDataLayout()." 195 "getTypeAllocSize(EVT(LocVT).getTypeForEVT(State.getContext()))," 196 " "; 197 if (Align) 198 O << "Align(" << Align << ")"; 199 else 200 O << "\n" 201 << IndentStr 202 << " State.getMachineFunction().getDataLayout()." 203 "getABITypeAlign(EVT(LocVT).getTypeForEVT(State.getContext()" 204 "))"; 205 O << ");\n" << IndentStr 206 << "State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset" 207 << Counter << ", LocVT, LocInfo));\n"; 208 O << IndentStr << "return false;\n"; 209 } else if (Action->isSubClassOf("CCAssignToStackWithShadow")) { 210 int Size = Action->getValueAsInt("Size"); 211 int Align = Action->getValueAsInt("Align"); 212 ListInit *ShadowRegList = Action->getValueAsListInit("ShadowRegList"); 213 214 unsigned ShadowRegListNumber = ++Counter; 215 216 O << IndentStr << "static const MCPhysReg ShadowRegList" 217 << ShadowRegListNumber << "[] = {\n"; 218 O << IndentStr << " "; 219 ListSeparator LS; 220 for (unsigned i = 0, e = ShadowRegList->size(); i != e; ++i) 221 O << LS << getQualifiedName(ShadowRegList->getElementAsRecord(i)); 222 O << "\n" << IndentStr << "};\n"; 223 224 O << IndentStr << "unsigned Offset" << ++Counter 225 << " = State.AllocateStack(" << Size << ", Align(" << Align << "), " 226 << "ShadowRegList" << ShadowRegListNumber << ");\n"; 227 O << IndentStr << "State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset" 228 << Counter << ", LocVT, LocInfo));\n"; 229 O << IndentStr << "return false;\n"; 230 } else if (Action->isSubClassOf("CCPromoteToType")) { 231 Record *DestTy = Action->getValueAsDef("DestTy"); 232 MVT::SimpleValueType DestVT = getValueType(DestTy); 233 O << IndentStr << "LocVT = " << getEnumName(DestVT) <<";\n"; 234 if (MVT(DestVT).isFloatingPoint()) { 235 O << IndentStr << "LocInfo = CCValAssign::FPExt;\n"; 236 } else { 237 O << IndentStr << "if (ArgFlags.isSExt())\n" 238 << IndentStr << " LocInfo = CCValAssign::SExt;\n" 239 << IndentStr << "else if (ArgFlags.isZExt())\n" 240 << IndentStr << " LocInfo = CCValAssign::ZExt;\n" 241 << IndentStr << "else\n" 242 << IndentStr << " LocInfo = CCValAssign::AExt;\n"; 243 } 244 } else if (Action->isSubClassOf("CCPromoteToUpperBitsInType")) { 245 Record *DestTy = Action->getValueAsDef("DestTy"); 246 MVT::SimpleValueType DestVT = getValueType(DestTy); 247 O << IndentStr << "LocVT = " << getEnumName(DestVT) << ";\n"; 248 if (MVT(DestVT).isFloatingPoint()) { 249 PrintFatalError(Action->getLoc(), 250 "CCPromoteToUpperBitsInType does not handle floating " 251 "point"); 252 } else { 253 O << IndentStr << "if (ArgFlags.isSExt())\n" 254 << IndentStr << " LocInfo = CCValAssign::SExtUpper;\n" 255 << IndentStr << "else if (ArgFlags.isZExt())\n" 256 << IndentStr << " LocInfo = CCValAssign::ZExtUpper;\n" 257 << IndentStr << "else\n" 258 << IndentStr << " LocInfo = CCValAssign::AExtUpper;\n"; 259 } 260 } else if (Action->isSubClassOf("CCBitConvertToType")) { 261 Record *DestTy = Action->getValueAsDef("DestTy"); 262 O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy)) <<";\n"; 263 O << IndentStr << "LocInfo = CCValAssign::BCvt;\n"; 264 } else if (Action->isSubClassOf("CCTruncToType")) { 265 Record *DestTy = Action->getValueAsDef("DestTy"); 266 O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy)) <<";\n"; 267 O << IndentStr << "LocInfo = CCValAssign::Trunc;\n"; 268 } else if (Action->isSubClassOf("CCPassIndirect")) { 269 Record *DestTy = Action->getValueAsDef("DestTy"); 270 O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy)) <<";\n"; 271 O << IndentStr << "LocInfo = CCValAssign::Indirect;\n"; 272 } else if (Action->isSubClassOf("CCPassByVal")) { 273 int Size = Action->getValueAsInt("Size"); 274 int Align = Action->getValueAsInt("Align"); 275 O << IndentStr << "State.HandleByVal(ValNo, ValVT, LocVT, LocInfo, " 276 << Size << ", Align(" << Align << "), ArgFlags);\n"; 277 O << IndentStr << "return false;\n"; 278 } else if (Action->isSubClassOf("CCCustom")) { 279 O << IndentStr 280 << "if (" << Action->getValueAsString("FuncName") << "(ValNo, ValVT, " 281 << "LocVT, LocInfo, ArgFlags, State))\n"; 282 O << IndentStr << " return false;\n"; 283 } else { 284 errs() << *Action; 285 PrintFatalError(Action->getLoc(), "Unknown CCAction!"); 286 } 287 } 288 } 289 290 namespace llvm { 291 292 void EmitCallingConv(RecordKeeper &RK, raw_ostream &OS) { 293 emitSourceFileHeader("Calling Convention Implementation Fragment", OS); 294 CallingConvEmitter(RK).run(OS); 295 } 296 297 } // End llvm namespace 298