Coverage Report

Created: 2025-12-27 06:12

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
21.8k
static uint64_t adjustFixupValue(unsigned Kind, uint64_t Value) {
27
21.8k
  switch (Kind) {
28
0
  default:
29
0
    llvm_unreachable("Unknown fixup kind!");
30
0
  case FK_Data_1:
31
16.5k
  case FK_Data_2:
32
18.9k
  case FK_Data_4:
33
19.2k
  case FK_Data_8:
34
19.2k
  case PPC::fixup_ppc_nofixup:
35
19.2k
    return Value;
36
991
  case PPC::fixup_ppc_brcond14:
37
1.05k
  case PPC::fixup_ppc_brcond14abs:
38
1.05k
    return Value & 0xfffc;
39
1.24k
  case PPC::fixup_ppc_br24:
40
1.25k
  case PPC::fixup_ppc_br24abs:
41
1.25k
    return Value & 0x3fffffc;
42
306
  case PPC::fixup_ppc_half16:
43
306
    return Value & 0xffff;
44
41
  case PPC::fixup_ppc_half16ds:
45
41
    return Value & 0xfffc;
46
21.8k
  }
47
21.8k
}
48
49
19.0k
static unsigned getFixupKindNumBytes(unsigned Kind) {
50
19.0k
  switch (Kind) {
51
0
  default:
52
0
    llvm_unreachable("Unknown fixup kind!");
53
0
  case FK_Data_1:
54
0
    return 1;
55
16.0k
  case FK_Data_2:
56
16.3k
  case PPC::fixup_ppc_half16:
57
16.3k
  case PPC::fixup_ppc_half16ds:
58
16.3k
    return 2;
59
2.10k
  case FK_Data_4:
60
2.25k
  case PPC::fixup_ppc_brcond14:
61
2.31k
  case PPC::fixup_ppc_brcond14abs:
62
2.43k
  case PPC::fixup_ppc_br24:
63
2.44k
  case PPC::fixup_ppc_br24abs:
64
2.44k
    return 4;
65
221
  case FK_Data_8:
66
221
    return 8;
67
0
  case PPC::fixup_ppc_nofixup:
68
0
    return 0;
69
19.0k
  }
70
19.0k
}
71
72
namespace {
73
74
class PPCAsmBackend : public MCAsmBackend {
75
  const Target &TheTarget;
76
  bool IsLittleEndian;
77
public:
78
5.77k
  PPCAsmBackend(const Target &T, bool isLittle) : MCAsmBackend(), TheTarget(T),
79
5.77k
    IsLittleEndian(isLittle) {}
80
81
8.12k
  unsigned getNumFixupKinds() const override {
82
8.12k
    return PPC::NumTargetFixupKinds;
83
8.12k
  }
84
85
66.0k
  const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override {
86
66.0k
    const static MCFixupKindInfo InfosBE[PPC::NumTargetFixupKinds] = {
87
      // name                    offset  bits  flags
88
66.0k
      { "fixup_ppc_br24",        6,      24,   MCFixupKindInfo::FKF_IsPCRel },
89
66.0k
      { "fixup_ppc_brcond14",    16,     14,   MCFixupKindInfo::FKF_IsPCRel },
90
66.0k
      { "fixup_ppc_br24abs",     6,      24,   0 },
91
66.0k
      { "fixup_ppc_brcond14abs", 16,     14,   0 },
92
66.0k
      { "fixup_ppc_half16",       0,     16,   0 },
93
66.0k
      { "fixup_ppc_half16ds",     0,     14,   0 },
94
66.0k
      { "fixup_ppc_nofixup",      0,      0,   0 }
95
66.0k
    };
96
66.0k
    const static MCFixupKindInfo InfosLE[PPC::NumTargetFixupKinds] = {
97
      // name                    offset  bits  flags
98
66.0k
      { "fixup_ppc_br24",        2,      24,   MCFixupKindInfo::FKF_IsPCRel },
99
66.0k
      { "fixup_ppc_brcond14",    2,      14,   MCFixupKindInfo::FKF_IsPCRel },
100
66.0k
      { "fixup_ppc_br24abs",     2,      24,   0 },
101
66.0k
      { "fixup_ppc_brcond14abs", 2,      14,   0 },
102
66.0k
      { "fixup_ppc_half16",      0,      16,   0 },
103
66.0k
      { "fixup_ppc_half16ds",    2,      14,   0 },
104
66.0k
      { "fixup_ppc_nofixup",     0,       0,   0 }
105
66.0k
    };
106
107
66.0k
    if (Kind < FirstTargetFixupKind)
108
57.9k
      return MCAsmBackend::getFixupKindInfo(Kind);
109
110
66.0k
    assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
111
8.12k
           "Invalid kind!");
112
8.12k
    return (IsLittleEndian? InfosLE : InfosBE)[Kind - FirstTargetFixupKind];
113
8.12k
  }
114
115
  void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
116
21.8k
                  uint64_t Value, bool IsPCRel, unsigned int &KsError) const override {
117
21.8k
    Value = adjustFixupValue(Fixup.getKind(), Value);
118
21.8k
    if (!Value) return;           // Doesn't change encoding.
119
120
19.0k
    unsigned Offset = Fixup.getOffset();
121
19.0k
    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
63.2k
    for (unsigned i = 0; i != NumBytes; ++i) {
127
44.2k
      unsigned Idx = IsLittleEndian ? i : (NumBytes - 1 - i);
128
44.2k
      Data[Offset + i] |= uint8_t((Value >> (Idx * 8)) & 0xff);
129
44.2k
    }
130
19.0k
  }
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
21.8k
                         bool &IsResolved) override {
136
21.8k
    switch ((PPC::Fixups)Fixup.getKind()) {
137
20.6k
    default: break;
138
20.6k
    case PPC::fixup_ppc_br24:
139
1.25k
    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.25k
      if (const MCSymbolRefExpr *A = Target.getSymA()) {
144
1.12k
        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.12k
          unsigned Other = S->getOther() << 2;
149
1.12k
          if ((Other & ELF::STO_PPC64_LOCAL_MASK) != 0)
150
0
            IsResolved = false;
151
1.12k
        }
152
1.12k
      }
153
1.25k
      break;
154
21.8k
    }
155
21.8k
  }
156
157
30.5k
  bool mayNeedRelaxation(const MCInst &Inst) const override {
158
    // FIXME.
159
30.5k
    return false;
160
30.5k
  }
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.96k
  bool writeNopData(uint64_t Count, MCObjectWriter *OW) const override {
177
1.96k
    uint64_t NumNops = Count / 4;
178
4.19M
    for (uint64_t i = 0; i != NumNops; ++i)
179
4.19M
      OW->write32(0x60000000);
180
181
1.96k
    OW->WriteZeros(Count % 4);
182
183
1.96k
    return true;
184
1.96k
  }
185
186
5.77k
  unsigned getPointerSize() const {
187
5.77k
    StringRef Name = TheTarget.getName();
188
5.77k
    if (Name == "ppc64" || Name == "ppc64le") return 8;
189
5.77k
    assert(Name == "ppc32" && "Unknown target name!");
190
2.08k
    return 4;
191
2.08k
  }
192
193
5.77k
  bool isLittleEndian() const {
194
5.77k
    return IsLittleEndian;
195
5.77k
  }
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
5.77k
      PPCAsmBackend(T, IsLittleEndian), OSABI(OSABI) { }
207
208
5.77k
    MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const override {
209
5.77k
      bool is64 = getPointerSize() == 8;
210
5.77k
      return createPPCELFObjectWriter(OS, is64, isLittleEndian(), OSABI);
211
5.77k
    }
212
  };
213
214
} // end anonymous namespace
215
216
MCAsmBackend *llvm_ks::createPPCAsmBackend(const Target &T,
217
                                        const MCRegisterInfo &MRI,
218
5.77k
                                        const Triple &TT, StringRef CPU) {
219
5.77k
  uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TT.getOS());
220
5.77k
  bool IsLittleEndian = TT.getArch() == Triple::ppc64le;
221
5.77k
  return new ELFPPCAsmBackend(T, IsLittleEndian, OSABI);
222
5.77k
}