1 //===-- llvm/BinaryFormat/XCOFF.cpp - The XCOFF file format -----*- C++/-*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/BinaryFormat/XCOFF.h"
10 #include "llvm/ADT/SmallString.h"
11 #include "llvm/ADT/StringRef.h"
12 #include "llvm/Support/Errc.h"
13 #include "llvm/Support/Error.h"
18 case XCOFF::XMC_##A: \
20 StringRef
XCOFF::getMappingClassString(XCOFF::StorageMappingClass SMC
) {
46 // TODO: need to add a test case for "Unknown" and other SMC.
50 #define RELOC_CASE(A) \
53 StringRef
XCOFF::getRelocationTypeString(XCOFF::RelocationType Type
) {
83 #define LANG_CASE(A) \
84 case XCOFF::TracebackTable::A: \
87 StringRef
XCOFF::getNameForTracebackTableLanguageId(
88 XCOFF::TracebackTable::LanguageID LangId
) {
103 LANG_CASE(ObjectiveC
)
110 Expected
<SmallString
<32>> XCOFF::parseParmsType(uint32_t Value
,
111 unsigned FixedParmsNum
,
112 unsigned FloatingParmsNum
) {
113 SmallString
<32> ParmsType
;
115 unsigned ParsedFixedNum
= 0;
116 unsigned ParsedFloatingNum
= 0;
117 unsigned ParsedNum
= 0;
118 unsigned ParmsNum
= FixedParmsNum
+ FloatingParmsNum
;
120 // In the function PPCFunctionInfo::getParmsType(), when there are no vector
121 // parameters, the 31st bit of ParmsType is always zero even if it indicates a
122 // floating point parameter. The parameter type information is lost. There
123 // are only 8 GPRs used for parameters passing, the floating parameters
124 // also occupy GPRs if there are available, so the 31st bit can never be a
125 // fixed parameter. At the same time, we also do not know whether the zero of
126 // the 31st bit indicates a float or double parameter type here. Therefore, we
127 // ignore the 31st bit.
128 while (Bits
< 31 && ParsedNum
< ParmsNum
) {
131 if ((Value
& TracebackTable::ParmTypeIsFloatingBit
) == 0) {
132 // Fixed parameter type.
138 if ((Value
& TracebackTable::ParmTypeFloatingIsDoubleBit
) == 0)
139 // Float parameter type.
142 // Double parameter type.
150 // We have more parameters than the 32 Bits could encode.
151 if (ParsedNum
< ParmsNum
)
152 ParmsType
+= ", ...";
154 if (Value
!= 0u || ParsedFixedNum
> FixedParmsNum
||
155 ParsedFloatingNum
> FloatingParmsNum
)
156 return createStringError(errc::invalid_argument
,
157 "ParmsType encodes can not map to ParmsNum "
158 "parameters in parseParmsType.");
162 SmallString
<32> XCOFF::getExtendedTBTableFlagString(uint8_t Flag
) {
165 if (Flag
& ExtendedTBTableFlag::TB_OS1
)
167 if (Flag
& ExtendedTBTableFlag::TB_RESERVED
)
168 Res
+= "TB_RESERVED ";
169 if (Flag
& ExtendedTBTableFlag::TB_SSP_CANARY
)
170 Res
+= "TB_SSP_CANARY ";
171 if (Flag
& ExtendedTBTableFlag::TB_OS2
)
173 if (Flag
& ExtendedTBTableFlag::TB_EH_INFO
)
174 Res
+= "TB_EH_INFO ";
175 if (Flag
& ExtendedTBTableFlag::TB_LONGTBTABLE2
)
176 Res
+= "TB_LONGTBTABLE2 ";
178 // Two of the bits that haven't got used in the mask.
182 // Pop the last space.
187 Expected
<SmallString
<32>>
188 XCOFF::parseParmsTypeWithVecInfo(uint32_t Value
, unsigned FixedParmsNum
,
189 unsigned FloatingParmsNum
,
190 unsigned VectorParmsNum
) {
191 SmallString
<32> ParmsType
;
193 unsigned ParsedFixedNum
= 0;
194 unsigned ParsedFloatingNum
= 0;
195 unsigned ParsedVectorNum
= 0;
196 unsigned ParsedNum
= 0;
197 unsigned ParmsNum
= FixedParmsNum
+ FloatingParmsNum
+ VectorParmsNum
;
199 for (int Bits
= 0; Bits
< 32 && ParsedNum
< ParmsNum
; Bits
+= 2) {
203 switch (Value
& TracebackTable::ParmTypeMask
) {
204 case TracebackTable::ParmTypeIsFixedBits
:
208 case TracebackTable::ParmTypeIsVectorBits
:
212 case TracebackTable::ParmTypeIsFloatingBits
:
216 case TracebackTable::ParmTypeIsDoubleBits
:
221 assert(false && "Unrecognized bits in ParmsType.");
226 // We have more parameters than the 32 Bits could encode.
227 if (ParsedNum
< ParmsNum
)
228 ParmsType
+= ", ...";
230 if (Value
!= 0u || ParsedFixedNum
> FixedParmsNum
||
231 ParsedFloatingNum
> FloatingParmsNum
|| ParsedVectorNum
> VectorParmsNum
)
232 return createStringError(
233 errc::invalid_argument
,
234 "ParmsType encodes can not map to ParmsNum parameters "
235 "in parseParmsTypeWithVecInfo.");
240 Expected
<SmallString
<32>> XCOFF::parseVectorParmsType(uint32_t Value
,
242 SmallString
<32> ParmsType
;
243 unsigned ParsedNum
= 0;
244 for (int Bits
= 0; ParsedNum
< ParmsNum
&& Bits
< 32; Bits
+= 2) {
247 switch (Value
& TracebackTable::ParmTypeMask
) {
248 case TracebackTable::ParmTypeIsVectorCharBit
:
252 case TracebackTable::ParmTypeIsVectorShortBit
:
256 case TracebackTable::ParmTypeIsVectorIntBit
:
260 case TracebackTable::ParmTypeIsVectorFloatBit
:
268 // We have more parameters than the 32 Bits could encode.
269 if (ParsedNum
< ParmsNum
)
270 ParmsType
+= ", ...";
273 return createStringError(errc::invalid_argument
,
274 "ParmsType encodes more than ParmsNum parameters "
275 "in parseVectorParmsType.");