/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/Lanai/MCTargetDesc/LanaiAsmBackend.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===-- LanaiAsmBackend.cpp - Lanai 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 "LanaiFixupKinds.h" |
10 | | #include "MCTargetDesc/LanaiMCTargetDesc.h" |
11 | | #include "llvm/MC/MCAsmBackend.h" |
12 | | #include "llvm/MC/MCAssembler.h" |
13 | | #include "llvm/MC/MCDirectives.h" |
14 | | #include "llvm/MC/MCELFObjectWriter.h" |
15 | | #include "llvm/MC/MCFixupKindInfo.h" |
16 | | #include "llvm/MC/MCObjectWriter.h" |
17 | | #include "llvm/MC/MCSubtargetInfo.h" |
18 | | #include "llvm/Support/ErrorHandling.h" |
19 | | #include "llvm/Support/raw_ostream.h" |
20 | | |
21 | | using namespace llvm; |
22 | | |
23 | | // Prepare value for the target space |
24 | 0 | static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) { |
25 | 0 | switch (Kind) { |
26 | 0 | case FK_Data_1: |
27 | 0 | case FK_Data_2: |
28 | 0 | case FK_Data_4: |
29 | 0 | case FK_Data_8: |
30 | 0 | return Value; |
31 | 0 | case Lanai::FIXUP_LANAI_21: |
32 | 0 | case Lanai::FIXUP_LANAI_21_F: |
33 | 0 | case Lanai::FIXUP_LANAI_25: |
34 | 0 | case Lanai::FIXUP_LANAI_32: |
35 | 0 | case Lanai::FIXUP_LANAI_HI16: |
36 | 0 | case Lanai::FIXUP_LANAI_LO16: |
37 | 0 | return Value; |
38 | 0 | default: |
39 | 0 | llvm_unreachable("Unknown fixup kind!"); |
40 | 0 | } |
41 | 0 | } |
42 | | |
43 | | namespace { |
44 | | class LanaiAsmBackend : public MCAsmBackend { |
45 | | Triple::OSType OSType; |
46 | | |
47 | | public: |
48 | | LanaiAsmBackend(const Target &T, Triple::OSType OST) |
49 | 27 | : MCAsmBackend(support::big), OSType(OST) {} |
50 | | |
51 | | void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup, |
52 | | const MCValue &Target, MutableArrayRef<char> Data, |
53 | | uint64_t Value, bool IsResolved, |
54 | | const MCSubtargetInfo *STI) const override; |
55 | | |
56 | | std::unique_ptr<MCObjectTargetWriter> |
57 | | createObjectTargetWriter() const override; |
58 | | |
59 | | // No instruction requires relaxation |
60 | | bool fixupNeedsRelaxation(const MCFixup & /*Fixup*/, uint64_t /*Value*/, |
61 | | const MCRelaxableFragment * /*DF*/, |
62 | 0 | const MCAsmLayout & /*Layout*/) const override { |
63 | 0 | return false; |
64 | 0 | } |
65 | | |
66 | | const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override; |
67 | | |
68 | 0 | unsigned getNumFixupKinds() const override { |
69 | 0 | return Lanai::NumTargetFixupKinds; |
70 | 0 | } |
71 | | |
72 | | bool mayNeedRelaxation(const MCInst & /*Inst*/, |
73 | 0 | const MCSubtargetInfo &STI) const override { |
74 | 0 | return false; |
75 | 0 | } |
76 | | |
77 | | void relaxInstruction(const MCInst & /*Inst*/, |
78 | | const MCSubtargetInfo & /*STI*/, |
79 | 0 | MCInst & /*Res*/) const override {} |
80 | | |
81 | | bool writeNopData(raw_ostream &OS, uint64_t Count) const override; |
82 | | }; |
83 | | |
84 | 1 | bool LanaiAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count) const { |
85 | 1 | if ((Count % 4) != 0) |
86 | 0 | return false; |
87 | 1 | |
88 | 1 | for (uint64_t i = 0; i < Count; i += 40 ) |
89 | 0 | OS.write("\x15\0\0\0", 4); |
90 | 1 | |
91 | 1 | return true; |
92 | 1 | } |
93 | | |
94 | | void LanaiAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup, |
95 | | const MCValue &Target, |
96 | | MutableArrayRef<char> Data, uint64_t Value, |
97 | | bool /*IsResolved*/, |
98 | 0 | const MCSubtargetInfo * /*STI*/) const { |
99 | 0 | MCFixupKind Kind = Fixup.getKind(); |
100 | 0 | Value = adjustFixupValue(static_cast<unsigned>(Kind), Value); |
101 | 0 |
|
102 | 0 | if (!Value) |
103 | 0 | return; // This value doesn't change the encoding |
104 | 0 | |
105 | 0 | // Where in the object and where the number of bytes that need |
106 | 0 | // fixing up |
107 | 0 | unsigned Offset = Fixup.getOffset(); |
108 | 0 | unsigned NumBytes = (getFixupKindInfo(Kind).TargetSize + 7) / 8; |
109 | 0 | unsigned FullSize = 4; |
110 | 0 |
|
111 | 0 | // Grab current value, if any, from bits. |
112 | 0 | uint64_t CurVal = 0; |
113 | 0 |
|
114 | 0 | // Load instruction and apply value |
115 | 0 | for (unsigned i = 0; i != NumBytes; ++i) { |
116 | 0 | unsigned Idx = (FullSize - 1 - i); |
117 | 0 | CurVal |= static_cast<uint64_t>(static_cast<uint8_t>(Data[Offset + Idx])) |
118 | 0 | << (i * 8); |
119 | 0 | } |
120 | 0 |
|
121 | 0 | uint64_t Mask = |
122 | 0 | (static_cast<uint64_t>(-1) >> (64 - getFixupKindInfo(Kind).TargetSize)); |
123 | 0 | CurVal |= Value & Mask; |
124 | 0 |
|
125 | 0 | // Write out the fixed up bytes back to the code/data bits. |
126 | 0 | for (unsigned i = 0; i != NumBytes; ++i) { |
127 | 0 | unsigned Idx = (FullSize - 1 - i); |
128 | 0 | Data[Offset + Idx] = static_cast<uint8_t>((CurVal >> (i * 8)) & 0xff); |
129 | 0 | } |
130 | 0 | } |
131 | | |
132 | | std::unique_ptr<MCObjectTargetWriter> |
133 | 27 | LanaiAsmBackend::createObjectTargetWriter() const { |
134 | 27 | return createLanaiELFObjectWriter(MCELFObjectTargetWriter::getOSABI(OSType)); |
135 | 27 | } |
136 | | |
137 | | const MCFixupKindInfo & |
138 | 14 | LanaiAsmBackend::getFixupKindInfo(MCFixupKind Kind) const { |
139 | 14 | static const MCFixupKindInfo Infos[Lanai::NumTargetFixupKinds] = { |
140 | 14 | // This table *must* be in same the order of fixup_* kinds in |
141 | 14 | // LanaiFixupKinds.h. |
142 | 14 | // Note: The number of bits indicated here are assumed to be contiguous. |
143 | 14 | // This does not hold true for LANAI_21 and LANAI_21_F which are applied |
144 | 14 | // to bits 0x7cffff and 0x7cfffc, respectively. Since the 'bits' counts |
145 | 14 | // here are used only for cosmetic purposes, we set the size to 16 bits |
146 | 14 | // for these 21-bit relocation as llvm/lib/MC/MCAsmStreamer.cpp checks |
147 | 14 | // no bits are set in the fixup range. |
148 | 14 | // |
149 | 14 | // name offset bits flags |
150 | 14 | {"FIXUP_LANAI_NONE", 0, 32, 0}, |
151 | 14 | {"FIXUP_LANAI_21", 16, 16 /*21*/, 0}, |
152 | 14 | {"FIXUP_LANAI_21_F", 16, 16 /*21*/, 0}, |
153 | 14 | {"FIXUP_LANAI_25", 7, 25, 0}, |
154 | 14 | {"FIXUP_LANAI_32", 0, 32, 0}, |
155 | 14 | {"FIXUP_LANAI_HI16", 16, 16, 0}, |
156 | 14 | {"FIXUP_LANAI_LO16", 16, 16, 0}}; |
157 | 14 | |
158 | 14 | if (Kind < FirstTargetFixupKind) |
159 | 0 | return MCAsmBackend::getFixupKindInfo(Kind); |
160 | 14 | |
161 | 14 | assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() && |
162 | 14 | "Invalid kind!"); |
163 | 14 | return Infos[Kind - FirstTargetFixupKind]; |
164 | 14 | } |
165 | | |
166 | | } // namespace |
167 | | |
168 | | MCAsmBackend *llvm::createLanaiAsmBackend(const Target &T, |
169 | | const MCSubtargetInfo &STI, |
170 | | const MCRegisterInfo & /*MRI*/, |
171 | 27 | const MCTargetOptions & /*Options*/) { |
172 | 27 | const Triple &TT = STI.getTargetTriple(); |
173 | 27 | if (!TT.isOSBinFormatELF()) |
174 | 27 | llvm_unreachable0 ("OS not supported"); |
175 | 27 | |
176 | 27 | return new LanaiAsmBackend(T, TT.getOS()); |
177 | 27 | } |