LLVM 22.0.0git
VEAsmBackend.cpp
Go to the documentation of this file.
1//===-- VEAsmBackend.cpp - VE Assembler 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
13#include "llvm/MC/MCExpr.h"
16#include "llvm/MC/MCValue.h"
19
20using namespace llvm;
21
22static uint64_t adjustFixupValue(unsigned Kind, uint64_t Value) {
23 switch (Kind) {
24 default:
25 llvm_unreachable("Unknown fixup kind!");
26 case FK_Data_1:
27 case FK_Data_2:
28 case FK_Data_4:
29 case FK_Data_8:
30 return Value;
38 return (Value >> 32) & 0xffffffff;
48 return Value & 0xffffffff;
49 }
50}
51
52/// getFixupKindNumBytes - The number of bytes the fixup may change.
53static unsigned getFixupKindNumBytes(unsigned Kind) {
54 switch (Kind) {
55 default:
56 llvm_unreachable("Unknown fixup kind!");
57 case FK_Data_1:
58 return 1;
59 case FK_Data_2:
60 return 2;
61 case FK_Data_4:
78 return 4;
79 case FK_Data_8:
80 return 8;
81 }
82}
83
84namespace {
85class VEAsmBackend : public MCAsmBackend {
86protected:
87 const Target &TheTarget;
88
89public:
90 VEAsmBackend(const Target &T)
91 : MCAsmBackend(llvm::endianness::little), TheTarget(T) {}
92
93 MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const override {
94 const static MCFixupKindInfo Infos[VE::NumTargetFixupKinds] = {
95 // name, offset, bits, flags
96 {"fixup_ve_reflong", 0, 32, 0}, {"fixup_ve_srel32", 0, 32, 0},
97 {"fixup_ve_hi32", 0, 32, 0}, {"fixup_ve_lo32", 0, 32, 0},
98 {"fixup_ve_pc_hi32", 0, 32, 0}, {"fixup_ve_pc_lo32", 0, 32, 0},
99 {"fixup_ve_got_hi32", 0, 32, 0}, {"fixup_ve_got_lo32", 0, 32, 0},
100 {"fixup_ve_gotoff_hi32", 0, 32, 0}, {"fixup_ve_gotoff_lo32", 0, 32, 0},
101 {"fixup_ve_plt_hi32", 0, 32, 0}, {"fixup_ve_plt_lo32", 0, 32, 0},
102 {"fixup_ve_tls_gd_hi32", 0, 32, 0}, {"fixup_ve_tls_gd_lo32", 0, 32, 0},
103 {"fixup_ve_tpoff_hi32", 0, 32, 0}, {"fixup_ve_tpoff_lo32", 0, 32, 0},
104 };
105
106 if (Kind < FirstTargetFixupKind)
108
110 "Invalid kind!");
111 return Infos[Kind - FirstTargetFixupKind];
112 }
113
114 void applyFixup(const MCFragment &, const MCFixup &, const MCValue &,
115 uint8_t *, uint64_t Value, bool IsResolved) override;
116
117 bool mayNeedRelaxation(unsigned Opcode, ArrayRef<MCOperand> Operands,
118 const MCSubtargetInfo &STI) const override {
119 // Not implemented yet. For example, if we have a branch with
120 // lager than SIMM32 immediate value, we want to relaxation such
121 // branch instructions.
122 return false;
123 }
124
125 bool writeNopData(raw_ostream &OS, uint64_t Count,
126 const MCSubtargetInfo *STI) const override {
127 if ((Count % 8) != 0)
128 return false;
129
130 for (uint64_t i = 0; i < Count; i += 8)
131 support::endian::write<uint64_t>(OS, 0x7900000000000000ULL,
133
134 return true;
135 }
136};
137
138class ELFVEAsmBackend : public VEAsmBackend {
139 Triple::OSType OSType;
140
141public:
142 ELFVEAsmBackend(const Target &T, Triple::OSType OSType)
143 : VEAsmBackend(T), OSType(OSType) {}
144
145 std::unique_ptr<MCObjectTargetWriter>
146 createObjectTargetWriter() const override {
148 return createVEELFObjectWriter(OSABI);
149 }
150};
151} // end anonymous namespace
152
153void VEAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
154 const MCValue &Target, uint8_t *Data,
155 uint64_t Value, bool IsResolved) {
156 switch (Fixup.getKind()) {
161 IsResolved = false;
162 break;
163 }
164 maybeAddReloc(F, Fixup, Target, Value, IsResolved);
165 Value = adjustFixupValue(Fixup.getKind(), Value);
166 if (!Value)
167 return; // Doesn't change encoding.
168
169 MCFixupKindInfo Info = getFixupKindInfo(Fixup.getKind());
170
171 // Shift the value into position.
172 Value <<= Info.TargetOffset;
173
174 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
175 assert(Fixup.getOffset() + NumBytes <= F.getSize() &&
176 "Invalid fixup offset!");
177 // For each byte of the fragment that the fixup touches, mask in the bits
178 // from the fixup value. The Value has been "split up" into the
179 // appropriate bitfields above.
180 for (unsigned i = 0; i != NumBytes; ++i) {
181 unsigned Idx = Endian == llvm::endianness::little ? i : (NumBytes - 1) - i;
182 Data[Idx] |= static_cast<uint8_t>((Value >> (i * 8)) & 0xff);
183 }
184}
185
187 const MCSubtargetInfo &STI,
188 const MCRegisterInfo &MRI,
189 const MCTargetOptions &Options) {
190 return new ELFVEAsmBackend(T, STI.getTargetTriple().getOS());
191}
unsigned const MachineRegisterInfo * MRI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
static LVOptions Options
Definition: LVOptions.cpp:25
#define F(x, y, z)
Definition: MD5.cpp:55
mir Rename Register Operands
PowerPC TLS Dynamic Call Fixup
endianness Endian
raw_pwrite_stream & OS
static uint64_t adjustFixupValue(unsigned Kind, uint64_t Value)
static unsigned getFixupKindNumBytes(unsigned Kind)
getFixupKindNumBytes - The number of bytes the fixup may change.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:55
virtual bool writeNopData(raw_ostream &OS, uint64_t Count, const MCSubtargetInfo *STI) const =0
Write an (optimal) nop sequence of Count bytes to the given output.
virtual MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
virtual bool mayNeedRelaxation(unsigned Opcode, ArrayRef< MCOperand > Operands, const MCSubtargetInfo &STI) const
Check whether the given instruction (encoded as Opcode+Operands) may need relaxation.
Definition: MCAsmBackend.h:135
virtual void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target, uint8_t *Data, uint64_t Value, bool IsResolved)=0
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:61
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
Generic base class for all target subtargets.
const Triple & getTargetTriple() const
Target - Wrapper for Target specific information.
OSType getOS() const
Get the parsed operating system type of this triple.
Definition: Triple.h:417
LLVM Value Representation.
Definition: Value.h:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ fixup_ve_tpoff_hi32
Definition: VEFixupKinds.h:54
@ fixup_ve_srel32
fixup_ve_srel32 - 32-bit fixup corresponding to foo for relative branch
Definition: VEFixupKinds.h:21
@ fixup_ve_plt_lo32
Definition: VEFixupKinds.h:49
@ fixup_ve_got_hi32
fixup_ve_got_hi32 - 32-bit fixup corresponding to foo@got_hi
Definition: VEFixupKinds.h:36
@ fixup_ve_gotoff_hi32
fixup_ve_gotoff_hi32 - 32-bit fixup corresponding to foo@gotoff_hi
Definition: VEFixupKinds.h:42
@ fixup_ve_got_lo32
fixup_ve_got_lo32 - 32-bit fixup corresponding to foo@got_lo
Definition: VEFixupKinds.h:39
@ fixup_ve_pc_hi32
fixup_ve_pc_hi32 - 32-bit fixup corresponding to foo@pc_hi
Definition: VEFixupKinds.h:30
@ fixup_ve_lo32
fixup_ve_lo32 - 32-bit fixup corresponding to foo@lo
Definition: VEFixupKinds.h:27
@ fixup_ve_gotoff_lo32
fixup_ve_gotoff_lo32 - 32-bit fixup corresponding to foo@gotoff_lo
Definition: VEFixupKinds.h:45
@ NumTargetFixupKinds
Definition: VEFixupKinds.h:59
@ fixup_ve_hi32
fixup_ve_hi32 - 32-bit fixup corresponding to foo@hi
Definition: VEFixupKinds.h:24
@ fixup_ve_pc_lo32
fixup_ve_pc_lo32 - 32-bit fixup corresponding to foo@pc_lo
Definition: VEFixupKinds.h:33
@ fixup_ve_tpoff_lo32
Definition: VEFixupKinds.h:55
@ fixup_ve_plt_hi32
fixup_ve_plt_hi32/lo32
Definition: VEFixupKinds.h:48
@ fixup_ve_reflong
fixup_ve_reflong - 32-bit fixup corresponding to foo
Definition: VEFixupKinds.h:18
@ fixup_ve_tls_gd_lo32
Definition: VEFixupKinds.h:53
@ fixup_ve_tls_gd_hi32
fixups for Thread Local Storage
Definition: VEFixupKinds.h:52
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ FirstTargetFixupKind
Definition: MCFixup.h:44
@ FK_Data_8
A eight-byte fixup.
Definition: MCFixup.h:37
@ FK_Data_1
A one-byte fixup.
Definition: MCFixup.h:34
@ FK_Data_4
A four-byte fixup.
Definition: MCFixup.h:36
@ FK_Data_2
A two-byte fixup.
Definition: MCFixup.h:35
MCAsmBackend * createVEAsmBackend(const Target &T, const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options)
endianness
Definition: bit.h:71
std::unique_ptr< MCObjectTargetWriter > createVEELFObjectWriter(uint8_t OSABI)
Target independent information on a fixup kind.
Definition: MCAsmBackend.h:38