121bce900SZi Xuan Wu //===----- unittests/CSKYAttributeParserTest.cpp --------------------------===// 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 #include "llvm/Support/CSKYAttributeParser.h" 921bce900SZi Xuan Wu #include "llvm/Support/ARMBuildAttributes.h" 1021bce900SZi Xuan Wu #include "llvm/Support/ELFAttributes.h" 1121bce900SZi Xuan Wu #include "gtest/gtest.h" 1221bce900SZi Xuan Wu #include <string> 1321bce900SZi Xuan Wu 1421bce900SZi Xuan Wu using namespace llvm; 1521bce900SZi Xuan Wu 1621bce900SZi Xuan Wu struct CSKYAttributeSection { 1721bce900SZi Xuan Wu unsigned Tag; 1821bce900SZi Xuan Wu struct { 1921bce900SZi Xuan Wu unsigned IntValue; 2021bce900SZi Xuan Wu const char *StringValue; 2121bce900SZi Xuan Wu } Value; 2221bce900SZi Xuan Wu 2321bce900SZi Xuan Wu CSKYAttributeSection(unsigned tag, unsigned value) : Tag(tag) { 2421bce900SZi Xuan Wu Value.IntValue = value; 2521bce900SZi Xuan Wu } 2621bce900SZi Xuan Wu 2721bce900SZi Xuan Wu CSKYAttributeSection(unsigned tag, const char *value) : Tag(tag) { 2821bce900SZi Xuan Wu Value.StringValue = value; 2921bce900SZi Xuan Wu } 3021bce900SZi Xuan Wu 3121bce900SZi Xuan Wu void writeInt(raw_ostream &OS) { 3221bce900SZi Xuan Wu OS.flush(); 3321bce900SZi Xuan Wu // Format version. 3421bce900SZi Xuan Wu OS << 'A' 3521bce900SZi Xuan Wu // uint32_t = VendorHeaderSize + TagHeaderSize + ContentsSize. 3621bce900SZi Xuan Wu << (uint8_t)16 << (uint8_t)0 << (uint8_t)0 3721bce900SZi Xuan Wu << (uint8_t)0 3821bce900SZi Xuan Wu // CurrentVendor. 3921bce900SZi Xuan Wu << "csky" 4021bce900SZi Xuan Wu << '\0' 4121bce900SZi Xuan Wu // ELFAttrs::File. 4221bce900SZi Xuan Wu << (uint8_t)1 4321bce900SZi Xuan Wu // uint32_t = TagHeaderSize + ContentsSize. 4421bce900SZi Xuan Wu << (uint8_t)6 << (uint8_t)0 << (uint8_t)0 4521bce900SZi Xuan Wu << (uint8_t)0 4621bce900SZi Xuan Wu // Tag 4721bce900SZi Xuan Wu << (uint8_t)Tag 4821bce900SZi Xuan Wu // IntValue 4921bce900SZi Xuan Wu << (uint8_t)Value.IntValue; 5021bce900SZi Xuan Wu } 5121bce900SZi Xuan Wu 5221bce900SZi Xuan Wu void writeString(raw_ostream &OS) { 5321bce900SZi Xuan Wu OS.flush(); 5421bce900SZi Xuan Wu // Format version. 5521bce900SZi Xuan Wu OS << 'A' 5621bce900SZi Xuan Wu // uint32_t = VendorHeaderSize + TagHeaderSize + ContentsSize. 5721bce900SZi Xuan Wu << (uint8_t)(16 + strlen(Value.StringValue)) << (uint8_t)0 << (uint8_t)0 5821bce900SZi Xuan Wu << (uint8_t)0 5921bce900SZi Xuan Wu // CurrentVendor. 6021bce900SZi Xuan Wu << "csky" 6121bce900SZi Xuan Wu << '\0' 6221bce900SZi Xuan Wu // ELFAttrs::File. 6321bce900SZi Xuan Wu << (uint8_t)1 6421bce900SZi Xuan Wu // uint32_t = TagHeaderSize + ContentsSize. 6521bce900SZi Xuan Wu << (uint8_t)(6 + strlen(Value.StringValue)) << (uint8_t)0 << (uint8_t)0 6621bce900SZi Xuan Wu << (uint8_t)0 6721bce900SZi Xuan Wu // Tag 6821bce900SZi Xuan Wu << (uint8_t)Tag 6921bce900SZi Xuan Wu // StringValue 7021bce900SZi Xuan Wu << Value.StringValue << '\0'; 7121bce900SZi Xuan Wu } 7221bce900SZi Xuan Wu }; 7321bce900SZi Xuan Wu 7421bce900SZi Xuan Wu static bool testAttributeInt(unsigned Tag, unsigned Value, unsigned ExpectedTag, 7521bce900SZi Xuan Wu unsigned ExpectedValue) { 7621bce900SZi Xuan Wu std::string buffer; 7721bce900SZi Xuan Wu raw_string_ostream OS(buffer); 7821bce900SZi Xuan Wu CSKYAttributeSection Section(Tag, Value); 7921bce900SZi Xuan Wu Section.writeInt(OS); 80*77bab2a6SJOE1994 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(buffer.c_str()), 81*77bab2a6SJOE1994 buffer.size()); 8221bce900SZi Xuan Wu 8321bce900SZi Xuan Wu CSKYAttributeParser Parser; 844a0ccfa8SKazu Hirata cantFail(Parser.parse(Bytes, llvm::endianness::little)); 8521bce900SZi Xuan Wu 866aebb5d1SFangrui Song std::optional<unsigned> Attr = Parser.getAttributeValue(ExpectedTag); 87d152e50cSKazu Hirata return Attr && *Attr == ExpectedValue; 8821bce900SZi Xuan Wu } 8921bce900SZi Xuan Wu 9021bce900SZi Xuan Wu static bool testAttributeString(unsigned Tag, const char *Value, 9121bce900SZi Xuan Wu unsigned ExpectedTag, 9221bce900SZi Xuan Wu const char *ExpectedValue) { 9321bce900SZi Xuan Wu std::string buffer; 9421bce900SZi Xuan Wu raw_string_ostream OS(buffer); 9521bce900SZi Xuan Wu CSKYAttributeSection Section(Tag, Value); 9621bce900SZi Xuan Wu Section.writeString(OS); 97*77bab2a6SJOE1994 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(buffer.c_str()), 98*77bab2a6SJOE1994 buffer.size()); 9921bce900SZi Xuan Wu 10021bce900SZi Xuan Wu CSKYAttributeParser Parser; 1014a0ccfa8SKazu Hirata cantFail(Parser.parse(Bytes, llvm::endianness::little)); 10221bce900SZi Xuan Wu 1036aebb5d1SFangrui Song std::optional<StringRef> Attr = Parser.getAttributeString(ExpectedTag); 104d152e50cSKazu Hirata return Attr && *Attr == ExpectedValue; 10521bce900SZi Xuan Wu } 10621bce900SZi Xuan Wu 10721bce900SZi Xuan Wu static void testParseError(unsigned Tag, unsigned Value, const char *msg) { 10821bce900SZi Xuan Wu std::string buffer; 10921bce900SZi Xuan Wu raw_string_ostream OS(buffer); 11021bce900SZi Xuan Wu CSKYAttributeSection Section(Tag, Value); 11121bce900SZi Xuan Wu Section.writeInt(OS); 112*77bab2a6SJOE1994 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(buffer.c_str()), 113*77bab2a6SJOE1994 buffer.size()); 11421bce900SZi Xuan Wu 11521bce900SZi Xuan Wu CSKYAttributeParser Parser; 1164a0ccfa8SKazu Hirata Error e = Parser.parse(Bytes, llvm::endianness::little); 11721bce900SZi Xuan Wu EXPECT_STREQ(toString(std::move(e)).c_str(), msg); 11821bce900SZi Xuan Wu } 11921bce900SZi Xuan Wu 12021bce900SZi Xuan Wu static bool testTagString(unsigned Tag, const char *name) { 12121bce900SZi Xuan Wu return ELFAttrs::attrTypeAsString(Tag, CSKYAttrs::getCSKYAttributeTags()) 12221bce900SZi Xuan Wu .str() == name; 12321bce900SZi Xuan Wu } 12421bce900SZi Xuan Wu 12521bce900SZi Xuan Wu TEST(ArchName, testAttribute) { 12621bce900SZi Xuan Wu EXPECT_TRUE(testTagString(4, "Tag_CSKY_ARCH_NAME")); 12721bce900SZi Xuan Wu EXPECT_TRUE( 12821bce900SZi Xuan Wu testAttributeString(4, "ck860", CSKYAttrs::CSKY_ARCH_NAME, "ck860")); 12921bce900SZi Xuan Wu EXPECT_FALSE( 13021bce900SZi Xuan Wu testAttributeString(4, "ck86", CSKYAttrs::CSKY_ARCH_NAME, "ck60")); 13121bce900SZi Xuan Wu } 13221bce900SZi Xuan Wu 13321bce900SZi Xuan Wu TEST(CPUName, testAttribute) { 13421bce900SZi Xuan Wu EXPECT_TRUE(testTagString(5, "Tag_CSKY_CPU_NAME")); 13521bce900SZi Xuan Wu EXPECT_TRUE( 13621bce900SZi Xuan Wu testAttributeString(5, "ck860fv", CSKYAttrs::CSKY_CPU_NAME, "ck860fv")); 13721bce900SZi Xuan Wu EXPECT_FALSE( 13821bce900SZi Xuan Wu testAttributeString(5, "ck860", CSKYAttrs::CSKY_CPU_NAME, "ck860fv")); 13921bce900SZi Xuan Wu } 14021bce900SZi Xuan Wu 14121bce900SZi Xuan Wu TEST(DSPVersion, testAttribute) { 14221bce900SZi Xuan Wu EXPECT_TRUE(testTagString(8, "Tag_CSKY_DSP_VERSION")); 14321bce900SZi Xuan Wu EXPECT_TRUE(testAttributeInt(8, 1, CSKYAttrs::CSKY_DSP_VERSION, 14421bce900SZi Xuan Wu CSKYAttrs::DSP_VERSION_EXTENSION)); 14521bce900SZi Xuan Wu EXPECT_TRUE(testAttributeInt(8, 2, CSKYAttrs::CSKY_DSP_VERSION, 14621bce900SZi Xuan Wu CSKYAttrs::DSP_VERSION_2)); 14721bce900SZi Xuan Wu EXPECT_FALSE(testAttributeInt(8, 0, CSKYAttrs::CSKY_DSP_VERSION, 14821bce900SZi Xuan Wu CSKYAttrs::DSP_VERSION_EXTENSION)); 14921bce900SZi Xuan Wu testParseError(8, 3, "unknown Tag_CSKY_DSP_VERSION value: 3"); 15021bce900SZi Xuan Wu } 15121bce900SZi Xuan Wu 15221bce900SZi Xuan Wu TEST(VDSPVersion, testAttribute) { 15321bce900SZi Xuan Wu EXPECT_TRUE(testTagString(9, "Tag_CSKY_VDSP_VERSION")); 15421bce900SZi Xuan Wu EXPECT_TRUE(testAttributeInt(9, 1, CSKYAttrs::CSKY_VDSP_VERSION, 15521bce900SZi Xuan Wu CSKYAttrs::VDSP_VERSION_1)); 15621bce900SZi Xuan Wu EXPECT_TRUE(testAttributeInt(9, 2, CSKYAttrs::CSKY_VDSP_VERSION, 15721bce900SZi Xuan Wu CSKYAttrs::VDSP_VERSION_2)); 15821bce900SZi Xuan Wu EXPECT_FALSE(testAttributeInt(9, 0, CSKYAttrs::CSKY_VDSP_VERSION, 15921bce900SZi Xuan Wu CSKYAttrs::VDSP_VERSION_2)); 16021bce900SZi Xuan Wu testParseError(9, 3, "unknown Tag_CSKY_VDSP_VERSION value: 3"); 16121bce900SZi Xuan Wu } 16221bce900SZi Xuan Wu 16321bce900SZi Xuan Wu TEST(FPUVersion, testAttribute) { 16421bce900SZi Xuan Wu EXPECT_TRUE(testTagString(16, "Tag_CSKY_FPU_VERSION")); 16521bce900SZi Xuan Wu EXPECT_TRUE(testAttributeInt(16, 1, CSKYAttrs::CSKY_FPU_VERSION, 16621bce900SZi Xuan Wu CSKYAttrs::FPU_VERSION_1)); 16721bce900SZi Xuan Wu EXPECT_TRUE(testAttributeInt(16, 2, CSKYAttrs::CSKY_FPU_VERSION, 16821bce900SZi Xuan Wu CSKYAttrs::FPU_VERSION_2)); 16921bce900SZi Xuan Wu EXPECT_TRUE(testAttributeInt(16, 3, CSKYAttrs::CSKY_FPU_VERSION, 17021bce900SZi Xuan Wu CSKYAttrs::FPU_VERSION_3)); 17121bce900SZi Xuan Wu EXPECT_FALSE(testAttributeInt(16, 0, CSKYAttrs::CSKY_FPU_VERSION, 17221bce900SZi Xuan Wu CSKYAttrs::FPU_VERSION_3)); 17321bce900SZi Xuan Wu testParseError(16, 4, "unknown Tag_CSKY_FPU_VERSION value: 4"); 17421bce900SZi Xuan Wu } 17521bce900SZi Xuan Wu 17621bce900SZi Xuan Wu TEST(FPUABI, testAttribute) { 17721bce900SZi Xuan Wu EXPECT_TRUE(testTagString(17, "Tag_CSKY_FPU_ABI")); 17821bce900SZi Xuan Wu EXPECT_TRUE(testAttributeInt(17, 1, CSKYAttrs::CSKY_FPU_ABI, 17921bce900SZi Xuan Wu CSKYAttrs::FPU_ABI_SOFT)); 18021bce900SZi Xuan Wu EXPECT_TRUE(testAttributeInt(17, 2, CSKYAttrs::CSKY_FPU_ABI, 18121bce900SZi Xuan Wu CSKYAttrs::FPU_ABI_SOFTFP)); 18221bce900SZi Xuan Wu EXPECT_TRUE(testAttributeInt(17, 3, CSKYAttrs::CSKY_FPU_ABI, 18321bce900SZi Xuan Wu CSKYAttrs::FPU_ABI_HARD)); 18421bce900SZi Xuan Wu EXPECT_FALSE(testAttributeInt(17, 0, CSKYAttrs::CSKY_FPU_ABI, 18521bce900SZi Xuan Wu CSKYAttrs::FPU_ABI_HARD)); 18621bce900SZi Xuan Wu testParseError(17, 4, "unknown Tag_CSKY_FPU_ABI value: 4"); 18721bce900SZi Xuan Wu } 18821bce900SZi Xuan Wu 18921bce900SZi Xuan Wu TEST(FPURounding, testAttribute) { 19021bce900SZi Xuan Wu EXPECT_TRUE(testTagString(18, "Tag_CSKY_FPU_ROUNDING")); 19121bce900SZi Xuan Wu EXPECT_TRUE( 19221bce900SZi Xuan Wu testAttributeInt(18, 0, CSKYAttrs::CSKY_FPU_ROUNDING, CSKYAttrs::NONE)); 19321bce900SZi Xuan Wu EXPECT_TRUE( 19421bce900SZi Xuan Wu testAttributeInt(18, 1, CSKYAttrs::CSKY_FPU_ROUNDING, CSKYAttrs::NEEDED)); 19521bce900SZi Xuan Wu testParseError(18, 2, "unknown Tag_CSKY_FPU_ROUNDING value: 2"); 19621bce900SZi Xuan Wu } 19721bce900SZi Xuan Wu 19821bce900SZi Xuan Wu TEST(FPUDenormal, testAttribute) { 19921bce900SZi Xuan Wu EXPECT_TRUE(testTagString(19, "Tag_CSKY_FPU_DENORMAL")); 20021bce900SZi Xuan Wu EXPECT_TRUE( 20121bce900SZi Xuan Wu testAttributeInt(19, 0, CSKYAttrs::CSKY_FPU_DENORMAL, CSKYAttrs::NONE)); 20221bce900SZi Xuan Wu EXPECT_TRUE( 20321bce900SZi Xuan Wu testAttributeInt(19, 1, CSKYAttrs::CSKY_FPU_DENORMAL, CSKYAttrs::NEEDED)); 20421bce900SZi Xuan Wu testParseError(19, 2, "unknown Tag_CSKY_FPU_DENORMAL value: 2"); 20521bce900SZi Xuan Wu } 20621bce900SZi Xuan Wu 20721bce900SZi Xuan Wu TEST(FPUException, testAttribute) { 20821bce900SZi Xuan Wu EXPECT_TRUE(testTagString(20, "Tag_CSKY_FPU_EXCEPTION")); 20921bce900SZi Xuan Wu EXPECT_TRUE( 21021bce900SZi Xuan Wu testAttributeInt(20, 0, CSKYAttrs::CSKY_FPU_EXCEPTION, CSKYAttrs::NONE)); 21121bce900SZi Xuan Wu EXPECT_TRUE(testAttributeInt(20, 1, CSKYAttrs::CSKY_FPU_EXCEPTION, 21221bce900SZi Xuan Wu CSKYAttrs::NEEDED)); 21321bce900SZi Xuan Wu testParseError(20, 2, "unknown Tag_CSKY_FPU_EXCEPTION value: 2"); 21421bce900SZi Xuan Wu } 21521bce900SZi Xuan Wu 21621bce900SZi Xuan Wu TEST(FPUNumberModule, testAttribute) { 21721bce900SZi Xuan Wu EXPECT_TRUE(testTagString(21, "Tag_CSKY_FPU_NUMBER_MODULE")); 21821bce900SZi Xuan Wu EXPECT_TRUE(testAttributeString( 21921bce900SZi Xuan Wu 21, "IEEE 754", CSKYAttrs::CSKY_FPU_NUMBER_MODULE, "IEEE 754")); 22021bce900SZi Xuan Wu EXPECT_FALSE(testAttributeString( 22121bce900SZi Xuan Wu 21, "IEEE 755", CSKYAttrs::CSKY_FPU_NUMBER_MODULE, "IEEE 754")); 22221bce900SZi Xuan Wu } 22321bce900SZi Xuan Wu 22421bce900SZi Xuan Wu TEST(FPUHardFP, testAttribute) { 22521bce900SZi Xuan Wu EXPECT_TRUE(testTagString(22, "Tag_CSKY_FPU_HARDFP")); 22621bce900SZi Xuan Wu EXPECT_TRUE(testAttributeInt(22, 1, CSKYAttrs::CSKY_FPU_HARDFP, 22721bce900SZi Xuan Wu CSKYAttrs::FPU_HARDFP_HALF)); 22821bce900SZi Xuan Wu EXPECT_TRUE(testAttributeInt(22, 2, CSKYAttrs::CSKY_FPU_HARDFP, 22921bce900SZi Xuan Wu CSKYAttrs::FPU_HARDFP_SINGLE)); 23021bce900SZi Xuan Wu EXPECT_TRUE(testAttributeInt(22, 4, CSKYAttrs::CSKY_FPU_HARDFP, 23121bce900SZi Xuan Wu CSKYAttrs::FPU_HARDFP_DOUBLE)); 23221bce900SZi Xuan Wu EXPECT_FALSE(testAttributeInt(22, 3, CSKYAttrs::CSKY_FPU_HARDFP, 23321bce900SZi Xuan Wu CSKYAttrs::FPU_HARDFP_DOUBLE)); 23421bce900SZi Xuan Wu testParseError(22, 0, "unknown Tag_CSKY_FPU_HARDFP value: 0"); 23521bce900SZi Xuan Wu testParseError(22, 8, "unknown Tag_CSKY_FPU_HARDFP value: 8"); 23621bce900SZi Xuan Wu } 237