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/MCFixupKindInfo.h"
15 #include "llvm/MC/MCObjectWriter.h"
16 #include "llvm/MC/MCSubtargetInfo.h"
17 #include "llvm/MC/MCValue.h"
18 #include "llvm/Support/EndianStream.h"
19 #include "llvm/Support/TargetRegistry.h"
20 
21 using namespace llvm;
22 
23 static uint64_t adjustFixupValue(unsigned Kind, uint64_t Value) {
24   switch (Kind) {
25   default:
26     llvm_unreachable("Unknown fixup kind!");
27   }
28 }
29 
30 namespace {
31 class VEAsmBackend : public MCAsmBackend {
32 protected:
33   const Target &TheTarget;
34 
35 public:
36   VEAsmBackend(const Target &T) : MCAsmBackend(support::little), TheTarget(T) {}
37 
38   unsigned getNumFixupKinds() const override { return VE::NumTargetFixupKinds; }
39 
40   const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override {
41     if (Kind < FirstTargetFixupKind)
42       return MCAsmBackend::getFixupKindInfo(Kind);
43 
44     // FIXME.
45     llvm_unreachable("getFixupKindInfo() unimplemented");
46   }
47 
48   bool shouldForceRelocation(const MCAssembler &Asm, const MCFixup &Fixup,
49                              const MCValue &Target) override {
50     // FIXME.
51     return false;
52   }
53 
54   bool mayNeedRelaxation(const MCInst &Inst,
55                          const MCSubtargetInfo &STI) const override {
56     // FIXME.
57     return false;
58   }
59 
60   /// fixupNeedsRelaxation - Target specific predicate for whether a given
61   /// fixup requires the associated instruction to be relaxed.
62   bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
63                             const MCRelaxableFragment *DF,
64                             const MCAsmLayout &Layout) const override {
65     // FIXME.
66     llvm_unreachable("fixupNeedsRelaxation() unimplemented");
67     return false;
68   }
69   void relaxInstruction(MCInst &Inst,
70                         const MCSubtargetInfo &STI) const override {
71     // FIXME.
72     llvm_unreachable("relaxInstruction() unimplemented");
73   }
74 
75   bool writeNopData(raw_ostream &OS, uint64_t Count) const override {
76     if ((Count % 8) != 0)
77       return false;
78 
79     for (uint64_t i = 0; i < Count; i += 8)
80       support::endian::write<uint64_t>(OS, 0x7900000000000000ULL,
81                                        support::little);
82 
83     return true;
84   }
85 };
86 
87 class ELFVEAsmBackend : public VEAsmBackend {
88   Triple::OSType OSType;
89 
90 public:
91   ELFVEAsmBackend(const Target &T, Triple::OSType OSType)
92       : VEAsmBackend(T), OSType(OSType) {}
93 
94   void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
95                   const MCValue &Target, MutableArrayRef<char> Data,
96                   uint64_t Value, bool IsResolved,
97                   const MCSubtargetInfo *STI) const override {
98     Value = adjustFixupValue(Fixup.getKind(), Value);
99     if (!Value)
100       return; // Doesn't change encoding.
101 
102     // FIXME.
103     llvm_unreachable("applyFixup() unimplemented");
104   }
105 
106   std::unique_ptr<MCObjectTargetWriter>
107   createObjectTargetWriter() const override {
108     uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(OSType);
109     return createVEELFObjectWriter(OSABI);
110   }
111 };
112 
113 } // end anonymous namespace
114 
115 MCAsmBackend *llvm::createVEAsmBackend(const Target &T,
116                                        const MCSubtargetInfo &STI,
117                                        const MCRegisterInfo &MRI,
118                                        const MCTargetOptions &Options) {
119   return new ELFVEAsmBackend(T, STI.getTargetTriple().getOS());
120 }
121