1 | //===- llvm/unittest/MC/MCInstPrinter.cpp ---------------------------------===// |
2 | // |
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 |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "llvm/MC/MCInstPrinter.h" |
10 | #include "llvm/MC/MCAsmInfo.h" |
11 | #include "llvm/MC/MCInstrInfo.h" |
12 | #include "llvm/MC/MCRegisterInfo.h" |
13 | #include "llvm/MC/MCTargetOptions.h" |
14 | #include "llvm/MC/TargetRegistry.h" |
15 | #include "llvm/Support/TargetSelect.h" |
16 | #include "llvm/Target/TargetMachine.h" |
17 | #include "llvm/Target/TargetOptions.h" |
18 | #include "gtest/gtest.h" |
19 | |
20 | using namespace llvm; |
21 | |
22 | namespace { |
23 | class MCInstPrinterTest : public ::testing::Test { |
24 | public: |
25 | std::unique_ptr<MCRegisterInfo> MRI; |
26 | std::unique_ptr<MCAsmInfo> MAI; |
27 | std::unique_ptr<const MCInstrInfo> MII; |
28 | std::unique_ptr<MCInstPrinter> Printer; |
29 | |
30 | MCInstPrinterTest() { |
31 | llvm::InitializeAllTargetInfos(); |
32 | llvm::InitializeAllTargetMCs(); |
33 | |
34 | std::string TripleName = "x86_64-pc-linux" ; |
35 | std::string ErrorStr; |
36 | |
37 | const Target *TheTarget = |
38 | TargetRegistry::lookupTarget(Triple: TripleName, Error&: ErrorStr); |
39 | |
40 | // If we didn't build x86, do not run the test. |
41 | if (!TheTarget) |
42 | return; |
43 | |
44 | MRI.reset(p: TheTarget->createMCRegInfo(TT: TripleName)); |
45 | MCTargetOptions MCOptions; |
46 | MAI.reset(p: TheTarget->createMCAsmInfo(MRI: *MRI, TheTriple: TripleName, Options: MCOptions)); |
47 | MII.reset(p: TheTarget->createMCInstrInfo()); |
48 | Printer.reset(p: TheTarget->createMCInstPrinter( |
49 | T: Triple(TripleName), SyntaxVariant: MAI->getAssemblerDialect(), MAI: *MAI, MII: *MII, MRI: *MRI)); |
50 | } |
51 | |
52 | template <typename T> std::string formatHex(T i) { |
53 | std::string Buffer; |
54 | raw_string_ostream OS(Buffer); |
55 | OS << Printer->formatHex(i); |
56 | OS.flush(); |
57 | return Buffer; |
58 | } |
59 | }; |
60 | } // namespace |
61 | |
62 | TEST_F(MCInstPrinterTest, formatHex) { |
63 | if (!Printer) |
64 | GTEST_SKIP(); |
65 | |
66 | EXPECT_EQ("0x1" , formatHex<int64_t>(1)); |
67 | EXPECT_EQ("0x7fffffffffffffff" , |
68 | formatHex(std::numeric_limits<int64_t>::max())); |
69 | EXPECT_EQ("-0x8000000000000000" , |
70 | formatHex(std::numeric_limits<int64_t>::min())); |
71 | } |
72 | |