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