1bdd1243dSDimitry Andric //===-- XtensaMCAsmBackend.cpp - Xtensa assembler backend -----------------===//
2bdd1243dSDimitry Andric //
3bdd1243dSDimitry Andric //                     The LLVM Compiler Infrastructure
4bdd1243dSDimitry Andric //
5bdd1243dSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
6bdd1243dSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
7bdd1243dSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8bdd1243dSDimitry Andric //
9bdd1243dSDimitry Andric //===----------------------------------------------------------------------===//
10bdd1243dSDimitry Andric 
11bdd1243dSDimitry Andric #include "MCTargetDesc/XtensaFixupKinds.h"
12bdd1243dSDimitry Andric #include "MCTargetDesc/XtensaMCTargetDesc.h"
13bdd1243dSDimitry Andric #include "llvm/MC/MCAsmBackend.h"
14bdd1243dSDimitry Andric #include "llvm/MC/MCAssembler.h"
15bdd1243dSDimitry Andric #include "llvm/MC/MCContext.h"
16bdd1243dSDimitry Andric #include "llvm/MC/MCELFObjectWriter.h"
17bdd1243dSDimitry Andric #include "llvm/MC/MCFixupKindInfo.h"
18bdd1243dSDimitry Andric #include "llvm/MC/MCInst.h"
19bdd1243dSDimitry Andric #include "llvm/MC/MCObjectWriter.h"
20bdd1243dSDimitry Andric #include "llvm/MC/MCSubtargetInfo.h"
21bdd1243dSDimitry Andric #include "llvm/Support/raw_ostream.h"
22bdd1243dSDimitry Andric 
23bdd1243dSDimitry Andric using namespace llvm;
24bdd1243dSDimitry Andric 
25bdd1243dSDimitry Andric namespace llvm {
26bdd1243dSDimitry Andric class MCObjectTargetWriter;
27bdd1243dSDimitry Andric class XtensaMCAsmBackend : public MCAsmBackend {
28bdd1243dSDimitry Andric   uint8_t OSABI;
29bdd1243dSDimitry Andric   bool IsLittleEndian;
30bdd1243dSDimitry Andric 
31bdd1243dSDimitry Andric public:
XtensaMCAsmBackend(uint8_t osABI,bool isLE)32bdd1243dSDimitry Andric   XtensaMCAsmBackend(uint8_t osABI, bool isLE)
33*5f757f3fSDimitry Andric       : MCAsmBackend(llvm::endianness::little), OSABI(osABI),
34*5f757f3fSDimitry Andric         IsLittleEndian(isLE) {}
35bdd1243dSDimitry Andric 
getNumFixupKinds() const36bdd1243dSDimitry Andric   unsigned getNumFixupKinds() const override {
37bdd1243dSDimitry Andric     return Xtensa::NumTargetFixupKinds;
38bdd1243dSDimitry Andric   }
39bdd1243dSDimitry Andric   const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override;
40bdd1243dSDimitry Andric   void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
41bdd1243dSDimitry Andric                   const MCValue &Target, MutableArrayRef<char> Data,
42bdd1243dSDimitry Andric                   uint64_t Value, bool IsResolved,
43bdd1243dSDimitry Andric                   const MCSubtargetInfo *STI) const override;
44bdd1243dSDimitry Andric   bool mayNeedRelaxation(const MCInst &Inst,
45bdd1243dSDimitry Andric                          const MCSubtargetInfo &STI) const override;
46bdd1243dSDimitry Andric   bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
47bdd1243dSDimitry Andric                             const MCRelaxableFragment *Fragment,
48bdd1243dSDimitry Andric                             const MCAsmLayout &Layout) const override;
49bdd1243dSDimitry Andric   void relaxInstruction(MCInst &Inst,
50bdd1243dSDimitry Andric                         const MCSubtargetInfo &STI) const override;
51bdd1243dSDimitry Andric   bool writeNopData(raw_ostream &OS, uint64_t Count,
52bdd1243dSDimitry Andric                     const MCSubtargetInfo *STI) const override;
53bdd1243dSDimitry Andric 
createObjectTargetWriter() const54bdd1243dSDimitry Andric   std::unique_ptr<MCObjectTargetWriter> createObjectTargetWriter() const override {
55bdd1243dSDimitry Andric     return createXtensaObjectWriter(OSABI, IsLittleEndian);
56bdd1243dSDimitry Andric   }
57bdd1243dSDimitry Andric };
58bdd1243dSDimitry Andric } // namespace llvm
59bdd1243dSDimitry Andric 
60bdd1243dSDimitry Andric const MCFixupKindInfo &
getFixupKindInfo(MCFixupKind Kind) const61bdd1243dSDimitry Andric XtensaMCAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
62bdd1243dSDimitry Andric   const static MCFixupKindInfo Infos[Xtensa::NumTargetFixupKinds] = {
63bdd1243dSDimitry Andric       // name                     offset bits  flags
64bdd1243dSDimitry Andric       {"fixup_xtensa_branch_6", 0, 16, MCFixupKindInfo::FKF_IsPCRel},
65bdd1243dSDimitry Andric       {"fixup_xtensa_branch_8", 16, 8, MCFixupKindInfo::FKF_IsPCRel},
66bdd1243dSDimitry Andric       {"fixup_xtensa_branch_12", 12, 12, MCFixupKindInfo::FKF_IsPCRel},
67bdd1243dSDimitry Andric       {"fixup_xtensa_jump_18", 6, 18, MCFixupKindInfo::FKF_IsPCRel},
68bdd1243dSDimitry Andric       {"fixup_xtensa_call_18", 6, 18,
69bdd1243dSDimitry Andric        MCFixupKindInfo::FKF_IsPCRel |
70bdd1243dSDimitry Andric            MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
71bdd1243dSDimitry Andric       {"fixup_xtensa_l32r_16", 8, 16,
72bdd1243dSDimitry Andric        MCFixupKindInfo::FKF_IsPCRel |
73bdd1243dSDimitry Andric            MCFixupKindInfo::FKF_IsAlignedDownTo32Bits}};
74bdd1243dSDimitry Andric 
75bdd1243dSDimitry Andric   if (Kind < FirstTargetFixupKind)
76bdd1243dSDimitry Andric     return MCAsmBackend::getFixupKindInfo(Kind);
77bdd1243dSDimitry Andric   assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
78bdd1243dSDimitry Andric          "Invalid kind!");
79bdd1243dSDimitry Andric   return Infos[Kind - FirstTargetFixupKind];
80bdd1243dSDimitry Andric }
81bdd1243dSDimitry Andric 
adjustFixupValue(const MCFixup & Fixup,uint64_t Value,MCContext & Ctx)82bdd1243dSDimitry Andric static uint64_t adjustFixupValue(const MCFixup &Fixup, uint64_t Value,
83bdd1243dSDimitry Andric                                  MCContext &Ctx) {
84bdd1243dSDimitry Andric   unsigned Kind = Fixup.getKind();
85bdd1243dSDimitry Andric   switch (Kind) {
86bdd1243dSDimitry Andric   default:
87bdd1243dSDimitry Andric     llvm_unreachable("Unknown fixup kind!");
88bdd1243dSDimitry Andric   case FK_Data_1:
89bdd1243dSDimitry Andric   case FK_Data_2:
90bdd1243dSDimitry Andric   case FK_Data_4:
91bdd1243dSDimitry Andric   case FK_Data_8:
92bdd1243dSDimitry Andric     return Value;
93bdd1243dSDimitry Andric   case Xtensa::fixup_xtensa_branch_6: {
94bdd1243dSDimitry Andric     Value -= 4;
95bdd1243dSDimitry Andric     if (!isInt<6>(Value))
96bdd1243dSDimitry Andric       Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
97bdd1243dSDimitry Andric     unsigned Hi2 = (Value >> 4) & 0x3;
98bdd1243dSDimitry Andric     unsigned Lo4 = Value & 0xf;
99bdd1243dSDimitry Andric     return (Hi2 << 4) | (Lo4 << 12);
100bdd1243dSDimitry Andric   }
101bdd1243dSDimitry Andric   case Xtensa::fixup_xtensa_branch_8:
102bdd1243dSDimitry Andric     Value -= 4;
103bdd1243dSDimitry Andric     if (!isInt<8>(Value))
104bdd1243dSDimitry Andric       Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
105bdd1243dSDimitry Andric     return (Value & 0xff);
106bdd1243dSDimitry Andric   case Xtensa::fixup_xtensa_branch_12:
107bdd1243dSDimitry Andric     Value -= 4;
108bdd1243dSDimitry Andric     if (!isInt<12>(Value))
109bdd1243dSDimitry Andric       Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
110bdd1243dSDimitry Andric     return (Value & 0xfff);
111bdd1243dSDimitry Andric   case Xtensa::fixup_xtensa_jump_18:
112bdd1243dSDimitry Andric     Value -= 4;
113bdd1243dSDimitry Andric     if (!isInt<18>(Value))
114bdd1243dSDimitry Andric       Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
115bdd1243dSDimitry Andric     return (Value & 0x3ffff);
116bdd1243dSDimitry Andric   case Xtensa::fixup_xtensa_call_18:
117bdd1243dSDimitry Andric     Value -= 4;
118bdd1243dSDimitry Andric     if (!isInt<20>(Value))
119bdd1243dSDimitry Andric       Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
120bdd1243dSDimitry Andric     if (Value & 0x3)
121bdd1243dSDimitry Andric       Ctx.reportError(Fixup.getLoc(), "fixup value must be 4-byte aligned");
122bdd1243dSDimitry Andric     return (Value & 0xffffc) >> 2;
123bdd1243dSDimitry Andric   case Xtensa::fixup_xtensa_l32r_16:
124bdd1243dSDimitry Andric     unsigned Offset = Fixup.getOffset();
125bdd1243dSDimitry Andric     if (Offset & 0x3)
126bdd1243dSDimitry Andric       Value -= 4;
127bdd1243dSDimitry Andric     if (!isInt<18>(Value) && (Value & 0x20000))
128bdd1243dSDimitry Andric       Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
129bdd1243dSDimitry Andric     if (Value & 0x3)
130bdd1243dSDimitry Andric       Ctx.reportError(Fixup.getLoc(), "fixup value must be 4-byte aligned");
131bdd1243dSDimitry Andric     return (Value & 0x3fffc) >> 2;
132bdd1243dSDimitry Andric   }
133bdd1243dSDimitry Andric }
134bdd1243dSDimitry Andric 
getSize(unsigned Kind)135bdd1243dSDimitry Andric static unsigned getSize(unsigned Kind) {
136bdd1243dSDimitry Andric   switch (Kind) {
137bdd1243dSDimitry Andric   default:
138bdd1243dSDimitry Andric     return 3;
139bdd1243dSDimitry Andric   case MCFixupKind::FK_Data_4:
140bdd1243dSDimitry Andric     return 4;
141bdd1243dSDimitry Andric   case Xtensa::fixup_xtensa_branch_6:
142bdd1243dSDimitry Andric     return 2;
143bdd1243dSDimitry Andric   }
144bdd1243dSDimitry Andric }
145bdd1243dSDimitry Andric 
applyFixup(const MCAssembler & Asm,const MCFixup & Fixup,const MCValue & Target,MutableArrayRef<char> Data,uint64_t Value,bool IsResolved,const MCSubtargetInfo * STI) const146bdd1243dSDimitry Andric void XtensaMCAsmBackend::applyFixup(const MCAssembler &Asm,
147bdd1243dSDimitry Andric                                     const MCFixup &Fixup, const MCValue &Target,
148bdd1243dSDimitry Andric                                     MutableArrayRef<char> Data, uint64_t Value,
149bdd1243dSDimitry Andric                                     bool IsResolved,
150bdd1243dSDimitry Andric                                     const MCSubtargetInfo *STI) const {
151bdd1243dSDimitry Andric   MCContext &Ctx = Asm.getContext();
152bdd1243dSDimitry Andric   MCFixupKindInfo Info = getFixupKindInfo(Fixup.getKind());
153bdd1243dSDimitry Andric 
154bdd1243dSDimitry Andric   Value = adjustFixupValue(Fixup, Value, Ctx);
155bdd1243dSDimitry Andric 
156bdd1243dSDimitry Andric   // Shift the value into position.
157bdd1243dSDimitry Andric   Value <<= Info.TargetOffset;
158bdd1243dSDimitry Andric 
159bdd1243dSDimitry Andric   if (!Value)
160bdd1243dSDimitry Andric     return; // Doesn't change encoding.
161bdd1243dSDimitry Andric 
162bdd1243dSDimitry Andric   unsigned Offset = Fixup.getOffset();
163bdd1243dSDimitry Andric   unsigned FullSize = getSize(Fixup.getKind());
164bdd1243dSDimitry Andric 
165bdd1243dSDimitry Andric   for (unsigned i = 0; i != FullSize; ++i) {
166bdd1243dSDimitry Andric     Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
167bdd1243dSDimitry Andric   }
168bdd1243dSDimitry Andric }
169bdd1243dSDimitry Andric 
mayNeedRelaxation(const MCInst & Inst,const MCSubtargetInfo & STI) const170bdd1243dSDimitry Andric bool XtensaMCAsmBackend::mayNeedRelaxation(const MCInst &Inst,
171bdd1243dSDimitry Andric                                            const MCSubtargetInfo &STI) const {
172bdd1243dSDimitry Andric   return false;
173bdd1243dSDimitry Andric }
174bdd1243dSDimitry Andric 
fixupNeedsRelaxation(const MCFixup & Fixup,uint64_t Value,const MCRelaxableFragment * Fragment,const MCAsmLayout & Layout) const175bdd1243dSDimitry Andric bool XtensaMCAsmBackend::fixupNeedsRelaxation(
176bdd1243dSDimitry Andric     const MCFixup &Fixup, uint64_t Value, const MCRelaxableFragment *Fragment,
177bdd1243dSDimitry Andric     const MCAsmLayout &Layout) const {
178bdd1243dSDimitry Andric   return false;
179bdd1243dSDimitry Andric }
180bdd1243dSDimitry Andric 
relaxInstruction(MCInst & Inst,const MCSubtargetInfo & STI) const181bdd1243dSDimitry Andric void XtensaMCAsmBackend::relaxInstruction(MCInst &Inst,
182bdd1243dSDimitry Andric                                           const MCSubtargetInfo &STI) const {}
183bdd1243dSDimitry Andric 
writeNopData(raw_ostream & OS,uint64_t Count,const MCSubtargetInfo * STI) const184bdd1243dSDimitry Andric bool XtensaMCAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
185bdd1243dSDimitry Andric                                       const MCSubtargetInfo *STI) const {
186bdd1243dSDimitry Andric   uint64_t NumNops24b = Count / 3;
187bdd1243dSDimitry Andric 
188bdd1243dSDimitry Andric   for (uint64_t i = 0; i != NumNops24b; ++i) {
189bdd1243dSDimitry Andric     // Currently just little-endian machine supported,
190bdd1243dSDimitry Andric     // but probably big-endian will be also implemented in future
191bdd1243dSDimitry Andric     if (IsLittleEndian) {
192bdd1243dSDimitry Andric       OS.write("\xf0", 1);
193bdd1243dSDimitry Andric       OS.write("\x20", 1);
194bdd1243dSDimitry Andric       OS.write("\0x00", 1);
195bdd1243dSDimitry Andric     } else {
196bdd1243dSDimitry Andric       report_fatal_error("Big-endian mode currently is not supported!");
197bdd1243dSDimitry Andric     }
198bdd1243dSDimitry Andric     Count -= 3;
199bdd1243dSDimitry Andric   }
200bdd1243dSDimitry Andric 
201bdd1243dSDimitry Andric   // TODO maybe function should return error if (Count > 0)
202bdd1243dSDimitry Andric   switch (Count) {
203bdd1243dSDimitry Andric   default:
204bdd1243dSDimitry Andric     break;
205bdd1243dSDimitry Andric   case 1:
206bdd1243dSDimitry Andric     OS.write("\0", 1);
207bdd1243dSDimitry Andric     break;
208bdd1243dSDimitry Andric   case 2:
209bdd1243dSDimitry Andric     // NOP.N instruction
210bdd1243dSDimitry Andric     OS.write("\x3d", 1);
211bdd1243dSDimitry Andric     OS.write("\xf0", 1);
212bdd1243dSDimitry Andric     break;
213bdd1243dSDimitry Andric   }
214bdd1243dSDimitry Andric 
215bdd1243dSDimitry Andric   return true;
216bdd1243dSDimitry Andric }
217bdd1243dSDimitry Andric 
createXtensaMCAsmBackend(const Target & T,const MCSubtargetInfo & STI,const MCRegisterInfo & MRI,const MCTargetOptions & Options)218bdd1243dSDimitry Andric MCAsmBackend *llvm::createXtensaMCAsmBackend(const Target &T,
219bdd1243dSDimitry Andric                                              const MCSubtargetInfo &STI,
220bdd1243dSDimitry Andric                                              const MCRegisterInfo &MRI,
221bdd1243dSDimitry Andric                                              const MCTargetOptions &Options) {
222bdd1243dSDimitry Andric   uint8_t OSABI =
223bdd1243dSDimitry Andric       MCELFObjectTargetWriter::getOSABI(STI.getTargetTriple().getOS());
224bdd1243dSDimitry Andric   return new llvm::XtensaMCAsmBackend(OSABI, true);
225bdd1243dSDimitry Andric }
226