Chris Lattner | 88ee2a1 | 2007-02-27 22:05:51 +0000 | [diff] [blame] | 1 | //===- CallingConvEmitter.cpp - Generate calling conventions --------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 3060910 | 2007-12-29 20:37:13 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 88ee2a1 | 2007-02-27 22:05:51 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This tablegen backend is responsible for emitting descriptions of the calling |
| 11 | // conventions supported by this target. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "CallingConvEmitter.h" |
| 16 | #include "Record.h" |
| 17 | #include "CodeGenTarget.h" |
| 18 | using namespace llvm; |
| 19 | |
| 20 | void CallingConvEmitter::run(std::ostream &O) { |
| 21 | EmitSourceFileHeader("Calling Convention Implementation Fragment", O); |
| 22 | |
| 23 | std::vector<Record*> CCs = Records.getAllDerivedDefinitions("CallingConv"); |
| 24 | |
| 25 | // Emit prototypes for all of the CC's so that they can forward ref each |
| 26 | // other. |
| 27 | for (unsigned i = 0, e = CCs.size(); i != e; ++i) { |
| 28 | O << "static bool " << CCs[i]->getName() |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame^] | 29 | << "(unsigned ValNo, MVT ValVT,\n" |
Chris Lattner | 88ee2a1 | 2007-02-27 22:05:51 +0000 | [diff] [blame] | 30 | << std::string(CCs[i]->getName().size()+13, ' ') |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame^] | 31 | << "MVT LocVT, CCValAssign::LocInfo LocInfo,\n" |
Chris Lattner | 2092c8a | 2007-02-28 04:43:48 +0000 | [diff] [blame] | 32 | << std::string(CCs[i]->getName().size()+13, ' ') |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 33 | << "ISD::ArgFlagsTy ArgFlags, CCState &State);\n"; |
Chris Lattner | 88ee2a1 | 2007-02-27 22:05:51 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | // Emit each calling convention description in full. |
| 37 | for (unsigned i = 0, e = CCs.size(); i != e; ++i) |
| 38 | EmitCallingConv(CCs[i], O); |
| 39 | } |
| 40 | |
| 41 | |
| 42 | void CallingConvEmitter::EmitCallingConv(Record *CC, std::ostream &O) { |
| 43 | ListInit *CCActions = CC->getValueAsListInit("Actions"); |
| 44 | Counter = 0; |
| 45 | |
| 46 | O << "\n\nstatic bool " << CC->getName() |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame^] | 47 | << "(unsigned ValNo, MVT ValVT,\n" |
Chris Lattner | 88ee2a1 | 2007-02-27 22:05:51 +0000 | [diff] [blame] | 48 | << std::string(CC->getName().size()+13, ' ') |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame^] | 49 | << "MVT LocVT, CCValAssign::LocInfo LocInfo,\n" |
Chris Lattner | 2092c8a | 2007-02-28 04:43:48 +0000 | [diff] [blame] | 50 | << std::string(CC->getName().size()+13, ' ') |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 51 | << "ISD::ArgFlagsTy ArgFlags, CCState &State) {\n"; |
Chris Lattner | 88ee2a1 | 2007-02-27 22:05:51 +0000 | [diff] [blame] | 52 | // Emit all of the actions, in order. |
| 53 | for (unsigned i = 0, e = CCActions->getSize(); i != e; ++i) { |
| 54 | O << "\n"; |
| 55 | EmitAction(CCActions->getElementAsRecord(i), 2, O); |
| 56 | } |
| 57 | |
| 58 | O << "\n return true; // CC didn't match.\n"; |
| 59 | O << "}\n"; |
| 60 | } |
| 61 | |
| 62 | void CallingConvEmitter::EmitAction(Record *Action, |
| 63 | unsigned Indent, std::ostream &O) { |
| 64 | std::string IndentStr = std::string(Indent, ' '); |
| 65 | |
| 66 | if (Action->isSubClassOf("CCPredicateAction")) { |
| 67 | O << IndentStr << "if ("; |
| 68 | |
Chris Lattner | e3bab80 | 2007-02-28 05:29:06 +0000 | [diff] [blame] | 69 | if (Action->isSubClassOf("CCIfType")) { |
Chris Lattner | 88ee2a1 | 2007-02-27 22:05:51 +0000 | [diff] [blame] | 70 | ListInit *VTs = Action->getValueAsListInit("VTs"); |
| 71 | for (unsigned i = 0, e = VTs->getSize(); i != e; ++i) { |
| 72 | Record *VT = VTs->getElementAsRecord(i); |
Chris Lattner | 2092c8a | 2007-02-28 04:43:48 +0000 | [diff] [blame] | 73 | if (i != 0) O << " ||\n " << IndentStr; |
Chris Lattner | 88ee2a1 | 2007-02-27 22:05:51 +0000 | [diff] [blame] | 74 | O << "LocVT == " << getEnumName(getValueType(VT)); |
| 75 | } |
| 76 | |
Chris Lattner | e3bab80 | 2007-02-28 05:29:06 +0000 | [diff] [blame] | 77 | } else if (Action->isSubClassOf("CCIf")) { |
Chris Lattner | 88ee2a1 | 2007-02-27 22:05:51 +0000 | [diff] [blame] | 78 | O << Action->getValueAsString("Predicate"); |
| 79 | } else { |
| 80 | Action->dump(); |
| 81 | throw "Unknown CCPredicateAction!"; |
| 82 | } |
| 83 | |
| 84 | O << ") {\n"; |
| 85 | EmitAction(Action->getValueAsDef("SubAction"), Indent+2, O); |
| 86 | O << IndentStr << "}\n"; |
| 87 | } else { |
| 88 | if (Action->isSubClassOf("CCDelegateTo")) { |
| 89 | Record *CC = Action->getValueAsDef("CC"); |
| 90 | O << IndentStr << "if (!" << CC->getName() |
| 91 | << "(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State))\n" |
| 92 | << IndentStr << " return false;\n"; |
| 93 | } else if (Action->isSubClassOf("CCAssignToReg")) { |
| 94 | ListInit *RegList = Action->getValueAsListInit("RegList"); |
| 95 | if (RegList->getSize() == 1) { |
| 96 | O << IndentStr << "if (unsigned Reg = State.AllocateReg("; |
| 97 | O << getQualifiedName(RegList->getElementAsRecord(0)) << ")) {\n"; |
| 98 | } else { |
| 99 | O << IndentStr << "static const unsigned RegList" << ++Counter |
| 100 | << "[] = {\n"; |
| 101 | O << IndentStr << " "; |
| 102 | for (unsigned i = 0, e = RegList->getSize(); i != e; ++i) { |
| 103 | if (i != 0) O << ", "; |
| 104 | O << getQualifiedName(RegList->getElementAsRecord(i)); |
| 105 | } |
| 106 | O << "\n" << IndentStr << "};\n"; |
| 107 | O << IndentStr << "if (unsigned Reg = State.AllocateReg(RegList" |
| 108 | << Counter << ", " << RegList->getSize() << ")) {\n"; |
| 109 | } |
| 110 | O << IndentStr << " State.addLoc(CCValAssign::getReg(ValNo, ValVT, " |
| 111 | << "Reg, LocVT, LocInfo));\n"; |
| 112 | O << IndentStr << " return false;\n"; |
| 113 | O << IndentStr << "}\n"; |
Anton Korobeynikov | 67073f1 | 2008-04-02 05:23:57 +0000 | [diff] [blame] | 114 | } else if (Action->isSubClassOf("CCAssignToRegWithShadow")) { |
| 115 | ListInit *RegList = Action->getValueAsListInit("RegList"); |
| 116 | ListInit *ShadowRegList = Action->getValueAsListInit("ShadowRegList"); |
| 117 | if (ShadowRegList->getSize() >0 && |
| 118 | ShadowRegList->getSize() != RegList->getSize()) |
| 119 | throw "Invalid length of list of shadowed registers"; |
| 120 | |
| 121 | if (RegList->getSize() == 1) { |
| 122 | O << IndentStr << "if (unsigned Reg = State.AllocateReg("; |
| 123 | O << getQualifiedName(RegList->getElementAsRecord(0)); |
| 124 | O << ", " << getQualifiedName(ShadowRegList->getElementAsRecord(0)); |
| 125 | O << ")) {\n"; |
| 126 | } else { |
| 127 | unsigned RegListNumber = ++Counter; |
| 128 | unsigned ShadowRegListNumber = ++Counter; |
| 129 | |
| 130 | O << IndentStr << "static const unsigned RegList" << RegListNumber |
| 131 | << "[] = {\n"; |
| 132 | O << IndentStr << " "; |
| 133 | for (unsigned i = 0, e = RegList->getSize(); i != e; ++i) { |
| 134 | if (i != 0) O << ", "; |
| 135 | O << getQualifiedName(RegList->getElementAsRecord(i)); |
| 136 | } |
| 137 | O << "\n" << IndentStr << "};\n"; |
| 138 | |
| 139 | O << IndentStr << "static const unsigned RegList" |
| 140 | << ShadowRegListNumber << "[] = {\n"; |
| 141 | O << IndentStr << " "; |
| 142 | for (unsigned i = 0, e = ShadowRegList->getSize(); i != e; ++i) { |
| 143 | if (i != 0) O << ", "; |
| 144 | O << getQualifiedName(ShadowRegList->getElementAsRecord(i)); |
| 145 | } |
| 146 | O << "\n" << IndentStr << "};\n"; |
| 147 | |
| 148 | O << IndentStr << "if (unsigned Reg = State.AllocateReg(RegList" |
| 149 | << RegListNumber << ", " << "RegList" << ShadowRegListNumber |
| 150 | << ", " << RegList->getSize() << ")) {\n"; |
| 151 | } |
| 152 | O << IndentStr << " State.addLoc(CCValAssign::getReg(ValNo, ValVT, " |
| 153 | << "Reg, LocVT, LocInfo));\n"; |
| 154 | O << IndentStr << " return false;\n"; |
| 155 | O << IndentStr << "}\n"; |
Chris Lattner | 88ee2a1 | 2007-02-27 22:05:51 +0000 | [diff] [blame] | 156 | } else if (Action->isSubClassOf("CCAssignToStack")) { |
| 157 | int Size = Action->getValueAsInt("Size"); |
| 158 | int Align = Action->getValueAsInt("Align"); |
Duncan Sands | 87b665d | 2007-11-14 08:29:13 +0000 | [diff] [blame] | 159 | |
Evan Cheng | 5daafa9 | 2008-01-15 03:10:35 +0000 | [diff] [blame] | 160 | O << IndentStr << "unsigned Offset" << ++Counter |
| 161 | << " = State.AllocateStack("; |
Duncan Sands | 87b665d | 2007-11-14 08:29:13 +0000 | [diff] [blame] | 162 | if (Size) |
Evan Cheng | 5daafa9 | 2008-01-15 03:10:35 +0000 | [diff] [blame] | 163 | O << Size << ", "; |
Duncan Sands | 87b665d | 2007-11-14 08:29:13 +0000 | [diff] [blame] | 164 | else |
Evan Cheng | 5daafa9 | 2008-01-15 03:10:35 +0000 | [diff] [blame] | 165 | O << "\n" << IndentStr << " State.getTarget().getTargetData()" |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame^] | 166 | "->getABITypeSize(LocVT.getTypeForMVT()), "; |
Duncan Sands | 87b665d | 2007-11-14 08:29:13 +0000 | [diff] [blame] | 167 | if (Align) |
| 168 | O << Align; |
| 169 | else |
Evan Cheng | 5daafa9 | 2008-01-15 03:10:35 +0000 | [diff] [blame] | 170 | O << "\n" << IndentStr << " State.getTarget().getTargetData()" |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame^] | 171 | "->getABITypeAlignment(LocVT.getTypeForMVT())"; |
Evan Cheng | 5daafa9 | 2008-01-15 03:10:35 +0000 | [diff] [blame] | 172 | O << ");\n" << IndentStr |
Duncan Sands | 87b665d | 2007-11-14 08:29:13 +0000 | [diff] [blame] | 173 | << "State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset" |
Dale Johannesen | e3ef744 | 2007-11-10 22:07:15 +0000 | [diff] [blame] | 174 | << Counter << ", LocVT, LocInfo));\n"; |
| 175 | O << IndentStr << "return false;\n"; |
Chris Lattner | 88ee2a1 | 2007-02-27 22:05:51 +0000 | [diff] [blame] | 176 | } else if (Action->isSubClassOf("CCPromoteToType")) { |
Chris Lattner | 2092c8a | 2007-02-28 04:43:48 +0000 | [diff] [blame] | 177 | Record *DestTy = Action->getValueAsDef("DestTy"); |
| 178 | O << IndentStr << "LocVT = " << getEnumName(getValueType(DestTy)) <<";\n"; |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 179 | O << IndentStr << "if (ArgFlags.isSExt())\n" |
Anton Korobeynikov | d0b82b3 | 2007-03-07 16:25:09 +0000 | [diff] [blame] | 180 | << IndentStr << IndentStr << "LocInfo = CCValAssign::SExt;\n" |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 181 | << IndentStr << "else if (ArgFlags.isZExt())\n" |
Anton Korobeynikov | d0b82b3 | 2007-03-07 16:25:09 +0000 | [diff] [blame] | 182 | << IndentStr << IndentStr << "LocInfo = CCValAssign::ZExt;\n" |
| 183 | << IndentStr << "else\n" |
| 184 | << IndentStr << IndentStr << "LocInfo = CCValAssign::AExt;\n"; |
Evan Cheng | 6bfa8a1 | 2008-01-15 03:34:58 +0000 | [diff] [blame] | 185 | } else if (Action->isSubClassOf("CCPassByVal")) { |
| 186 | int Size = Action->getValueAsInt("Size"); |
| 187 | int Align = Action->getValueAsInt("Align"); |
| 188 | O << IndentStr |
| 189 | << "State.HandleByVal(ValNo, ValVT, LocVT, LocInfo, " |
| 190 | << Size << ", " << Align << ", ArgFlags);\n"; |
Rafael Espindola | 594d37e | 2007-08-10 14:44:42 +0000 | [diff] [blame] | 191 | O << IndentStr << "return false;\n"; |
Chris Lattner | 88ee2a1 | 2007-02-27 22:05:51 +0000 | [diff] [blame] | 192 | } else { |
| 193 | Action->dump(); |
| 194 | throw "Unknown CCAction!"; |
| 195 | } |
| 196 | } |
Chris Lattner | 50d4565 | 2007-02-27 22:08:27 +0000 | [diff] [blame] | 197 | } |