LLVM  14.0.0git
MCAsmBackend.cpp
Go to the documentation of this file.
1 //===- MCAsmBackend.cpp - Target MC Assembly Backend ----------------------===//
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/MCAsmBackend.h"
10 #include "llvm/ADT/None.h"
11 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/MC/MCObjectWriter.h"
19 #include <cassert>
20 #include <cstddef>
21 #include <cstdint>
22 
23 using namespace llvm;
24 
26 
27 MCAsmBackend::~MCAsmBackend() = default;
28 
29 std::unique_ptr<MCObjectWriter>
31  auto TW = createObjectTargetWriter();
32  switch (TW->getFormat()) {
33  case Triple::ELF:
34  return createELFObjectWriter(cast<MCELFObjectTargetWriter>(std::move(TW)), OS,
36  case Triple::MachO:
37  return createMachObjectWriter(cast<MCMachObjectTargetWriter>(std::move(TW)),
38  OS, Endian == support::little);
39  case Triple::COFF:
41  cast<MCWinCOFFObjectTargetWriter>(std::move(TW)), OS);
42  case Triple::Wasm:
43  return createWasmObjectWriter(cast<MCWasmObjectTargetWriter>(std::move(TW)),
44  OS);
45  case Triple::XCOFF:
47  cast<MCXCOFFObjectTargetWriter>(std::move(TW)), OS);
48  default:
49  llvm_unreachable("unexpected object format");
50  }
51 }
52 
53 std::unique_ptr<MCObjectWriter>
55  raw_pwrite_stream &DwoOS) const {
56  auto TW = createObjectTargetWriter();
57  switch (TW->getFormat()) {
58  case Triple::ELF:
60  cast<MCELFObjectTargetWriter>(std::move(TW)), OS, DwoOS,
62  case Triple::Wasm:
64  cast<MCWasmObjectTargetWriter>(std::move(TW)), OS, DwoOS);
65  default:
66  report_fatal_error("dwo only supported with ELF and Wasm");
67  }
68 }
69 
71  return None;
72 }
73 
75  static const MCFixupKindInfo Builtins[] = {
76  {"FK_NONE", 0, 0, 0},
77  {"FK_Data_1", 0, 8, 0},
78  {"FK_Data_2", 0, 16, 0},
79  {"FK_Data_4", 0, 32, 0},
80  {"FK_Data_8", 0, 64, 0},
81  {"FK_Data_6b", 0, 6, 0},
82  {"FK_PCRel_1", 0, 8, MCFixupKindInfo::FKF_IsPCRel},
83  {"FK_PCRel_2", 0, 16, MCFixupKindInfo::FKF_IsPCRel},
84  {"FK_PCRel_4", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
85  {"FK_PCRel_8", 0, 64, MCFixupKindInfo::FKF_IsPCRel},
86  {"FK_GPRel_1", 0, 8, 0},
87  {"FK_GPRel_2", 0, 16, 0},
88  {"FK_GPRel_4", 0, 32, 0},
89  {"FK_GPRel_8", 0, 64, 0},
90  {"FK_DTPRel_4", 0, 32, 0},
91  {"FK_DTPRel_8", 0, 64, 0},
92  {"FK_TPRel_4", 0, 32, 0},
93  {"FK_TPRel_8", 0, 64, 0},
94  {"FK_SecRel_1", 0, 8, 0},
95  {"FK_SecRel_2", 0, 16, 0},
96  {"FK_SecRel_4", 0, 32, 0},
97  {"FK_SecRel_8", 0, 64, 0},
98  };
99 
100  assert((size_t)Kind <= array_lengthof(Builtins) && "Unknown fixup kind");
101  return Builtins[Kind];
102 }
103 
105  const MCFixup &Fixup, bool Resolved, uint64_t Value,
106  const MCRelaxableFragment *DF, const MCAsmLayout &Layout,
107  const bool WasForced) const {
108  if (!Resolved)
109  return true;
110  return fixupNeedsRelaxation(Fixup, Value, DF, Layout);
111 }
llvm::Triple::Wasm
@ Wasm
Definition: Triple.h:241
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::MCRelaxableFragment
A relaxable fragment holds on to its MCInst, since it may need to be relaxed during the assembler lay...
Definition: MCFragment.h:271
llvm::createELFObjectWriter
std::unique_ptr< MCObjectWriter > createELFObjectWriter(std::unique_ptr< MCELFObjectTargetWriter > MOTW, raw_pwrite_stream &OS, bool IsLittleEndian)
Construct a new ELF writer instance.
Definition: ELFObjectWriter.cpp:1512
llvm::MCAsmBackend::getFixupKindInfo
virtual const MCFixupKindInfo & getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
Definition: MCAsmBackend.cpp:74
llvm::createWasmObjectWriter
std::unique_ptr< MCObjectWriter > createWasmObjectWriter(std::unique_ptr< MCWasmObjectTargetWriter > MOTW, raw_pwrite_stream &OS)
Construct a new Wasm writer instance.
Definition: WasmObjectWriter.cpp:1920
MCWasmObjectWriter.h
llvm::MCAsmBackend::~MCAsmBackend
virtual ~MCAsmBackend()
llvm::Optional
Definition: APInt.h:33
MCFixupKindInfo.h
llvm::Triple::XCOFF
@ XCOFF
Definition: Triple.h:242
STLExtras.h
llvm::MCAsmBackend::createDwoObjectWriter
std::unique_ptr< MCObjectWriter > createDwoObjectWriter(raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS) const
Create an MCObjectWriter that writes two object files: a .o file which is linked into the final progr...
Definition: MCAsmBackend.cpp:54
llvm::createWinCOFFObjectWriter
std::unique_ptr< MCObjectWriter > createWinCOFFObjectWriter(std::unique_ptr< MCWinCOFFObjectTargetWriter > MOTW, raw_pwrite_stream &OS)
Construct a new Win COFF writer instance.
Definition: WinCOFFObjectWriter.cpp:1178
llvm::MCAsmBackend::Endian
const support::endianness Endian
Definition: MCAsmBackend.h:45
llvm::createXCOFFObjectWriter
std::unique_ptr< MCObjectWriter > createXCOFFObjectWriter(std::unique_ptr< MCXCOFFObjectTargetWriter > MOTW, raw_pwrite_stream &OS)
Definition: XCOFFObjectWriter.cpp:1201
MCAsmBackend.h
llvm::support::little
@ little
Definition: Endian.h:27
llvm::report_fatal_error
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:140
MCMachObjectWriter.h
llvm::array_lengthof
constexpr size_t array_lengthof(T(&)[N])
Find the length of an array.
Definition: STLExtras.h:1394
llvm::None
const NoneType None
Definition: None.h:23
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
DF
static RegisterPass< DebugifyFunctionPass > DF("debugify-function", "Attach debug info to a function")
llvm::createMachObjectWriter
std::unique_ptr< MCObjectWriter > createMachObjectWriter(std::unique_ptr< MCMachObjectTargetWriter > MOTW, raw_pwrite_stream &OS, bool IsLittleEndian)
Construct a new Mach-O writer instance.
Definition: MachObjectWriter.cpp:1048
llvm::Triple::ELF
@ ELF
Definition: Triple.h:238
uint64_t
llvm::raw_pwrite_stream
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:417
MCELFObjectWriter.h
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::MCFixupKindInfo::FKF_IsPCRel
@ FKF_IsPCRel
Is this fixup kind PCrelative? This is used by the assembler backend to evaluate fixup values in a ta...
Definition: MCFixupKindInfo.h:19
MCXCOFFObjectWriter.h
llvm::MCAsmBackend::getFixupKind
virtual Optional< MCFixupKind > getFixupKind(StringRef Name) const
Map a relocation name used in .reloc to a fixup kind.
Definition: MCAsmBackend.cpp:70
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::MCFixupKindInfo
Target independent information on a fixup kind.
Definition: MCFixupKindInfo.h:15
llvm::MCAsmBackend::fixupNeedsRelaxationAdvanced
virtual bool fixupNeedsRelaxationAdvanced(const MCFixup &Fixup, bool Resolved, uint64_t Value, const MCRelaxableFragment *DF, const MCAsmLayout &Layout, const bool WasForced) const
Target specific predicate for whether a given fixup requires the associated instruction to be relaxed...
Definition: MCAsmBackend.cpp:104
None.h
Fixup
PowerPC TLS Dynamic Call Fixup
Definition: PPCTLSDynamicCall.cpp:235
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::MCAsmBackend::createObjectTargetWriter
virtual std::unique_ptr< MCObjectTargetWriter > createObjectTargetWriter() const =0
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:134
MCObjectWriter.h
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
llvm::MCAsmLayout
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:28
llvm::MCAsmBackend::fixupNeedsRelaxation
virtual bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value, const MCRelaxableFragment *DF, const MCAsmLayout &Layout) const =0
Simple predicate for targets where !Resolved implies requiring relaxation.
llvm::Triple::MachO
@ MachO
Definition: Triple.h:240
llvm::createELFDwoObjectWriter
std::unique_ptr< MCObjectWriter > createELFDwoObjectWriter(std::unique_ptr< MCELFObjectTargetWriter > MOTW, raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS, bool IsLittleEndian)
Definition: ELFObjectWriter.cpp:1519
llvm::MCFixupKind
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:21
llvm::Triple::COFF
@ COFF
Definition: Triple.h:237
llvm::MCAsmBackend::createObjectWriter
std::unique_ptr< MCObjectWriter > createObjectWriter(raw_pwrite_stream &OS) const
Create a new MCObjectWriter instance for use by the assembler backend to emit the final object file.
Definition: MCAsmBackend.cpp:30
llvm::createWasmDwoObjectWriter
std::unique_ptr< MCObjectWriter > createWasmDwoObjectWriter(std::unique_ptr< MCWasmObjectTargetWriter > MOTW, raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS)
Definition: WasmObjectWriter.cpp:1926
llvm::MCAsmBackend::MCAsmBackend
MCAsmBackend(support::endianness Endian)
Definition: MCAsmBackend.cpp:25
llvm::support::endianness
endianness
Definition: Endian.h:27
MCWinCOFFObjectWriter.h
llvm::MCFixup
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
llvm::Value
LLVM Value Representation.
Definition: Value.h:75