xref: /llvm-project/llvm/lib/Support/CSKYAttributeParser.cpp (revision 1c1b8c20c2d58b6a307e6fdc6db271e81ee8d603)
121bce900SZi Xuan Wu //===-- CSKYAttributeParser.cpp - CSKY Attribute Parser -----------------===//
221bce900SZi Xuan Wu //
321bce900SZi Xuan Wu // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
421bce900SZi Xuan Wu // See https://llvm.org/LICENSE.txt for license information.
521bce900SZi Xuan Wu // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
621bce900SZi Xuan Wu //
721bce900SZi Xuan Wu //===----------------------------------------------------------------------===//
821bce900SZi Xuan Wu 
921bce900SZi Xuan Wu #include "llvm/Support/CSKYAttributeParser.h"
1021bce900SZi Xuan Wu #include "llvm/ADT/StringExtras.h"
1121bce900SZi Xuan Wu #include "llvm/Support/Errc.h"
1221bce900SZi Xuan Wu 
1321bce900SZi Xuan Wu using namespace llvm;
1421bce900SZi Xuan Wu 
1521bce900SZi Xuan Wu const CSKYAttributeParser::DisplayHandler
1621bce900SZi Xuan Wu     CSKYAttributeParser::displayRoutines[] = {
1721bce900SZi Xuan Wu         {
1821bce900SZi Xuan Wu             CSKYAttrs::CSKY_ARCH_NAME,
1921bce900SZi Xuan Wu             &ELFAttributeParser::stringAttribute,
2021bce900SZi Xuan Wu         },
2121bce900SZi Xuan Wu         {
2221bce900SZi Xuan Wu             CSKYAttrs::CSKY_CPU_NAME,
2321bce900SZi Xuan Wu             &ELFAttributeParser::stringAttribute,
2421bce900SZi Xuan Wu         },
2521bce900SZi Xuan Wu         {
2621bce900SZi Xuan Wu             CSKYAttrs::CSKY_ISA_FLAGS,
2721bce900SZi Xuan Wu             &ELFAttributeParser::integerAttribute,
2821bce900SZi Xuan Wu         },
2921bce900SZi Xuan Wu         {
3021bce900SZi Xuan Wu             CSKYAttrs::CSKY_ISA_EXT_FLAGS,
3121bce900SZi Xuan Wu             &ELFAttributeParser::integerAttribute,
3221bce900SZi Xuan Wu         },
3321bce900SZi Xuan Wu         {
3421bce900SZi Xuan Wu             CSKYAttrs::CSKY_DSP_VERSION,
3521bce900SZi Xuan Wu             &CSKYAttributeParser::dspVersion,
3621bce900SZi Xuan Wu         },
3721bce900SZi Xuan Wu         {
3821bce900SZi Xuan Wu             CSKYAttrs::CSKY_VDSP_VERSION,
3921bce900SZi Xuan Wu             &CSKYAttributeParser::vdspVersion,
4021bce900SZi Xuan Wu         },
4121bce900SZi Xuan Wu         {
4221bce900SZi Xuan Wu             CSKYAttrs::CSKY_FPU_VERSION,
4321bce900SZi Xuan Wu             &CSKYAttributeParser::fpuVersion,
4421bce900SZi Xuan Wu         },
4521bce900SZi Xuan Wu         {
4621bce900SZi Xuan Wu             CSKYAttrs::CSKY_FPU_ABI,
4721bce900SZi Xuan Wu             &CSKYAttributeParser::fpuABI,
4821bce900SZi Xuan Wu         },
4921bce900SZi Xuan Wu         {
5021bce900SZi Xuan Wu             CSKYAttrs::CSKY_FPU_ROUNDING,
5121bce900SZi Xuan Wu             &CSKYAttributeParser::fpuRounding,
5221bce900SZi Xuan Wu         },
5321bce900SZi Xuan Wu         {
5421bce900SZi Xuan Wu             CSKYAttrs::CSKY_FPU_DENORMAL,
5521bce900SZi Xuan Wu             &CSKYAttributeParser::fpuDenormal,
5621bce900SZi Xuan Wu         },
5721bce900SZi Xuan Wu         {
5821bce900SZi Xuan Wu             CSKYAttrs::CSKY_FPU_EXCEPTION,
5921bce900SZi Xuan Wu             &CSKYAttributeParser::fpuException,
6021bce900SZi Xuan Wu         },
6121bce900SZi Xuan Wu         {
6221bce900SZi Xuan Wu             CSKYAttrs::CSKY_FPU_NUMBER_MODULE,
6321bce900SZi Xuan Wu             &ELFAttributeParser::stringAttribute,
6421bce900SZi Xuan Wu         },
6521bce900SZi Xuan Wu         {
6621bce900SZi Xuan Wu             CSKYAttrs::CSKY_FPU_HARDFP,
6721bce900SZi Xuan Wu             &CSKYAttributeParser::fpuHardFP,
6821bce900SZi Xuan Wu         }};
6921bce900SZi Xuan Wu 
7021bce900SZi Xuan Wu Error CSKYAttributeParser::handler(uint64_t tag, bool &handled) {
7121bce900SZi Xuan Wu   handled = false;
72ec5eab7eSKazu Hirata   for (const auto &AH : displayRoutines) {
73ec5eab7eSKazu Hirata     if (uint64_t(AH.attribute) == tag) {
74ec5eab7eSKazu Hirata       if (Error e = (this->*AH.routine)(tag))
7521bce900SZi Xuan Wu         return e;
7621bce900SZi Xuan Wu       handled = true;
7721bce900SZi Xuan Wu       break;
7821bce900SZi Xuan Wu     }
7921bce900SZi Xuan Wu   }
8021bce900SZi Xuan Wu 
8121bce900SZi Xuan Wu   return Error::success();
8221bce900SZi Xuan Wu }
8321bce900SZi Xuan Wu 
8421bce900SZi Xuan Wu Error CSKYAttributeParser::dspVersion(unsigned tag) {
85*1c1b8c20SPiotr Fusik   static const char *const strings[] = {"Error", "DSP Extension", "DSP 2.0"};
8638818b60Sserge-sans-paille   return parseStringAttribute("Tag_CSKY_DSP_VERSION", tag, ArrayRef(strings));
8721bce900SZi Xuan Wu }
8821bce900SZi Xuan Wu 
8921bce900SZi Xuan Wu Error CSKYAttributeParser::vdspVersion(unsigned tag) {
90*1c1b8c20SPiotr Fusik   static const char *const strings[] = {"Error", "VDSP Version 1",
91*1c1b8c20SPiotr Fusik                                         "VDSP Version 2"};
9238818b60Sserge-sans-paille   return parseStringAttribute("Tag_CSKY_VDSP_VERSION", tag, ArrayRef(strings));
9321bce900SZi Xuan Wu }
9421bce900SZi Xuan Wu 
9521bce900SZi Xuan Wu Error CSKYAttributeParser::fpuVersion(unsigned tag) {
96*1c1b8c20SPiotr Fusik   static const char *const strings[] = {"Error", "FPU Version 1",
97*1c1b8c20SPiotr Fusik                                         "FPU Version 2", "FPU Version 3"};
9838818b60Sserge-sans-paille   return parseStringAttribute("Tag_CSKY_FPU_VERSION", tag, ArrayRef(strings));
9921bce900SZi Xuan Wu }
10021bce900SZi Xuan Wu 
10121bce900SZi Xuan Wu Error CSKYAttributeParser::fpuABI(unsigned tag) {
102*1c1b8c20SPiotr Fusik   static const char *const strings[] = {"Error", "Soft", "SoftFP", "Hard"};
10338818b60Sserge-sans-paille   return parseStringAttribute("Tag_CSKY_FPU_ABI", tag, ArrayRef(strings));
10421bce900SZi Xuan Wu }
10521bce900SZi Xuan Wu 
10621bce900SZi Xuan Wu Error CSKYAttributeParser::fpuRounding(unsigned tag) {
107*1c1b8c20SPiotr Fusik   static const char *const strings[] = {"None", "Needed"};
10838818b60Sserge-sans-paille   return parseStringAttribute("Tag_CSKY_FPU_ROUNDING", tag, ArrayRef(strings));
10921bce900SZi Xuan Wu }
11021bce900SZi Xuan Wu 
11121bce900SZi Xuan Wu Error CSKYAttributeParser::fpuDenormal(unsigned tag) {
112*1c1b8c20SPiotr Fusik   static const char *const strings[] = {"None", "Needed"};
11338818b60Sserge-sans-paille   return parseStringAttribute("Tag_CSKY_FPU_DENORMAL", tag, ArrayRef(strings));
11421bce900SZi Xuan Wu }
11521bce900SZi Xuan Wu 
11621bce900SZi Xuan Wu Error CSKYAttributeParser::fpuException(unsigned tag) {
117*1c1b8c20SPiotr Fusik   static const char *const strings[] = {"None", "Needed"};
11838818b60Sserge-sans-paille   return parseStringAttribute("Tag_CSKY_FPU_EXCEPTION", tag, ArrayRef(strings));
11921bce900SZi Xuan Wu }
12021bce900SZi Xuan Wu 
12121bce900SZi Xuan Wu Error CSKYAttributeParser::fpuHardFP(unsigned tag) {
12221bce900SZi Xuan Wu   uint64_t value = de.getULEB128(cursor);
12321bce900SZi Xuan Wu   ListSeparator LS(" ");
12421bce900SZi Xuan Wu 
12521bce900SZi Xuan Wu   std::string description;
12621bce900SZi Xuan Wu 
12721bce900SZi Xuan Wu   if (value & 0x1) {
12821bce900SZi Xuan Wu     description += LS;
12921bce900SZi Xuan Wu     description += "Half";
13021bce900SZi Xuan Wu   }
13121bce900SZi Xuan Wu   if ((value >> 1) & 0x1) {
13221bce900SZi Xuan Wu     description += LS;
13321bce900SZi Xuan Wu     description += "Single";
13421bce900SZi Xuan Wu   }
13521bce900SZi Xuan Wu   if ((value >> 2) & 0x1) {
13621bce900SZi Xuan Wu     description += LS;
13721bce900SZi Xuan Wu     description += "Double";
13821bce900SZi Xuan Wu   }
13921bce900SZi Xuan Wu 
14021bce900SZi Xuan Wu   if (description.empty()) {
14121bce900SZi Xuan Wu     printAttribute(tag, value, "");
14221bce900SZi Xuan Wu     return createStringError(errc::invalid_argument,
14321bce900SZi Xuan Wu                              "unknown Tag_CSKY_FPU_HARDFP value: " +
14421bce900SZi Xuan Wu                                  Twine(value));
14521bce900SZi Xuan Wu   }
14621bce900SZi Xuan Wu 
14721bce900SZi Xuan Wu   printAttribute(tag, value, description);
14821bce900SZi Xuan Wu   return Error::success();
14921bce900SZi Xuan Wu }
150