Coverage Report

Created: 2025-07-14 06:17

/src/keystone/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCAsmBackend.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- SystemZMCAsmBackend.cpp - SystemZ 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/SystemZMCTargetDesc.h"
11
#include "MCTargetDesc/SystemZMCFixups.h"
12
#include "llvm/MC/MCAsmBackend.h"
13
#include "llvm/MC/MCELFObjectWriter.h"
14
#include "llvm/MC/MCFixupKindInfo.h"
15
#include "llvm/MC/MCInst.h"
16
#include "llvm/MC/MCObjectWriter.h"
17
18
#include <keystone/keystone.h>
19
20
using namespace llvm_ks;
21
22
// Value is a fully-resolved relocation value: Symbol + Addend [- Pivot].
23
// Return the bits that should be installed in a relocation field for
24
// fixup kind Kind.
25
0
static uint64_t extractBitsForFixup(MCFixupKind Kind, uint64_t Value) {
26
0
  if (Kind < FirstTargetFixupKind)
27
0
    return Value;
28
29
0
  switch (unsigned(Kind)) {
30
0
  case SystemZ::FK_390_PC16DBL:
31
0
  case SystemZ::FK_390_PC32DBL:
32
0
    return (int64_t)Value / 2;
33
34
0
  case SystemZ::FK_390_TLS_CALL:
35
0
    return 0;
36
0
  }
37
38
0
  llvm_unreachable("Unknown fixup kind!");
39
0
}
40
41
namespace {
42
class SystemZMCAsmBackend : public MCAsmBackend {
43
  uint8_t OSABI;
44
public:
45
  SystemZMCAsmBackend(uint8_t osABI)
46
38
    : OSABI(osABI) {}
47
48
  // Override MCAsmBackend
49
0
  unsigned getNumFixupKinds() const override {
50
0
    return SystemZ::NumTargetFixupKinds;
51
0
  }
52
  const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override;
53
  void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
54
                  uint64_t Value, bool IsPCRel, unsigned int &KsError) const override;
55
0
  bool mayNeedRelaxation(const MCInst &Inst) const override {
56
0
    return false;
57
0
  }
58
  bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
59
                            const MCRelaxableFragment *Fragment,
60
0
                            const MCAsmLayout &Layout, unsigned &KsError) const override {
61
0
    return false;
62
0
  }
63
0
  void relaxInstruction(const MCInst &Inst, MCInst &Res) const override {
64
0
    llvm_unreachable("SystemZ does do not have assembler relaxation");
65
0
  }
66
  bool writeNopData(uint64_t Count, MCObjectWriter *OW) const override;
67
38
  MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const override {
68
38
    return createSystemZObjectWriter(OS, OSABI);
69
38
  }
70
};
71
} // end anonymous namespace
72
73
const MCFixupKindInfo &
74
0
SystemZMCAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
75
0
  const static MCFixupKindInfo Infos[SystemZ::NumTargetFixupKinds] = {
76
0
    { "FK_390_PC16DBL",  0, 16, MCFixupKindInfo::FKF_IsPCRel },
77
0
    { "FK_390_PC32DBL",  0, 32, MCFixupKindInfo::FKF_IsPCRel },
78
0
    { "FK_390_TLS_CALL", 0, 0, 0 }
79
0
  };
80
81
0
  if (Kind < FirstTargetFixupKind)
82
0
    return MCAsmBackend::getFixupKindInfo(Kind);
83
84
0
  assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
85
0
         "Invalid kind!");
86
0
  return Infos[Kind - FirstTargetFixupKind];
87
0
}
88
89
void SystemZMCAsmBackend::applyFixup(const MCFixup &Fixup, char *Data,
90
                                     unsigned DataSize, uint64_t Value,
91
0
                                     bool IsPCRel, unsigned int &KsError) const {
92
0
  MCFixupKind Kind = Fixup.getKind();
93
0
  unsigned Offset = Fixup.getOffset();
94
0
  unsigned Size = (getFixupKindInfo(Kind).TargetSize + 7) / 8;
95
96
  //assert(Offset + Size <= DataSize && "Invalid fixup offset!");
97
0
  if (Offset + Size > DataSize) {
98
0
      KsError = KS_ERR_ASM_FIXUP_INVALID;
99
0
      return;
100
0
  }
101
102
  // Big-endian insertion of Size bytes.
103
0
  Value = extractBitsForFixup(Kind, Value);
104
0
  unsigned ShiftValue = (Size * 8) - 8;
105
0
  for (unsigned I = 0; I != Size; ++I) {
106
0
    Data[Offset + I] |= uint8_t(Value >> ShiftValue);
107
0
    ShiftValue -= 8;
108
0
  }
109
0
}
110
111
bool SystemZMCAsmBackend::writeNopData(uint64_t Count,
112
0
                                       MCObjectWriter *OW) const {
113
0
  for (uint64_t I = 0; I != Count; ++I)
114
0
    OW->write8(7);
115
0
  return true;
116
0
}
117
118
MCAsmBackend *llvm_ks::createSystemZMCAsmBackend(const Target &T,
119
                                              const MCRegisterInfo &MRI,
120
38
                                              const Triple &TT, StringRef CPU) {
121
38
  uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TT.getOS());
122
38
  return new SystemZMCAsmBackend(OSABI);
123
38
}