|
12 | 12 | //===----------------------------------------------------------------------===//
|
13 | 13 |
|
14 | 14 | #include "LoongArchBaseInfo.h"
|
| 15 | +#include "LoongArchMCTargetDesc.h" |
15 | 16 | #include "llvm/ADT/ArrayRef.h"
|
16 | 17 | #include "llvm/MC/MCSubtargetInfo.h"
|
17 | 18 | #include "llvm/Support/ErrorHandling.h"
|
@@ -49,63 +50,127 @@ static ABI checkABIStandardized(ABI Abi) {
|
49 | 50 | return Abi;
|
50 | 51 | }
|
51 | 52 |
|
52 |
| -ABI computeTargetABI(const Triple &TT, StringRef ABIName) { |
53 |
| - ABI ArgProvidedABI = getTargetABI(ABIName); |
| 53 | +static ABI getTripleABI(const Triple &TT) { |
54 | 54 | bool Is64Bit = TT.isArch64Bit();
|
55 | 55 | ABI TripleABI;
|
56 |
| - |
57 |
| - // Figure out the ABI explicitly requested via the triple's environment type. |
58 | 56 | switch (TT.getEnvironment()) {
|
59 | 57 | case llvm::Triple::EnvironmentType::GNUSF:
|
60 |
| - TripleABI = Is64Bit ? LoongArchABI::ABI_LP64S : LoongArchABI::ABI_ILP32S; |
| 58 | + TripleABI = Is64Bit ? ABI_LP64S : ABI_ILP32S; |
61 | 59 | break;
|
62 | 60 | case llvm::Triple::EnvironmentType::GNUF32:
|
63 |
| - TripleABI = Is64Bit ? LoongArchABI::ABI_LP64F : LoongArchABI::ABI_ILP32F; |
| 61 | + TripleABI = Is64Bit ? ABI_LP64F : ABI_ILP32F; |
64 | 62 | break;
|
65 |
| - |
66 | 63 | // Let the fallback case behave like {ILP32,LP64}D.
|
67 | 64 | case llvm::Triple::EnvironmentType::GNUF64:
|
68 | 65 | default:
|
69 |
| - TripleABI = Is64Bit ? LoongArchABI::ABI_LP64D : LoongArchABI::ABI_ILP32D; |
| 66 | + TripleABI = Is64Bit ? ABI_LP64D : ABI_ILP32D; |
70 | 67 | break;
|
71 | 68 | }
|
| 69 | + return TripleABI; |
| 70 | +} |
72 | 71 |
|
73 |
| - switch (ArgProvidedABI) { |
74 |
| - case LoongArchABI::ABI_Unknown: |
75 |
| - // Fallback to the triple-implied ABI if ABI name is not specified or |
76 |
| - // invalid. |
77 |
| - if (!ABIName.empty()) |
78 |
| - errs() << "'" << ABIName |
79 |
| - << "' is not a recognized ABI for this target, ignoring and using " |
80 |
| - "triple-implied ABI\n"; |
81 |
| - return checkABIStandardized(TripleABI); |
| 72 | +ABI computeTargetABI(const Triple &TT, const FeatureBitset &FeatureBits, |
| 73 | + StringRef ABIName) { |
| 74 | + bool Is64Bit = TT.isArch64Bit(); |
| 75 | + ABI ArgProvidedABI = getTargetABI(ABIName); |
| 76 | + ABI TripleABI = getTripleABI(TT); |
| 77 | + |
| 78 | + auto IsABIValidForFeature = [=](ABI Abi) { |
| 79 | + switch (Abi) { |
| 80 | + default: |
| 81 | + return false; |
| 82 | + case ABI_ILP32S: |
| 83 | + return !Is64Bit; |
| 84 | + case ABI_ILP32F: |
| 85 | + return !Is64Bit && FeatureBits[LoongArch::FeatureBasicF]; |
| 86 | + case ABI_ILP32D: |
| 87 | + return !Is64Bit && FeatureBits[LoongArch::FeatureBasicD]; |
| 88 | + case ABI_LP64S: |
| 89 | + return Is64Bit; |
| 90 | + case ABI_LP64F: |
| 91 | + return Is64Bit && FeatureBits[LoongArch::FeatureBasicF]; |
| 92 | + case ABI_LP64D: |
| 93 | + return Is64Bit && FeatureBits[LoongArch::FeatureBasicD]; |
| 94 | + } |
| 95 | + }; |
| 96 | + |
| 97 | + // 1. If the '-target-abi' is valid, use it. |
| 98 | + if (IsABIValidForFeature(ArgProvidedABI)) { |
| 99 | + if (TT.hasEnvironment() && ArgProvidedABI != TripleABI) |
| 100 | + errs() |
| 101 | + << "warning: triple-implied ABI conflicts with provided target-abi '" |
| 102 | + << ABIName << "', using target-abi\n"; |
| 103 | + return checkABIStandardized(ArgProvidedABI); |
| 104 | + } |
82 | 105 |
|
83 |
| - case LoongArchABI::ABI_ILP32S: |
84 |
| - case LoongArchABI::ABI_ILP32F: |
85 |
| - case LoongArchABI::ABI_ILP32D: |
86 |
| - if (Is64Bit) { |
87 |
| - errs() << "32-bit ABIs are not supported for 64-bit targets, ignoring " |
88 |
| - "target-abi and using triple-implied ABI\n"; |
| 106 | + // 2. If the triple-implied ABI is valid, use it. |
| 107 | + if (IsABIValidForFeature(TripleABI)) { |
| 108 | + // If target-abi is not specified, use the valid triple-implied ABI. |
| 109 | + if (ABIName.empty()) |
89 | 110 | return checkABIStandardized(TripleABI);
|
90 |
| - } |
91 |
| - break; |
92 | 111 |
|
93 |
| - case LoongArchABI::ABI_LP64S: |
94 |
| - case LoongArchABI::ABI_LP64F: |
95 |
| - case LoongArchABI::ABI_LP64D: |
96 |
| - if (!Is64Bit) { |
97 |
| - errs() << "64-bit ABIs are not supported for 32-bit targets, ignoring " |
98 |
| - "target-abi and using triple-implied ABI\n"; |
| 112 | + switch (ArgProvidedABI) { |
| 113 | + case ABI_Unknown: |
| 114 | + // Fallback to the triple-implied ABI if ABI name is specified but |
| 115 | + // invalid. |
| 116 | + errs() << "warning: the '" << ABIName |
| 117 | + << "' is not a recognized ABI for this target, ignoring and " |
| 118 | + "using triple-implied ABI\n"; |
99 | 119 | return checkABIStandardized(TripleABI);
|
| 120 | + case ABI_ILP32S: |
| 121 | + case ABI_ILP32F: |
| 122 | + case ABI_ILP32D: |
| 123 | + if (Is64Bit) { |
| 124 | + errs() << "warning: 32-bit ABIs are not supported for 64-bit targets, " |
| 125 | + "ignoring and using triple-implied ABI\n"; |
| 126 | + return checkABIStandardized(TripleABI); |
| 127 | + } |
| 128 | + break; |
| 129 | + case ABI_LP64S: |
| 130 | + case ABI_LP64F: |
| 131 | + case ABI_LP64D: |
| 132 | + if (!Is64Bit) { |
| 133 | + errs() << "warning: 64-bit ABIs are not supported for 32-bit targets, " |
| 134 | + "ignoring and using triple-implied ABI\n"; |
| 135 | + return checkABIStandardized(TripleABI); |
| 136 | + } |
| 137 | + break; |
100 | 138 | }
|
101 |
| - break; |
102 |
| - } |
103 | 139 |
|
104 |
| - if (!ABIName.empty() && TT.hasEnvironment() && ArgProvidedABI != TripleABI) |
105 |
| - errs() << "warning: triple-implied ABI conflicts with provided target-abi '" |
106 |
| - << ABIName << "', using target-abi\n"; |
| 140 | + switch (ArgProvidedABI) { |
| 141 | + case ABI_ILP32F: |
| 142 | + case ABI_LP64F: |
| 143 | + errs() << "warning: the '" << ABIName |
| 144 | + << "' ABI can't be used for a target that doesn't support the 'F' " |
| 145 | + "instruction set, ignoring and using triple-implied ABI\n"; |
| 146 | + break; |
| 147 | + case ABI_ILP32D: |
| 148 | + case ABI_LP64D: |
| 149 | + errs() << "warning: the '" << ABIName |
| 150 | + << "' ABI can't be used for a target that doesn't support the 'D' " |
| 151 | + "instruction set, ignoring and using triple-implied ABI\n"; |
| 152 | + break; |
| 153 | + default: |
| 154 | + llvm_unreachable(""); |
| 155 | + } |
| 156 | + return checkABIStandardized(TripleABI); |
| 157 | + } |
107 | 158 |
|
108 |
| - return checkABIStandardized(ArgProvidedABI); |
| 159 | + // 3. Parse the 'feature-abi', and use it. |
| 160 | + auto GetFeatureABI = [=]() { |
| 161 | + if (FeatureBits[LoongArch::FeatureBasicD]) |
| 162 | + return Is64Bit ? ABI_LP64D : ABI_ILP32D; |
| 163 | + if (FeatureBits[LoongArch::FeatureBasicF]) |
| 164 | + return Is64Bit ? ABI_LP64F : ABI_ILP32F; |
| 165 | + return Is64Bit ? ABI_LP64S : ABI_ILP32S; |
| 166 | + }; |
| 167 | + if (ABIName.empty()) |
| 168 | + errs() << "warning: the triple-implied ABI is invalid, ignoring and using " |
| 169 | + "feature-implied ABI\n"; |
| 170 | + else |
| 171 | + errs() << "warning: both target-abi and the triple-implied ABI are " |
| 172 | + "invalid, ignoring and using feature-implied ABI\n"; |
| 173 | + return checkABIStandardized(GetFeatureABI()); |
109 | 174 | }
|
110 | 175 |
|
111 | 176 | ABI getTargetABI(StringRef ABIName) {
|
|
0 commit comments