Coverage Report

Created: 2026-07-25 06:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/keystone/llvm/lib/Target/PowerPC/MCTargetDesc/PPCAsmBackend.cpp
Line
Count
Source
1
//===-- PPCAsmBackend.cpp - PPC Assembler Backend -------------------------===//
2
//
3
//                     The LLVM Compiler Infrastructure
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
10
#include "MCTargetDesc/PPCMCTargetDesc.h"
11
#include "MCTargetDesc/PPCFixupKinds.h"
12
#include "llvm/MC/MCAsmBackend.h"
13
#include "llvm/MC/MCAssembler.h"
14
#include "llvm/MC/MCELFObjectWriter.h"
15
#include "llvm/MC/MCFixupKindInfo.h"
16
#include "llvm/MC/MCObjectWriter.h"
17
#include "llvm/MC/MCSectionMachO.h"
18
#include "llvm/MC/MCSymbolELF.h"
19
#include "llvm/MC/MCValue.h"
20
#include "llvm/Support/ELF.h"
21
#include "llvm/Support/ErrorHandling.h"
22
#include "llvm/Support/MachO.h"
23
#include "llvm/Support/TargetRegistry.h"
24
using namespace llvm_ks;
25
26
29.5k
static uint64_t adjustFixupValue(unsigned Kind, uint64_t Value) {
27
29.5k
  switch (Kind) {
28
0
  default:
29
0
    llvm_unreachable("Unknown fixup kind!");
30
0
  case FK_Data_1:
31
23.3k
  case FK_Data_2:
32
25.3k
  case FK_Data_4:
33
26.1k
  case FK_Data_8:
34
26.1k
  case PPC::fixup_ppc_nofixup:
35
26.1k
    return Value;
36
1.33k
  case PPC::fixup_ppc_brcond14:
37
1.39k
  case PPC::fixup_ppc_brcond14abs:
38
1.39k
    return Value & 0xfffc;
39
1.63k
  case PPC::fixup_ppc_br24:
40
1.63k
  case PPC::fixup_ppc_br24abs:
41
1.63k
    return Value & 0x3fffffc;
42
311
  case PPC::fixup_ppc_half16:
43
311
    return Value & 0xffff;
44
121
  case PPC::fixup_ppc_half16ds:
45
121
    return Value & 0xfffc;
46
29.5k
  }
47
29.5k
}
48
49
24.6k
static unsigned getFixupKindNumBytes(unsigned Kind) {
50
24.6k
  switch (Kind) {
51
0
  default:
52
0
    llvm_unreachable("Unknown fixup kind!");
53
0
  case FK_Data_1:
54
0
    return 1;
55
21.5k
  case FK_Data_2:
56
21.8k
  case PPC::fixup_ppc_half16:
57
22.0k
  case PPC::fixup_ppc_half16ds:
58
22.0k
    return 2;
59
1.58k
  case FK_Data_4:
60
1.79k
  case PPC::fixup_ppc_brcond14:
61
1.84k
  case PPC::fixup_ppc_brcond14abs:
62
2.02k
  case PPC::fixup_ppc_br24:
63
2.03k
  case PPC::fixup_ppc_br24abs:
64
2.03k
    return 4;
65
618
  case FK_Data_8:
66
618
    return 8;
67
0
  case PPC::fixup_ppc_nofixup:
68
0
    return 0;
69
24.6k
  }
70
24.6k
}
71
72
namespace {
73
74
class PPCAsmBackend : public MCAsmBackend {
75
  const Target &TheTarget;
76
  bool IsLittleEndian;
77
public:
78
7.36k
  PPCAsmBackend(const Target &T, bool isLittle) : MCAsmBackend(), TheTarget(T),
79
7.36k
    IsLittleEndian(isLittle) {}
80
81
10.5k
  unsigned getNumFixupKinds() const override {
82
10.5k
    return PPC::NumTargetFixupKinds;
83
10.5k
  }
84
85
89.3k
  const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override {
86
89.3k
    const static MCFixupKindInfo InfosBE[PPC::NumTargetFixupKinds] = {
87
      // name                    offset  bits  flags
88
89.3k
      { "fixup_ppc_br24",        6,      24,   MCFixupKindInfo::FKF_IsPCRel },
89
89.3k
      { "fixup_ppc_brcond14",    16,     14,   MCFixupKindInfo::FKF_IsPCRel },
90
89.3k
      { "fixup_ppc_br24abs",     6,      24,   0 },
91
89.3k
      { "fixup_ppc_brcond14abs", 16,     14,   0 },
92
89.3k
      { "fixup_ppc_half16",       0,     16,   0 },
93
89.3k
      { "fixup_ppc_half16ds",     0,     14,   0 },
94
89.3k
      { "fixup_ppc_nofixup",      0,      0,   0 }
95
89.3k
    };
96
89.3k
    const static MCFixupKindInfo InfosLE[PPC::NumTargetFixupKinds] = {
97
      // name                    offset  bits  flags
98
89.3k
      { "fixup_ppc_br24",        2,      24,   MCFixupKindInfo::FKF_IsPCRel },
99
89.3k
      { "fixup_ppc_brcond14",    2,      14,   MCFixupKindInfo::FKF_IsPCRel },
100
89.3k
      { "fixup_ppc_br24abs",     2,      24,   0 },
101
89.3k
      { "fixup_ppc_brcond14abs", 2,      14,   0 },
102
89.3k
      { "fixup_ppc_half16",      0,      16,   0 },
103
89.3k
      { "fixup_ppc_half16ds",    2,      14,   0 },
104
89.3k
      { "fixup_ppc_nofixup",     0,       0,   0 }
105
89.3k
    };
106
107
89.3k
    if (Kind < FirstTargetFixupKind)
108
78.7k
      return MCAsmBackend::getFixupKindInfo(Kind);
109
110
89.3k
    assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
111
10.5k
           "Invalid kind!");
112
10.5k
    return (IsLittleEndian? InfosLE : InfosBE)[Kind - FirstTargetFixupKind];
113
10.5k
  }
114
115
  void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
116
29.5k
                  uint64_t Value, bool IsPCRel, unsigned int &KsError) const override {
117
29.5k
    Value = adjustFixupValue(Fixup.getKind(), Value);
118
29.5k
    if (!Value) return;           // Doesn't change encoding.
119
120
24.6k
    unsigned Offset = Fixup.getOffset();
121
24.6k
    unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
122
123
    // For each byte of the fragment that the fixup touches, mask in the bits
124
    // from the fixup value. The Value has been "split up" into the appropriate
125
    // bitfields above.
126
81.7k
    for (unsigned i = 0; i != NumBytes; ++i) {
127
57.1k
      unsigned Idx = IsLittleEndian ? i : (NumBytes - 1 - i);
128
57.1k
      Data[Offset + i] |= uint8_t((Value >> (Idx * 8)) & 0xff);
129
57.1k
    }
130
24.6k
  }
131
132
  void processFixupValue(const MCAssembler &Asm, const MCAsmLayout &Layout,
133
                         const MCFixup &Fixup, const MCFragment *DF,
134
                         const MCValue &Target, uint64_t &Value,
135
29.5k
                         bool &IsResolved) override {
136
29.5k
    switch ((PPC::Fixups)Fixup.getKind()) {
137
27.9k
    default: break;
138
27.9k
    case PPC::fixup_ppc_br24:
139
1.63k
    case PPC::fixup_ppc_br24abs:
140
      // If the target symbol has a local entry point we must not attempt
141
      // to resolve the fixup directly.  Emit a relocation and leave
142
      // resolution of the final target address to the linker.
143
1.63k
      if (const MCSymbolRefExpr *A = Target.getSymA()) {
144
1.46k
        if (const auto *S = dyn_cast<MCSymbolELF>(&A->getSymbol())) {
145
          // The "other" values are stored in the last 6 bits of the second
146
          // byte. The traditional defines for STO values assume the full byte
147
          // and thus the shift to pack it.
148
1.46k
          unsigned Other = S->getOther() << 2;
149
1.46k
          if ((Other & ELF::STO_PPC64_LOCAL_MASK) != 0)
150
0
            IsResolved = false;
151
1.46k
        }
152
1.46k
      }
153
1.63k
      break;
154
29.5k
    }
155
29.5k
  }
156
157
33.9k
  bool mayNeedRelaxation(const MCInst &Inst) const override {
158
    // FIXME.
159
33.9k
    return false;
160
33.9k
  }
161
162
  bool fixupNeedsRelaxation(const MCFixup &Fixup,
163
                            uint64_t Value,
164
                            const MCRelaxableFragment *DF,
165
0
                            const MCAsmLayout &Layout, unsigned &KsError) const override {
166
    // FIXME.
167
0
    llvm_unreachable("relaxInstruction() unimplemented");
168
0
  }
169
170
171
0
  void relaxInstruction(const MCInst &Inst, MCInst &Res) const override {
172
    // FIXME.
173
0
    llvm_unreachable("relaxInstruction() unimplemented");
174
0
  }
175
176
1.68k
  bool writeNopData(uint64_t Count, MCObjectWriter *OW) const override {
177
1.68k
    uint64_t NumNops = Count / 4;
178
2.99k
    for (uint64_t i = 0; i != NumNops; ++i)
179
1.30k
      OW->write32(0x60000000);
180
181
1.68k
    OW->WriteZeros(Count % 4);
182
183
1.68k
    return true;
184
1.68k
  }
185
186
7.36k
  unsigned getPointerSize() const {
187
7.36k
    StringRef Name = TheTarget.getName();
188
7.36k
    if (Name == "ppc64" || Name == "ppc64le") return 8;
189
7.36k
    assert(Name == "ppc32" && "Unknown target name!");
190
2.74k
    return 4;
191
2.74k
  }
192
193
7.36k
  bool isLittleEndian() const {
194
7.36k
    return IsLittleEndian;
195
7.36k
  }
196
};
197
} // end anonymous namespace
198
199
200
// FIXME: This should be in a separate file.
201
namespace {
202
  class ELFPPCAsmBackend : public PPCAsmBackend {
203
    uint8_t OSABI;
204
  public:
205
    ELFPPCAsmBackend(const Target &T, bool IsLittleEndian, uint8_t OSABI) :
206
7.36k
      PPCAsmBackend(T, IsLittleEndian), OSABI(OSABI) { }
207
208
7.36k
    MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const override {
209
7.36k
      bool is64 = getPointerSize() == 8;
210
7.36k
      return createPPCELFObjectWriter(OS, is64, isLittleEndian(), OSABI);
211
7.36k
    }
212
  };
213
214
} // end anonymous namespace
215
216
MCAsmBackend *llvm_ks::createPPCAsmBackend(const Target &T,
217
                                        const MCRegisterInfo &MRI,
218
7.36k
                                        const Triple &TT, StringRef CPU) {
219
7.36k
  uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TT.getOS());
220
7.36k
  bool IsLittleEndian = TT.getArch() == Triple::ppc64le;
221
7.36k
  return new ELFPPCAsmBackend(T, IsLittleEndian, OSABI);
222
7.36k
}