LLVM  10.0.0svn
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"
12 #include "llvm/MC/MCCodePadder.h"
16 #include "llvm/MC/MCObjectWriter.h"
20 #include <cassert>
21 #include <cstddef>
22 #include <cstdint>
23 
24 using namespace llvm;
25 
27  : CodePadder(new MCCodePadder()), Endian(Endian) {}
28 
29 MCAsmBackend::~MCAsmBackend() = default;
30 
31 std::unique_ptr<MCObjectWriter>
33  auto TW = createObjectTargetWriter();
34  switch (TW->getFormat()) {
35  case Triple::ELF:
36  return createELFObjectWriter(cast<MCELFObjectTargetWriter>(std::move(TW)), OS,
38  case Triple::MachO:
39  return createMachObjectWriter(cast<MCMachObjectTargetWriter>(std::move(TW)),
40  OS, Endian == support::little);
41  case Triple::COFF:
43  cast<MCWinCOFFObjectTargetWriter>(std::move(TW)), OS);
44  case Triple::Wasm:
45  return createWasmObjectWriter(cast<MCWasmObjectTargetWriter>(std::move(TW)),
46  OS);
47  case Triple::XCOFF:
49  cast<MCXCOFFObjectTargetWriter>(std::move(TW)), OS);
50  default:
51  llvm_unreachable("unexpected object format");
52  }
53 }
54 
55 std::unique_ptr<MCObjectWriter>
57  raw_pwrite_stream &DwoOS) const {
58  auto TW = createObjectTargetWriter();
59  if (TW->getFormat() != Triple::ELF)
60  report_fatal_error("dwo only supported with ELF");
61  return createELFDwoObjectWriter(cast<MCELFObjectTargetWriter>(std::move(TW)),
62  OS, DwoOS, Endian == support::little);
63 }
64 
66  return None;
67 }
68 
70  static const MCFixupKindInfo Builtins[] = {
71  {"FK_NONE", 0, 0, 0},
72  {"FK_Data_1", 0, 8, 0},
73  {"FK_Data_2", 0, 16, 0},
74  {"FK_Data_4", 0, 32, 0},
75  {"FK_Data_8", 0, 64, 0},
76  {"FK_Data_6b", 0, 6, 0},
77  {"FK_PCRel_1", 0, 8, MCFixupKindInfo::FKF_IsPCRel},
78  {"FK_PCRel_2", 0, 16, MCFixupKindInfo::FKF_IsPCRel},
79  {"FK_PCRel_4", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
80  {"FK_PCRel_8", 0, 64, MCFixupKindInfo::FKF_IsPCRel},
81  {"FK_GPRel_1", 0, 8, 0},
82  {"FK_GPRel_2", 0, 16, 0},
83  {"FK_GPRel_4", 0, 32, 0},
84  {"FK_GPRel_8", 0, 64, 0},
85  {"FK_DTPRel_4", 0, 32, 0},
86  {"FK_DTPRel_8", 0, 64, 0},
87  {"FK_TPRel_4", 0, 32, 0},
88  {"FK_TPRel_8", 0, 64, 0},
89  {"FK_SecRel_1", 0, 8, 0},
90  {"FK_SecRel_2", 0, 16, 0},
91  {"FK_SecRel_4", 0, 32, 0},
92  {"FK_SecRel_8", 0, 64, 0},
93  {"FK_Data_Add_1", 0, 8, 0},
94  {"FK_Data_Add_2", 0, 16, 0},
95  {"FK_Data_Add_4", 0, 32, 0},
96  {"FK_Data_Add_8", 0, 64, 0},
97  {"FK_Data_Add_6b", 0, 6, 0},
98  {"FK_Data_Sub_1", 0, 8, 0},
99  {"FK_Data_Sub_2", 0, 16, 0},
100  {"FK_Data_Sub_4", 0, 32, 0},
101  {"FK_Data_Sub_8", 0, 64, 0},
102  {"FK_Data_Sub_6b", 0, 6, 0}};
103 
104  assert((size_t)Kind <= array_lengthof(Builtins) && "Unknown fixup kind");
105  return Builtins[Kind];
106 }
107 
109  const MCFixup &Fixup, bool Resolved, uint64_t Value,
110  const MCRelaxableFragment *DF, const MCAsmLayout &Layout,
111  const bool WasForced) const {
112  if (!Resolved)
113  return true;
114  return fixupNeedsRelaxation(Fixup, Value, DF, Layout);
115 }
116 
119  CodePadder->handleBasicBlockStart(OS, Context);
120 }
121 
123  const MCCodePaddingContext &Context) {
124  CodePadder->handleBasicBlockEnd(Context);
125 }
126 
128  CodePadder->handleInstructionBegin(Inst);
129 }
130 
132  CodePadder->handleInstructionEnd(Inst);
133 }
134 
136  return CodePadder->relaxFragment(PF, Layout);
137 }
Fragment for adding required padding.
Definition: MCFragment.h:342
void handleCodePaddingBasicBlockEnd(const MCCodePaddingContext &Context)
Handles all target related code padding after writing a block to an object file.
LLVMContext & Context
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:139
This class represents lattice values for constants.
Definition: AllocatorList.h:23
const support::endianness Endian
Definition: MCAsmBackend.h:52
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
virtual const MCFixupKindInfo & getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
virtual ~MCAsmBackend()
virtual std::unique_ptr< MCObjectTargetWriter > createObjectTargetWriter() const =0
void handleCodePaddingInstructionEnd(const MCInst &Inst)
Handles all target related code padding after writing an instruction to an object file...
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.
Target-independent base class incharge of all code padding decisions for a target.
Definition: MCCodePadder.h:38
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...
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:77
Is this fixup kind PCrelative? This is used by the assembler backend to evaluate fixup values in a ta...
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:28
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...
Streaming object file generation interface.
std::unique_ptr< MCObjectWriter > createELFObjectWriter(std::unique_ptr< MCELFObjectTargetWriter > MOTW, raw_pwrite_stream &OS, bool IsLittleEndian)
Construct a new ELF writer instance.
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:158
A relaxable fragment holds on to its MCInst, since it may need to be relaxed during the assembler lay...
Definition: MCFragment.h:272
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:22
std::unique_ptr< MCObjectWriter > createWasmObjectWriter(std::unique_ptr< MCWasmObjectTargetWriter > MOTW, raw_pwrite_stream &OS)
Construct a new Wasm writer instance.
void handleCodePaddingInstructionBegin(const MCInst &Inst)
Handles all target related code padding before writing a new instruction to an object file...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
std::unique_ptr< MCObjectWriter > createWinCOFFObjectWriter(std::unique_ptr< MCWinCOFFObjectTargetWriter > MOTW, raw_pwrite_stream &OS)
Construct a new Win COFF writer instance.
std::unique_ptr< MCObjectWriter > createXCOFFObjectWriter(std::unique_ptr< MCXCOFFObjectTargetWriter > MOTW, raw_pwrite_stream &OS)
PowerPC TLS Dynamic Call Fixup
void handleCodePaddingBasicBlockStart(MCObjectStreamer *OS, const MCCodePaddingContext &Context)
Handles all target related code padding when starting to write a new basic block to an object file...
std::unique_ptr< MCObjectWriter > createELFDwoObjectWriter(std::unique_ptr< MCELFObjectTargetWriter > MOTW, raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS, bool IsLittleEndian)
constexpr size_t array_lengthof(T(&)[N])
Find the length of an array.
Definition: STLExtras.h:1035
virtual Optional< MCFixupKind > getFixupKind(StringRef Name) const
Map a relocation name used in .reloc to a fixup kind.
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...
std::unique_ptr< MCObjectWriter > createMachObjectWriter(std::unique_ptr< MCMachObjectTargetWriter > MOTW, raw_pwrite_stream &OS, bool IsLittleEndian)
Construct a new Mach-O writer instance.
Target independent information on a fixup kind.
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:359
bool relaxFragment(MCPaddingFragment *PF, MCAsmLayout &Layout)
Relaxes a fragment (changes the size of the padding) according to target requirements.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
MCAsmBackend(support::endianness Endian)