MCInstPrinter.cpp 2.0 KB

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