Coverage Report

Created: 2025-08-30 07:19

/src/keystone/llvm/lib/Target/PowerPC/MCTargetDesc/PPCAsmBackend.cpp
Line
Count
Source (jump to first uncovered line)
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
12.0k
static uint64_t adjustFixupValue(unsigned Kind, uint64_t Value) {
27
12.0k
  switch (Kind) {
28
0
  default:
29
0
    llvm_unreachable("Unknown fixup kind!");
30
0
  case FK_Data_1:
31
6.88k
  case FK_Data_2:
32
9.36k
  case FK_Data_4:
33
9.61k
  case FK_Data_8:
34
9.61k
  case PPC::fixup_ppc_nofixup:
35
9.61k
    return Value;
36
931
  case PPC::fixup_ppc_brcond14:
37
993
  case PPC::fixup_ppc_brcond14abs:
38
993
    return Value & 0xfffc;
39
1.27k
  case PPC::fixup_ppc_br24:
40
1.28k
  case PPC::fixup_ppc_br24abs:
41
1.28k
    return Value & 0x3fffffc;
42
127
  case PPC::fixup_ppc_half16:
43
127
    return Value & 0xffff;
44
64
  case PPC::fixup_ppc_half16ds:
45
64
    return Value & 0xfffc;
46
12.0k
  }
47
12.0k
}
48
49
9.31k
static unsigned getFixupKindNumBytes(unsigned Kind) {
50
9.31k
  switch (Kind) {
51
0
  default:
52
0
    llvm_unreachable("Unknown fixup kind!");
53
0
  case FK_Data_1:
54
0
    return 1;
55
6.49k
  case FK_Data_2:
56
6.62k
  case PPC::fixup_ppc_half16:
57
6.68k
  case PPC::fixup_ppc_half16ds:
58
6.68k
    return 2;
59
2.09k
  case FK_Data_4:
60
2.22k
  case PPC::fixup_ppc_brcond14:
61
2.27k
  case PPC::fixup_ppc_brcond14abs:
62
2.41k
  case PPC::fixup_ppc_br24:
63
2.42k
  case PPC::fixup_ppc_br24abs:
64
2.42k
    return 4;
65
201
  case FK_Data_8:
66
201
    return 8;
67
0
  case PPC::fixup_ppc_nofixup:
68
0
    return 0;
69
9.31k
  }
70
9.31k
}
71
72
namespace {
73
74
class PPCAsmBackend : public MCAsmBackend {
75
  const Target &TheTarget;
76
  bool IsLittleEndian;
77
public:
78
5.13k
  PPCAsmBackend(const Target &T, bool isLittle) : MCAsmBackend(), TheTarget(T),
79
5.13k
    IsLittleEndian(isLittle) {}
80
81
7.56k
  unsigned getNumFixupKinds() const override {
82
7.56k
    return PPC::NumTargetFixupKinds;
83
7.56k
  }
84
85
36.6k
  const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override {
86
36.6k
    const static MCFixupKindInfo InfosBE[PPC::NumTargetFixupKinds] = {
87
      // name                    offset  bits  flags
88
36.6k
      { "fixup_ppc_br24",        6,      24,   MCFixupKindInfo::FKF_IsPCRel },
89
36.6k
      { "fixup_ppc_brcond14",    16,     14,   MCFixupKindInfo::FKF_IsPCRel },
90
36.6k
      { "fixup_ppc_br24abs",     6,      24,   0 },
91
36.6k
      { "fixup_ppc_brcond14abs", 16,     14,   0 },
92
36.6k
      { "fixup_ppc_half16",       0,     16,   0 },
93
36.6k
      { "fixup_ppc_half16ds",     0,     14,   0 },
94
36.6k
      { "fixup_ppc_nofixup",      0,      0,   0 }
95
36.6k
    };
96
36.6k
    const static MCFixupKindInfo InfosLE[PPC::NumTargetFixupKinds] = {
97
      // name                    offset  bits  flags
98
36.6k
      { "fixup_ppc_br24",        2,      24,   MCFixupKindInfo::FKF_IsPCRel },
99
36.6k
      { "fixup_ppc_brcond14",    2,      14,   MCFixupKindInfo::FKF_IsPCRel },
100
36.6k
      { "fixup_ppc_br24abs",     2,      24,   0 },
101
36.6k
      { "fixup_ppc_brcond14abs", 2,      14,   0 },
102
36.6k
      { "fixup_ppc_half16",      0,      16,   0 },
103
36.6k
      { "fixup_ppc_half16ds",    2,      14,   0 },
104
36.6k
      { "fixup_ppc_nofixup",     0,       0,   0 }
105
36.6k
    };
106
107
36.6k
    if (Kind < FirstTargetFixupKind)
108
29.1k
      return MCAsmBackend::getFixupKindInfo(Kind);
109
110
7.56k
    assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
111
7.56k
           "Invalid kind!");
112
7.56k
    return (IsLittleEndian? InfosLE : InfosBE)[Kind - FirstTargetFixupKind];
113
7.56k
  }
114
115
  void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
116
12.0k
                  uint64_t Value, bool IsPCRel, unsigned int &KsError) const override {
117
12.0k
    Value = adjustFixupValue(Fixup.getKind(), Value);
118
12.0k
    if (!Value) return;           // Doesn't change encoding.
119
120
9.31k
    unsigned Offset = Fixup.getOffset();
121
9.31k
    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
34.0k
    for (unsigned i = 0; i != NumBytes; ++i) {
127
24.6k
      unsigned Idx = IsLittleEndian ? i : (NumBytes - 1 - i);
128
24.6k
      Data[Offset + i] |= uint8_t((Value >> (Idx * 8)) & 0xff);
129
24.6k
    }
130
9.31k
  }
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
12.0k
                         bool &IsResolved) override {
136
12.0k
    switch ((PPC::Fixups)Fixup.getKind()) {
137
10.7k
    default: break;
138
10.7k
    case PPC::fixup_ppc_br24:
139
1.28k
    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.28k
      if (const MCSymbolRefExpr *A = Target.getSymA()) {
144
1.11k
        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.11k
          unsigned Other = S->getOther() << 2;
149
1.11k
          if ((Other & ELF::STO_PPC64_LOCAL_MASK) != 0)
150
0
            IsResolved = false;
151
1.11k
        }
152
1.11k
      }
153
1.28k
      break;
154
12.0k
    }
155
12.0k
  }
156
157
29.8k
  bool mayNeedRelaxation(const MCInst &Inst) const override {
158
    // FIXME.
159
29.8k
    return false;
160
29.8k
  }
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.46k
  bool writeNopData(uint64_t Count, MCObjectWriter *OW) const override {
177
1.46k
    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.46k
    OW->WriteZeros(Count % 4);
182
183
1.46k
    return true;
184
1.46k
  }
185
186
5.13k
  unsigned getPointerSize() const {
187
5.13k
    StringRef Name = TheTarget.getName();
188
5.13k
    if (Name == "ppc64" || Name == "ppc64le") return 8;
189
1.75k
    assert(Name == "ppc32" && "Unknown target name!");
190
1.75k
    return 4;
191
1.75k
  }
192
193
5.13k
  bool isLittleEndian() const {
194
5.13k
    return IsLittleEndian;
195
5.13k
  }
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.13k
      PPCAsmBackend(T, IsLittleEndian), OSABI(OSABI) { }
207
208
5.13k
    MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const override {
209
5.13k
      bool is64 = getPointerSize() == 8;
210
5.13k
      return createPPCELFObjectWriter(OS, is64, isLittleEndian(), OSABI);
211
5.13k
    }
212
  };
213
214
} // end anonymous namespace
215
216
MCAsmBackend *llvm_ks::createPPCAsmBackend(const Target &T,
217
                                        const MCRegisterInfo &MRI,
218
5.13k
                                        const Triple &TT, StringRef CPU) {
219
5.13k
  uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TT.getOS());
220
5.13k
  bool IsLittleEndian = TT.getArch() == Triple::ppc64le;
221
5.13k
  return new ELFPPCAsmBackend(T, IsLittleEndian, OSABI);
222
5.13k
}