xref: /freebsd/contrib/llvm-project/llvm/lib/Target/VE/MCTargetDesc/VEAsmBackend.cpp (revision 700637cbb5e582861067a11aaca4d053546871d2)
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 
9 #include "MCTargetDesc/VEFixupKinds.h"
10 #include "MCTargetDesc/VEMCTargetDesc.h"
11 #include "llvm/MC/MCAsmBackend.h"
12 #include "llvm/MC/MCELFObjectWriter.h"
13 #include "llvm/MC/MCExpr.h"
14 #include "llvm/MC/MCObjectWriter.h"
15 #include "llvm/MC/MCSubtargetInfo.h"
16 #include "llvm/MC/MCValue.h"
17 #include "llvm/MC/TargetRegistry.h"
18 #include "llvm/Support/EndianStream.h"
19 
20 using namespace llvm;
21 
adjustFixupValue(unsigned Kind,uint64_t Value)22 static 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;
31   case VE::fixup_ve_hi32:
32   case VE::fixup_ve_pc_hi32:
33   case VE::fixup_ve_got_hi32:
34   case VE::fixup_ve_gotoff_hi32:
35   case VE::fixup_ve_plt_hi32:
36   case VE::fixup_ve_tls_gd_hi32:
37   case VE::fixup_ve_tpoff_hi32:
38     return (Value >> 32) & 0xffffffff;
39   case VE::fixup_ve_reflong:
40   case VE::fixup_ve_srel32:
41   case VE::fixup_ve_lo32:
42   case VE::fixup_ve_pc_lo32:
43   case VE::fixup_ve_got_lo32:
44   case VE::fixup_ve_gotoff_lo32:
45   case VE::fixup_ve_plt_lo32:
46   case VE::fixup_ve_tls_gd_lo32:
47   case VE::fixup_ve_tpoff_lo32:
48     return Value & 0xffffffff;
49   }
50 }
51 
52 /// getFixupKindNumBytes - The number of bytes the fixup may change.
getFixupKindNumBytes(unsigned Kind)53 static 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:
62   case VE::fixup_ve_reflong:
63   case VE::fixup_ve_srel32:
64   case VE::fixup_ve_hi32:
65   case VE::fixup_ve_lo32:
66   case VE::fixup_ve_pc_hi32:
67   case VE::fixup_ve_pc_lo32:
68   case VE::fixup_ve_got_hi32:
69   case VE::fixup_ve_got_lo32:
70   case VE::fixup_ve_gotoff_hi32:
71   case VE::fixup_ve_gotoff_lo32:
72   case VE::fixup_ve_plt_hi32:
73   case VE::fixup_ve_plt_lo32:
74   case VE::fixup_ve_tls_gd_hi32:
75   case VE::fixup_ve_tls_gd_lo32:
76   case VE::fixup_ve_tpoff_hi32:
77   case VE::fixup_ve_tpoff_lo32:
78     return 4;
79   case FK_Data_8:
80     return 8;
81   }
82 }
83 
84 namespace {
85 class VEAsmBackend : public MCAsmBackend {
86 protected:
87   const Target &TheTarget;
88 
89 public:
VEAsmBackend(const Target & T)90   VEAsmBackend(const Target &T)
91       : MCAsmBackend(llvm::endianness::little), TheTarget(T) {}
92 
getFixupKindInfo(MCFixupKind Kind) const93   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)
107       return MCAsmBackend::getFixupKindInfo(Kind);
108 
109     assert(unsigned(Kind - FirstTargetFixupKind) < VE::NumTargetFixupKinds &&
110            "Invalid kind!");
111     return Infos[Kind - FirstTargetFixupKind];
112   }
113 
114   void applyFixup(const MCFragment &, const MCFixup &, const MCValue &,
115                   MutableArrayRef<char>, uint64_t Value,
116                   bool IsResolved) override;
117 
mayNeedRelaxation(unsigned Opcode,ArrayRef<MCOperand> Operands,const MCSubtargetInfo & STI) const118   bool mayNeedRelaxation(unsigned Opcode, ArrayRef<MCOperand> Operands,
119                          const MCSubtargetInfo &STI) const override {
120     // Not implemented yet.  For example, if we have a branch with
121     // lager than SIMM32 immediate value, we want to relaxation such
122     // branch instructions.
123     return false;
124   }
125 
writeNopData(raw_ostream & OS,uint64_t Count,const MCSubtargetInfo * STI) const126   bool writeNopData(raw_ostream &OS, uint64_t Count,
127                     const MCSubtargetInfo *STI) const override {
128     if ((Count % 8) != 0)
129       return false;
130 
131     for (uint64_t i = 0; i < Count; i += 8)
132       support::endian::write<uint64_t>(OS, 0x7900000000000000ULL,
133                                        llvm::endianness::little);
134 
135     return true;
136   }
137 };
138 
139 class ELFVEAsmBackend : public VEAsmBackend {
140   Triple::OSType OSType;
141 
142 public:
ELFVEAsmBackend(const Target & T,Triple::OSType OSType)143   ELFVEAsmBackend(const Target &T, Triple::OSType OSType)
144       : VEAsmBackend(T), OSType(OSType) {}
145 
146   std::unique_ptr<MCObjectTargetWriter>
createObjectTargetWriter() const147   createObjectTargetWriter() const override {
148     uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(OSType);
149     return createVEELFObjectWriter(OSABI);
150   }
151 };
152 } // end anonymous namespace
153 
applyFixup(const MCFragment & F,const MCFixup & Fixup,const MCValue & Target,MutableArrayRef<char> Data,uint64_t Value,bool IsResolved)154 void VEAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
155                               const MCValue &Target, MutableArrayRef<char> Data,
156                               uint64_t Value, bool IsResolved) {
157   switch (Fixup.getKind()) {
158   case VE::fixup_ve_tls_gd_hi32:
159   case VE::fixup_ve_tls_gd_lo32:
160   case VE::fixup_ve_tpoff_hi32:
161   case VE::fixup_ve_tpoff_lo32:
162     IsResolved = false;
163     break;
164   }
165   maybeAddReloc(F, Fixup, Target, Value, IsResolved);
166   Value = adjustFixupValue(Fixup.getKind(), Value);
167   if (!Value)
168     return; // Doesn't change encoding.
169 
170   MCFixupKindInfo Info = getFixupKindInfo(Fixup.getKind());
171 
172   // Shift the value into position.
173   Value <<= Info.TargetOffset;
174 
175   unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
176   unsigned Offset = Fixup.getOffset();
177   assert(Offset + NumBytes <= Data.size() && "Invalid fixup offset!");
178   // For each byte of the fragment that the fixup touches, mask in the bits
179   // from the fixup value. The Value has been "split up" into the
180   // appropriate bitfields above.
181   for (unsigned i = 0; i != NumBytes; ++i) {
182     unsigned Idx = Endian == llvm::endianness::little ? i : (NumBytes - 1) - i;
183     Data[Offset + Idx] |= static_cast<uint8_t>((Value >> (i * 8)) & 0xff);
184   }
185 }
186 
createVEAsmBackend(const Target & T,const MCSubtargetInfo & STI,const MCRegisterInfo & MRI,const MCTargetOptions & Options)187 MCAsmBackend *llvm::createVEAsmBackend(const Target &T,
188                                        const MCSubtargetInfo &STI,
189                                        const MCRegisterInfo &MRI,
190                                        const MCTargetOptions &Options) {
191   return new ELFVEAsmBackend(T, STI.getTargetTriple().getOS());
192 }
193