1 //===--- ARMAttributeParser.cpp - ARM Attribute Information Printer -------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "ARMAttributeParser.h"
11 #include "StreamWriter.h"
12 #include "llvm/ADT/StringExtras.h"
13 #include "llvm/Support/LEB128.h"
16 using namespace llvm::ARMBuildAttrs;
19 static const EnumEntry<unsigned> TagNames[] = {
20 { "Tag_File", ARMBuildAttrs::File },
21 { "Tag_Section", ARMBuildAttrs::Section },
22 { "Tag_Symbol", ARMBuildAttrs::Symbol },
25 template <typename type_, size_t size_>
26 size_t countof(const type_ (&)[size_]) {
31 #define ATTRIBUTE_HANDLER(Attr_) \
32 { ARMBuildAttrs::Attr_, &ARMAttributeParser::Attr_ }
34 const ARMAttributeParser::DisplayHandler
35 ARMAttributeParser::DisplayRoutines[] = {
36 { ARMBuildAttrs::CPU_raw_name, &ARMAttributeParser::StringAttribute, },
37 { ARMBuildAttrs::CPU_name, &ARMAttributeParser::StringAttribute },
38 ATTRIBUTE_HANDLER(CPU_arch),
39 ATTRIBUTE_HANDLER(CPU_arch_profile),
40 ATTRIBUTE_HANDLER(ARM_ISA_use),
41 ATTRIBUTE_HANDLER(THUMB_ISA_use),
42 ATTRIBUTE_HANDLER(FP_arch),
43 ATTRIBUTE_HANDLER(WMMX_arch),
44 ATTRIBUTE_HANDLER(Advanced_SIMD_arch),
45 ATTRIBUTE_HANDLER(PCS_config),
46 ATTRIBUTE_HANDLER(ABI_PCS_R9_use),
47 ATTRIBUTE_HANDLER(ABI_PCS_RW_data),
48 ATTRIBUTE_HANDLER(ABI_PCS_RO_data),
49 ATTRIBUTE_HANDLER(ABI_PCS_GOT_use),
50 ATTRIBUTE_HANDLER(ABI_PCS_wchar_t),
51 ATTRIBUTE_HANDLER(ABI_FP_rounding),
52 ATTRIBUTE_HANDLER(ABI_FP_denormal),
53 ATTRIBUTE_HANDLER(ABI_FP_exceptions),
54 ATTRIBUTE_HANDLER(ABI_FP_user_exceptions),
55 ATTRIBUTE_HANDLER(ABI_FP_number_model),
56 ATTRIBUTE_HANDLER(ABI_align_needed),
57 ATTRIBUTE_HANDLER(ABI_align_preserved),
58 ATTRIBUTE_HANDLER(ABI_enum_size),
59 ATTRIBUTE_HANDLER(ABI_HardFP_use),
60 ATTRIBUTE_HANDLER(ABI_VFP_args),
61 ATTRIBUTE_HANDLER(ABI_WMMX_args),
62 ATTRIBUTE_HANDLER(ABI_optimization_goals),
63 ATTRIBUTE_HANDLER(ABI_FP_optimization_goals),
64 ATTRIBUTE_HANDLER(compatibility),
65 ATTRIBUTE_HANDLER(CPU_unaligned_access),
66 ATTRIBUTE_HANDLER(FP_HP_extension),
67 ATTRIBUTE_HANDLER(ABI_FP_16bit_format),
68 ATTRIBUTE_HANDLER(MPextension_use),
69 ATTRIBUTE_HANDLER(DIV_use),
70 ATTRIBUTE_HANDLER(T2EE_use),
71 ATTRIBUTE_HANDLER(Virtualization_use),
72 ATTRIBUTE_HANDLER(nodefaults)
75 #undef ATTRIBUTE_HANDLER
77 uint64_t ARMAttributeParser::ParseInteger(const uint8_t *Data,
80 uint64_t Value = decodeULEB128(Data + Offset, &Length);
81 Offset = Offset + Length;
85 StringRef ARMAttributeParser::ParseString(const uint8_t *Data,
87 const char *String = reinterpret_cast<const char*>(Data + Offset);
88 size_t Length = std::strlen(String);
89 Offset = Offset + Length + 1;
90 return StringRef(String, Length);
93 void ARMAttributeParser::IntegerAttribute(AttrType Tag, const uint8_t *Data,
95 SW.printNumber(ARMBuildAttrs::AttrTypeAsString(Tag),
96 ParseInteger(Data, Offset));
99 void ARMAttributeParser::StringAttribute(AttrType Tag, const uint8_t *Data,
101 StringRef TagName = ARMBuildAttrs::AttrTypeAsString(Tag, /*TagPrefix*/false);
103 DictScope AS(SW, "Attribute");
104 SW.printNumber("Tag", Tag);
105 if (!TagName.empty())
106 SW.printString("TagName", TagName);
107 SW.printString("Value", ParseString(Data, Offset));
110 void ARMAttributeParser::PrintAttribute(unsigned Tag, unsigned Value,
111 StringRef ValueDesc) {
112 StringRef TagName = ARMBuildAttrs::AttrTypeAsString(Tag, /*TagPrefix*/false);
114 DictScope AS(SW, "Attribute");
115 SW.printNumber("Tag", Tag);
116 SW.printNumber("Value", Value);
117 if (!TagName.empty())
118 SW.printString("TagName", TagName);
119 if (!ValueDesc.empty())
120 SW.printString("Description", ValueDesc);
123 void ARMAttributeParser::CPU_arch(AttrType Tag, const uint8_t *Data,
125 static const char *Strings[] = {
126 "Pre-v4", "ARM v4", "ARM v4T", "ARM v5T", "ARM v5TE", "ARM v5TEJ", "ARM v6",
127 "ARM v6KZ", "ARM v6T2", "ARM v6K", "ARM v7", "ARM v6-M", "ARM v6S-M",
128 "ARM v7E-M", "ARM v8"
131 uint64_t Value = ParseInteger(Data, Offset);
132 StringRef ValueDesc = (Value < countof(Strings)) ? Strings[Value] : NULL;
133 PrintAttribute(Tag, Value, ValueDesc);
136 void ARMAttributeParser::CPU_arch_profile(AttrType Tag, const uint8_t *Data,
138 uint64_t Encoded = ParseInteger(Data, Offset);
142 default: Profile = "Unknown"; break;
143 case 'A': Profile = "Application"; break;
144 case 'R': Profile = "Real-time"; break;
145 case 'M': Profile = "Microcontroller"; break;
146 case 'S': Profile = "Classic"; break;
147 case '0': Profile = "None"; break;
150 PrintAttribute(Tag, Encoded, Profile);
153 void ARMAttributeParser::ARM_ISA_use(AttrType Tag, const uint8_t *Data,
155 static const char *Strings[] = { "Not Permitted", "Permitted" };
157 uint64_t Value = ParseInteger(Data, Offset);
158 StringRef ValueDesc = (Value < countof(Strings)) ? Strings[Value] : NULL;
159 PrintAttribute(Tag, Value, ValueDesc);
162 void ARMAttributeParser::THUMB_ISA_use(AttrType Tag, const uint8_t *Data,
164 static const char *Strings[] = { "Not Permitted", "Thumb-1", "Thumb-2" };
166 uint64_t Value = ParseInteger(Data, Offset);
167 StringRef ValueDesc = (Value < countof(Strings)) ? Strings[Value] : NULL;
168 PrintAttribute(Tag, Value, ValueDesc);
171 void ARMAttributeParser::FP_arch(AttrType Tag, const uint8_t *Data,
173 static const char *Strings[] = {
174 "Not Permitted", "VFPv1", "VFPv2", "VFPv3", "VFPv3-D16", "VFPv4",
175 "VFPv4-D16", "ARMv8-a FP", "ARMv8-a FP-D16"
178 uint64_t Value = ParseInteger(Data, Offset);
179 StringRef ValueDesc = (Value < countof(Strings)) ? Strings[Value] : NULL;
180 PrintAttribute(Tag, Value, ValueDesc);
183 void ARMAttributeParser::WMMX_arch(AttrType Tag, const uint8_t *Data,
185 static const char *Strings[] = { "Not Permitted", "WMMXv1", "WMMXv2" };
187 uint64_t Value = ParseInteger(Data, Offset);
188 StringRef ValueDesc = (Value < countof(Strings)) ? Strings[Value] : NULL;
189 PrintAttribute(Tag, Value, ValueDesc);
192 void ARMAttributeParser::Advanced_SIMD_arch(AttrType Tag, const uint8_t *Data,
194 static const char *Strings[] = {
195 "Not Permitted", "NEONv1", "NEONv2+FMA", "ARMv8-a NEON"
198 uint64_t Value = ParseInteger(Data, Offset);
199 StringRef ValueDesc = (Value < countof(Strings)) ? Strings[Value] : NULL;
200 PrintAttribute(Tag, Value, ValueDesc);
203 void ARMAttributeParser::PCS_config(AttrType Tag, const uint8_t *Data,
205 static const char *Strings[] = {
206 "None", "Bare Platform", "Linux Application", "Linux DSO", "Palm OS 2004",
207 "Reserved (Palm OS)", "Symbian OS 2004", "Reserved (Symbian OS)"
210 uint64_t Value = ParseInteger(Data, Offset);
211 StringRef ValueDesc = (Value < countof(Strings)) ? Strings[Value] : NULL;
212 PrintAttribute(Tag, Value, ValueDesc);
215 void ARMAttributeParser::ABI_PCS_R9_use(AttrType Tag, const uint8_t *Data,
217 static const char *Strings[] = { "v6", "Static Base", "TLS", "Unused" };
219 uint64_t Value = ParseInteger(Data, Offset);
220 StringRef ValueDesc = (Value < countof(Strings)) ? Strings[Value] : NULL;
221 PrintAttribute(Tag, Value, ValueDesc);
224 void ARMAttributeParser::ABI_PCS_RW_data(AttrType Tag, const uint8_t *Data,
226 static const char *Strings[] = {
227 "Absolute", "PC-relative", "SB-relative", "Not Permitted"
230 uint64_t Value = ParseInteger(Data, Offset);
231 StringRef ValueDesc = (Value < countof(Strings)) ? Strings[Value] : NULL;
232 PrintAttribute(Tag, Value, ValueDesc);
235 void ARMAttributeParser::ABI_PCS_RO_data(AttrType Tag, const uint8_t *Data,
237 static const char *Strings[] = { "Absolute", "PC-relative", "Not Permitted" };
239 uint64_t Value = ParseInteger(Data, Offset);
240 StringRef ValueDesc = (Value < countof(Strings)) ? Strings[Value] : NULL;
241 PrintAttribute(Tag, Value, ValueDesc);
244 void ARMAttributeParser::ABI_PCS_GOT_use(AttrType Tag, const uint8_t *Data,
246 static const char *Strings[] = { "Not Permitted", "Direct", "GOT-Indirect" };
248 uint64_t Value = ParseInteger(Data, Offset);
249 StringRef ValueDesc = (Value < countof(Strings)) ? Strings[Value] : NULL;
250 PrintAttribute(Tag, Value, ValueDesc);
253 void ARMAttributeParser::ABI_PCS_wchar_t(AttrType Tag, const uint8_t *Data,
255 static const char *Strings[] = {
256 "Not Permitted", "Unknown", "2-byte", "Unknown", "4-byte"
259 uint64_t Value = ParseInteger(Data, Offset);
260 StringRef ValueDesc = (Value < countof(Strings)) ? Strings[Value] : NULL;
261 PrintAttribute(Tag, Value, ValueDesc);
264 void ARMAttributeParser::ABI_FP_rounding(AttrType Tag, const uint8_t *Data,
266 static const char *Strings[] = { "IEEE-754", "Runtime" };
268 uint64_t Value = ParseInteger(Data, Offset);
269 StringRef ValueDesc = (Value < countof(Strings)) ? Strings[Value] : NULL;
270 PrintAttribute(Tag, Value, ValueDesc);
273 void ARMAttributeParser::ABI_FP_denormal(AttrType Tag, const uint8_t *Data,
275 static const char *Strings[] = { "Unsupported", "IEEE-754", "Sign Only" };
277 uint64_t Value = ParseInteger(Data, Offset);
278 StringRef ValueDesc = (Value < countof(Strings)) ? Strings[Value] : NULL;
279 PrintAttribute(Tag, Value, ValueDesc);
282 void ARMAttributeParser::ABI_FP_exceptions(AttrType Tag, const uint8_t *Data,
284 static const char *Strings[] = { "Not Permitted", "IEEE-754" };
286 uint64_t Value = ParseInteger(Data, Offset);
287 StringRef ValueDesc = (Value < countof(Strings)) ? Strings[Value] : NULL;
288 PrintAttribute(Tag, Value, ValueDesc);
291 void ARMAttributeParser::ABI_FP_user_exceptions(AttrType Tag,
294 static const char *Strings[] = { "Not Permitted", "IEEE-754" };
296 uint64_t Value = ParseInteger(Data, Offset);
297 StringRef ValueDesc = (Value < countof(Strings)) ? Strings[Value] : NULL;
298 PrintAttribute(Tag, Value, ValueDesc);
301 void ARMAttributeParser::ABI_FP_number_model(AttrType Tag, const uint8_t *Data,
303 static const char *Strings[] = {
304 "Not Permitted", "Finite Only", "RTABI", "IEEE-754"
307 uint64_t Value = ParseInteger(Data, Offset);
308 StringRef ValueDesc = (Value < countof(Strings)) ? Strings[Value] : NULL;
309 PrintAttribute(Tag, Value, ValueDesc);
312 void ARMAttributeParser::ABI_align_needed(AttrType Tag, const uint8_t *Data,
314 static const char *Strings[] = {
315 "Not Permitted", "8-byte alignment", "4-byte alignment", "Reserved"
318 uint64_t Value = ParseInteger(Data, Offset);
320 std::string Description;
321 if (Value < countof(Strings))
322 Description = std::string(Strings[Value]);
323 else if (Value <= 12)
324 Description = std::string("8-byte alignment, ") + utostr(1 << Value)
325 + std::string("-byte extended alignment");
327 Description = "Invalid";
329 PrintAttribute(Tag, Value, Description);
332 void ARMAttributeParser::ABI_align_preserved(AttrType Tag, const uint8_t *Data,
334 static const char *Strings[] = {
335 "Not Required", "8-byte data alignment", "8-byte data and code alignment",
339 uint64_t Value = ParseInteger(Data, Offset);
341 std::string Description;
342 if (Value < countof(Strings))
343 Description = std::string(Strings[Value]);
344 else if (Value <= 12)
345 Description = std::string("8-byte stack alignment, ") + utostr(1 << Value)
346 + std::string("-byte data alignment");
348 Description = "Invalid";
350 PrintAttribute(Tag, Value, Description);
353 void ARMAttributeParser::ABI_enum_size(AttrType Tag, const uint8_t *Data,
355 static const char *Strings[] = {
356 "Not Permitted", "Packed", "Int32", "External Int32"
359 uint64_t Value = ParseInteger(Data, Offset);
360 StringRef ValueDesc = (Value < countof(Strings)) ? Strings[Value] : NULL;
361 PrintAttribute(Tag, Value, ValueDesc);
364 void ARMAttributeParser::ABI_HardFP_use(AttrType Tag, const uint8_t *Data,
366 static const char *Strings[] = {
367 "Tag_FP_arch", "Single-Precision", "Reserved", "Tag_FP_arch (deprecated)"
370 uint64_t Value = ParseInteger(Data, Offset);
371 StringRef ValueDesc = (Value < countof(Strings)) ? Strings[Value] : NULL;
372 PrintAttribute(Tag, Value, ValueDesc);
375 void ARMAttributeParser::ABI_VFP_args(AttrType Tag, const uint8_t *Data,
377 static const char *Strings[] = {
378 "AAPCS", "AAPCS VFP", "Custom", "Not Permitted"
381 uint64_t Value = ParseInteger(Data, Offset);
382 StringRef ValueDesc = (Value < countof(Strings)) ? Strings[Value] : NULL;
383 PrintAttribute(Tag, Value, ValueDesc);
386 void ARMAttributeParser::ABI_WMMX_args(AttrType Tag, const uint8_t *Data,
388 static const char *Strings[] = { "AAPCS", "iWMMX", "Custom" };
390 uint64_t Value = ParseInteger(Data, Offset);
391 StringRef ValueDesc = (Value < countof(Strings)) ? Strings[Value] : NULL;
392 PrintAttribute(Tag, Value, ValueDesc);
395 void ARMAttributeParser::ABI_optimization_goals(AttrType Tag,
398 static const char *Strings[] = {
399 "None", "Speed", "Aggressive Speed", "Size", "Aggressive Size", "Debugging",
403 uint64_t Value = ParseInteger(Data, Offset);
404 StringRef ValueDesc = (Value < countof(Strings)) ? Strings[Value] : NULL;
405 PrintAttribute(Tag, Value, ValueDesc);
408 void ARMAttributeParser::ABI_FP_optimization_goals(AttrType Tag,
411 static const char *Strings[] = {
412 "None", "Speed", "Aggressive Speed", "Size", "Aggressive Size", "Accuracy",
416 uint64_t Value = ParseInteger(Data, Offset);
417 StringRef ValueDesc = (Value < countof(Strings)) ? Strings[Value] : NULL;
418 PrintAttribute(Tag, Value, ValueDesc);
421 void ARMAttributeParser::compatibility(AttrType Tag, const uint8_t *Data,
423 uint64_t Integer = ParseInteger(Data, Offset);
424 StringRef String = ParseString(Data, Offset);
426 DictScope AS(SW, "Attribute");
427 SW.printNumber("Tag", Tag);
428 SW.startLine() << "Value: " << Integer << ", " << String << '\n';
429 SW.printString("TagName", AttrTypeAsString(Tag, /*TagPrefix*/false));
432 SW.printString("Description", StringRef("No Specific Requirements"));
435 SW.printString("Description", StringRef("AEABI Conformant"));
438 SW.printString("Description", StringRef("AEABI Non-Conformant"));
443 void ARMAttributeParser::CPU_unaligned_access(AttrType Tag, const uint8_t *Data,
445 static const char *Strings[] = { "Not Permitted", "v6-style" };
447 uint64_t Value = ParseInteger(Data, Offset);
448 StringRef ValueDesc = (Value < countof(Strings)) ? Strings[Value] : NULL;
449 PrintAttribute(Tag, Value, ValueDesc);
452 void ARMAttributeParser::FP_HP_extension(AttrType Tag, const uint8_t *Data,
454 static const char *Strings[] = { "If Available", "Permitted" };
456 uint64_t Value = ParseInteger(Data, Offset);
457 StringRef ValueDesc = (Value < countof(Strings)) ? Strings[Value] : NULL;
458 PrintAttribute(Tag, Value, ValueDesc);
461 void ARMAttributeParser::ABI_FP_16bit_format(AttrType Tag, const uint8_t *Data,
463 static const char *Strings[] = { "Not Permitted", "IEEE-754", "VFPv3" };
465 uint64_t Value = ParseInteger(Data, Offset);
466 StringRef ValueDesc = (Value < countof(Strings)) ? Strings[Value] : NULL;
467 PrintAttribute(Tag, Value, ValueDesc);
470 void ARMAttributeParser::MPextension_use(AttrType Tag, const uint8_t *Data,
472 static const char *Strings[] = { "Not Permitted", "Permitted" };
474 uint64_t Value = ParseInteger(Data, Offset);
475 StringRef ValueDesc = (Value < countof(Strings)) ? Strings[Value] : NULL;
476 PrintAttribute(Tag, Value, ValueDesc);
479 void ARMAttributeParser::DIV_use(AttrType Tag, const uint8_t *Data,
481 static const char *Strings[] = {
482 "If Available", "Not Permitted", "Permitted"
485 uint64_t Value = ParseInteger(Data, Offset);
486 StringRef ValueDesc = (Value < countof(Strings)) ? Strings[Value] : NULL;
487 PrintAttribute(Tag, Value, ValueDesc);
490 void ARMAttributeParser::T2EE_use(AttrType Tag, const uint8_t *Data,
492 static const char *Strings[] = { "Not Permitted", "Permitted" };
494 uint64_t Value = ParseInteger(Data, Offset);
495 StringRef ValueDesc = (Value < countof(Strings)) ? Strings[Value] : NULL;
496 PrintAttribute(Tag, Value, ValueDesc);
499 void ARMAttributeParser::Virtualization_use(AttrType Tag, const uint8_t *Data,
501 static const char *Strings[] = {
502 "Not Permitted", "TrustZone", "Virtualization Extensions",
503 "TrustZone + Virtualization Extensions"
506 uint64_t Value = ParseInteger(Data, Offset);
507 StringRef ValueDesc = (Value < countof(Strings)) ? Strings[Value] : NULL;
508 PrintAttribute(Tag, Value, ValueDesc);
511 void ARMAttributeParser::nodefaults(AttrType Tag, const uint8_t *Data,
513 uint64_t Value = ParseInteger(Data, Offset);
514 PrintAttribute(Tag, Value, "Unspecified Tags UNDEFINED");
517 void ARMAttributeParser::ParseIndexList(const uint8_t *Data, uint32_t &Offset,
518 SmallVectorImpl<uint8_t> &IndexList) {
521 uint64_t Value = decodeULEB128(Data + Offset, &Length);
522 Offset = Offset + Length;
525 IndexList.push_back(Value);
529 void ARMAttributeParser::ParseAttributeList(const uint8_t *Data,
530 uint32_t &Offset, uint32_t Length) {
531 while (Offset < Length) {
533 uint64_t Tag = decodeULEB128(Data + Offset, &Length);
536 bool Handled = false;
537 for (unsigned AHI = 0, AHE = countof(DisplayRoutines);
538 AHI != AHE && !Handled; ++AHI) {
539 if (DisplayRoutines[AHI].Attribute == Tag) {
540 (this->*DisplayRoutines[AHI].Routine)(ARMBuildAttrs::AttrType(Tag),
548 errs() << "unhandled AEABI Tag " << Tag
549 << " (" << ARMBuildAttrs::AttrTypeAsString(Tag) << ")\n";
554 IntegerAttribute(ARMBuildAttrs::AttrType(Tag), Data, Offset);
556 StringAttribute(ARMBuildAttrs::AttrType(Tag), Data, Offset);
561 void ARMAttributeParser::ParseSubsection(const uint8_t *Data, uint32_t Length) {
562 uint32_t Offset = sizeof(uint32_t); /* SectionLength */
564 SW.printNumber("SectionLength", Length);
566 const char *VendorName = reinterpret_cast<const char*>(Data + Offset);
567 size_t VendorNameLength = std::strlen(VendorName);
568 SW.printString("Vendor", StringRef(VendorName, VendorNameLength));
569 Offset = Offset + VendorNameLength + 1;
571 if (StringRef(VendorName, VendorNameLength).lower() != "aeabi")
574 while (Offset < Length) {
575 /// Tag_File | Tag_Section | Tag_Symbol uleb128:byte-size
576 uint8_t Tag = Data[Offset];
577 SW.printEnum("Tag", Tag, makeArrayRef(TagNames));
578 Offset = Offset + sizeof(Tag);
581 *reinterpret_cast<const support::ulittle32_t*>(Data + Offset);
582 SW.printNumber("Size", Size);
583 Offset = Offset + sizeof(Size);
586 errs() << "subsection length greater than section length\n";
590 StringRef ScopeName, IndexName;
591 SmallVector<uint8_t, 8> Indicies;
593 case ARMBuildAttrs::File:
594 ScopeName = "FileAttributes";
596 case ARMBuildAttrs::Section:
597 ScopeName = "SectionAttributes";
598 IndexName = "Sections";
599 ParseIndexList(Data, Offset, Indicies);
601 case ARMBuildAttrs::Symbol:
602 ScopeName = "SymbolAttributes";
603 IndexName = "Symbols";
604 ParseIndexList(Data, Offset, Indicies);
607 errs() << "unrecognised tag: 0x" << utohexstr(Tag) << '\n';
611 DictScope ASS(SW, ScopeName);
613 if (!Indicies.empty())
614 SW.printList(IndexName, Indicies);
616 ParseAttributeList(Data, Offset, Length);
620 void ARMAttributeParser::Parse(ArrayRef<uint8_t> Section) {
622 unsigned SectionNumber = 0;
624 while (Offset < Section.size()) {
625 uint32_t SectionLength =
626 *reinterpret_cast<const support::ulittle32_t*>(Section.data() + Offset);
628 SW.startLine() << "Section " << ++SectionNumber << " {\n";
631 ParseSubsection(Section.data() + Offset, SectionLength);
632 Offset = Offset + SectionLength;
635 SW.startLine() << "}\n";