[AMDGPU][AsmParser][NFC] Translate parsed MIMG instructions to MCInsts automatically.
[llvm-project.git] / llvm / lib / DWARFLinkerParallel / DWARFEmitterImpl.cpp
blob50909c0ba66937f643fc10aff1ef8a096a91346c
1 //===- DWARFEmitterImpl.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 //===----------------------------------------------------------------------===//
9 #include "DWARFEmitterImpl.h"
10 #include "llvm/DWARFLinker/DWARFLinkerCompileUnit.h"
11 #include "llvm/MC/MCAsmBackend.h"
12 #include "llvm/MC/MCCodeEmitter.h"
13 #include "llvm/MC/MCObjectWriter.h"
14 #include "llvm/MC/MCSubtargetInfo.h"
15 #include "llvm/MC/MCTargetOptions.h"
16 #include "llvm/MC/MCTargetOptionsCommandFlags.h"
17 #include "llvm/MC/TargetRegistry.h"
18 #include "llvm/Support/FormattedStream.h"
20 namespace llvm {
21 namespace dwarflinker_parallel {
23 Error DwarfEmitterImpl::init(Triple TheTriple,
24 StringRef Swift5ReflectionSegmentName) {
25 std::string ErrorStr;
26 std::string TripleName;
28 // Get the target.
29 const Target *TheTarget =
30 TargetRegistry::lookupTarget(TripleName, TheTriple, ErrorStr);
31 if (!TheTarget)
32 return createStringError(std::errc::invalid_argument, ErrorStr.c_str());
33 TripleName = TheTriple.getTriple();
35 // Create all the MC Objects.
36 MRI.reset(TheTarget->createMCRegInfo(TripleName));
37 if (!MRI)
38 return createStringError(std::errc::invalid_argument,
39 "no register info for target %s",
40 TripleName.c_str());
42 MCTargetOptions MCOptions = mc::InitMCTargetOptionsFromFlags();
43 MAI.reset(TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
44 if (!MAI)
45 return createStringError(std::errc::invalid_argument,
46 "no asm info for target %s", TripleName.c_str());
48 MSTI.reset(TheTarget->createMCSubtargetInfo(TripleName, "", ""));
49 if (!MSTI)
50 return createStringError(std::errc::invalid_argument,
51 "no subtarget info for target %s",
52 TripleName.c_str());
54 MC.reset(new MCContext(TheTriple, MAI.get(), MRI.get(), MSTI.get(), nullptr,
55 nullptr, true, Swift5ReflectionSegmentName));
56 MOFI.reset(TheTarget->createMCObjectFileInfo(*MC, /*PIC=*/false, false));
57 MC->setObjectFileInfo(MOFI.get());
59 MAB = TheTarget->createMCAsmBackend(*MSTI, *MRI, MCOptions);
60 if (!MAB)
61 return createStringError(std::errc::invalid_argument,
62 "no asm backend for target %s",
63 TripleName.c_str());
65 MII.reset(TheTarget->createMCInstrInfo());
66 if (!MII)
67 return createStringError(std::errc::invalid_argument,
68 "no instr info info for target %s",
69 TripleName.c_str());
71 MCE = TheTarget->createMCCodeEmitter(*MII, *MC);
72 if (!MCE)
73 return createStringError(std::errc::invalid_argument,
74 "no code emitter for target %s",
75 TripleName.c_str());
77 switch (OutFileType) {
78 case DWARFLinker::OutputFileType::Assembly: {
79 MIP = TheTarget->createMCInstPrinter(TheTriple, MAI->getAssemblerDialect(),
80 *MAI, *MII, *MRI);
81 MS = TheTarget->createAsmStreamer(
82 *MC, std::make_unique<formatted_raw_ostream>(OutFile), true, true, MIP,
83 std::unique_ptr<MCCodeEmitter>(MCE), std::unique_ptr<MCAsmBackend>(MAB),
84 true);
85 break;
87 case DWARFLinker::OutputFileType::Object: {
88 MS = TheTarget->createMCObjectStreamer(
89 TheTriple, *MC, std::unique_ptr<MCAsmBackend>(MAB),
90 MAB->createObjectWriter(OutFile), std::unique_ptr<MCCodeEmitter>(MCE),
91 *MSTI, MCOptions.MCRelaxAll, MCOptions.MCIncrementalLinkerCompatible,
92 /*DWARFMustBeAtTheEnd*/ false);
93 break;
97 if (!MS)
98 return createStringError(std::errc::invalid_argument,
99 "no object streamer for target %s",
100 TripleName.c_str());
102 // Finally create the AsmPrinter we'll use to emit the DIEs.
103 TM.reset(TheTarget->createTargetMachine(TripleName, "", "", TargetOptions(),
104 std::nullopt));
105 if (!TM)
106 return createStringError(std::errc::invalid_argument,
107 "no target machine for target %s",
108 TripleName.c_str());
110 Asm.reset(TheTarget->createAsmPrinter(*TM, std::unique_ptr<MCStreamer>(MS)));
111 if (!Asm)
112 return createStringError(std::errc::invalid_argument,
113 "no asm printer for target %s",
114 TripleName.c_str());
115 Asm->setDwarfUsesRelocationsAcrossSections(false);
117 RangesSectionSize = 0;
118 RngListsSectionSize = 0;
119 LocSectionSize = 0;
120 LocListsSectionSize = 0;
121 LineSectionSize = 0;
122 FrameSectionSize = 0;
123 DebugInfoSectionSize = 0;
124 MacInfoSectionSize = 0;
125 MacroSectionSize = 0;
127 return Error::success();
130 } // end of namespace dwarflinker_parallel
131 } // namespace llvm