xref: /llvm-project/llvm/unittests/CodeGen/TestAsmPrinter.cpp (revision 62c7f035b4392c1933550eead6ddab35122720bc)
1380e746bSIgor Kudrin //===--- unittests/CodeGen/TestAsmPrinter.cpp -------------------*- C++ -*-===//
2380e746bSIgor Kudrin //
3380e746bSIgor Kudrin // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4380e746bSIgor Kudrin // See https://llvm.org/LICENSE.txt for license information.
5380e746bSIgor Kudrin // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6380e746bSIgor Kudrin //
7380e746bSIgor Kudrin //===----------------------------------------------------------------------===//
8380e746bSIgor Kudrin 
9380e746bSIgor Kudrin #include "TestAsmPrinter.h"
10380e746bSIgor Kudrin #include "llvm/CodeGen/AsmPrinter.h"
11380e746bSIgor Kudrin #include "llvm/MC/MCAsmInfo.h"
12380e746bSIgor Kudrin #include "llvm/MC/MCContext.h"
1389b57061SReid Kleckner #include "llvm/MC/TargetRegistry.h"
14380e746bSIgor Kudrin #include "llvm/Target/TargetLoweringObjectFile.h"
15380e746bSIgor Kudrin #include "llvm/Target/TargetMachine.h"
16*62c7f035SArchibald Elliott #include "llvm/TargetParser/Triple.h"
17380e746bSIgor Kudrin 
18380e746bSIgor Kudrin using namespace llvm;
19380e746bSIgor Kudrin using ::testing::StrictMock;
20380e746bSIgor Kudrin 
21380e746bSIgor Kudrin // Note: a non-const reference argument cannot be passed through
22380e746bSIgor Kudrin // testing::StrictMock, thus, we pass a pointer and dereference it here.
MockMCStreamer(MCContext * Ctx)23380e746bSIgor Kudrin MockMCStreamer::MockMCStreamer(MCContext *Ctx) : MCStreamer(*Ctx) {}
24380e746bSIgor Kudrin 
25380e746bSIgor Kudrin MockMCStreamer::~MockMCStreamer() = default;
26380e746bSIgor Kudrin 
27380e746bSIgor Kudrin TestAsmPrinter::TestAsmPrinter() = default;
28380e746bSIgor Kudrin 
29380e746bSIgor Kudrin TestAsmPrinter::~TestAsmPrinter() = default;
30380e746bSIgor Kudrin 
31380e746bSIgor Kudrin llvm::Expected<std::unique_ptr<TestAsmPrinter>>
create(const std::string & TripleStr,uint16_t DwarfVersion,dwarf::DwarfFormat DwarfFormat)32380e746bSIgor Kudrin TestAsmPrinter::create(const std::string &TripleStr, uint16_t DwarfVersion,
33380e746bSIgor Kudrin                        dwarf::DwarfFormat DwarfFormat) {
34380e746bSIgor Kudrin   std::string ErrorStr;
35380e746bSIgor Kudrin   const Target *TheTarget = TargetRegistry::lookupTarget(TripleStr, ErrorStr);
36380e746bSIgor Kudrin   if (!TheTarget)
37380e746bSIgor Kudrin     return std::unique_ptr<TestAsmPrinter>();
38380e746bSIgor Kudrin 
39380e746bSIgor Kudrin   std::unique_ptr<TestAsmPrinter> TestPrinter(new TestAsmPrinter);
40380e746bSIgor Kudrin   if (llvm::Error E =
41380e746bSIgor Kudrin           TestPrinter->init(TheTarget, TripleStr, DwarfVersion, DwarfFormat))
42380e746bSIgor Kudrin     return std::move(E);
43380e746bSIgor Kudrin 
44380e746bSIgor Kudrin   return std::move(TestPrinter);
45380e746bSIgor Kudrin }
46380e746bSIgor Kudrin 
47380e746bSIgor Kudrin // Note:: based on dwarfgen::Generator::init() from
48380e746bSIgor Kudrin // llvm/unittests/DebugInfo/DWARF/DwarfGenerator.cpp
init(const Target * TheTarget,StringRef TripleName,uint16_t DwarfVersion,dwarf::DwarfFormat DwarfFormat)49380e746bSIgor Kudrin llvm::Error TestAsmPrinter::init(const Target *TheTarget, StringRef TripleName,
50380e746bSIgor Kudrin                                  uint16_t DwarfVersion,
51380e746bSIgor Kudrin                                  dwarf::DwarfFormat DwarfFormat) {
52380e746bSIgor Kudrin   TM.reset(TheTarget->createTargetMachine(TripleName, "", "", TargetOptions(),
53b6a01caaSKazu Hirata                                           std::nullopt));
54380e746bSIgor Kudrin   if (!TM)
55380e746bSIgor Kudrin     return make_error<StringError>("no target machine for target " + TripleName,
56380e746bSIgor Kudrin                                    inconvertibleErrorCode());
57380e746bSIgor Kudrin 
58632ebc4aSPhilipp Krones   Triple TheTriple(TripleName);
59632ebc4aSPhilipp Krones   MC.reset(new MCContext(TheTriple, TM->getMCAsmInfo(), TM->getMCRegisterInfo(),
60c2f819afSPhilipp Krones                          TM->getMCSubtargetInfo()));
61380e746bSIgor Kudrin   TM->getObjFileLowering()->Initialize(*MC, *TM);
62c2f819afSPhilipp Krones   MC->setObjectFileInfo(TM->getObjFileLowering());
63380e746bSIgor Kudrin 
64380e746bSIgor Kudrin   MS = new StrictMock<MockMCStreamer>(MC.get());
65380e746bSIgor Kudrin 
66380e746bSIgor Kudrin   Asm.reset(
67380e746bSIgor Kudrin       TheTarget->createAsmPrinter(*TM, std::unique_ptr<MockMCStreamer>(MS)));
68380e746bSIgor Kudrin   if (!Asm)
69380e746bSIgor Kudrin     return make_error<StringError>("no asm printer for target " + TripleName,
70380e746bSIgor Kudrin                                    inconvertibleErrorCode());
71380e746bSIgor Kudrin 
72380e746bSIgor Kudrin   // Set the DWARF version correctly on all classes that we use.
73380e746bSIgor Kudrin   MC->setDwarfVersion(DwarfVersion);
74380e746bSIgor Kudrin   Asm->setDwarfVersion(DwarfVersion);
75380e746bSIgor Kudrin 
76380e746bSIgor Kudrin   // Set the DWARF format.
77380e746bSIgor Kudrin   MC->setDwarfFormat(DwarfFormat);
78380e746bSIgor Kudrin 
79380e746bSIgor Kudrin   return Error::success();
80380e746bSIgor Kudrin }
81380e746bSIgor Kudrin 
setDwarfUsesRelocationsAcrossSections(bool Enable)82380e746bSIgor Kudrin void TestAsmPrinter::setDwarfUsesRelocationsAcrossSections(bool Enable) {
832216ee49SAlexey Lapshin   Asm->setDwarfUsesRelocationsAcrossSections(Enable);
84380e746bSIgor Kudrin }
85