Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCCodeEmitter.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- SystemZMCCodeEmitter.cpp - Convert SystemZ code to machine code ---===//
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
// This file implements the SystemZMCCodeEmitter class.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "MCTargetDesc/SystemZMCFixups.h"
14
#include "MCTargetDesc/SystemZMCTargetDesc.h"
15
#include "llvm/ADT/SmallVector.h"
16
#include "llvm/MC/MCCodeEmitter.h"
17
#include "llvm/MC/MCContext.h"
18
#include "llvm/MC/MCExpr.h"
19
#include "llvm/MC/MCFixup.h"
20
#include "llvm/MC/MCInst.h"
21
#include "llvm/MC/MCInstrInfo.h"
22
#include "llvm/MC/MCRegisterInfo.h"
23
#include "llvm/MC/MCSubtargetInfo.h"
24
#include "llvm/Support/ErrorHandling.h"
25
#include "llvm/Support/raw_ostream.h"
26
#include <cassert>
27
#include <cstdint>
28
29
using namespace llvm;
30
31
#define DEBUG_TYPE "mccodeemitter"
32
33
namespace {
34
35
class SystemZMCCodeEmitter : public MCCodeEmitter {
36
  const MCInstrInfo &MCII;
37
  MCContext &Ctx;
38
39
public:
40
  SystemZMCCodeEmitter(const MCInstrInfo &mcii, MCContext &ctx)
41
28
    : MCII(mcii), Ctx(ctx) {
42
28
  }
43
44
28
  ~SystemZMCCodeEmitter() override = default;
45
46
  // OVerride MCCodeEmitter.
47
  void encodeInstruction(const MCInst &MI, raw_ostream &OS,
48
                         SmallVectorImpl<MCFixup> &Fixups,
49
                         const MCSubtargetInfo &STI) const override;
50
51
private:
52
  // Automatically generated by TableGen.
53
  uint64_t getBinaryCodeForInstr(const MCInst &MI,
54
                                 SmallVectorImpl<MCFixup> &Fixups,
55
                                 const MCSubtargetInfo &STI) const;
56
57
  // Called by the TableGen code to get the binary encoding of operand
58
  // MO in MI.  Fixups is the list of fixups against MI.
59
  uint64_t getMachineOpValue(const MCInst &MI, const MCOperand &MO,
60
                             SmallVectorImpl<MCFixup> &Fixups,
61
                             const MCSubtargetInfo &STI) const;
62
63
  // Called by the TableGen code to get the binary encoding of an address.
64
  // The index or length, if any, is encoded first, followed by the base,
65
  // followed by the displacement.  In a 20-bit displacement,
66
  // the low 12 bits are encoded before the high 8 bits.
67
  uint64_t getBDAddr12Encoding(const MCInst &MI, unsigned OpNum,
68
                               SmallVectorImpl<MCFixup> &Fixups,
69
                               const MCSubtargetInfo &STI) const;
70
  uint64_t getBDAddr20Encoding(const MCInst &MI, unsigned OpNum,
71
                               SmallVectorImpl<MCFixup> &Fixups,
72
                               const MCSubtargetInfo &STI) const;
73
  uint64_t getBDXAddr12Encoding(const MCInst &MI, unsigned OpNum,
74
                                SmallVectorImpl<MCFixup> &Fixups,
75
                                const MCSubtargetInfo &STI) const;
76
  uint64_t getBDXAddr20Encoding(const MCInst &MI, unsigned OpNum,
77
                                SmallVectorImpl<MCFixup> &Fixups,
78
                                const MCSubtargetInfo &STI) const;
79
  uint64_t getBDLAddr12Len4Encoding(const MCInst &MI, unsigned OpNum,
80
                                    SmallVectorImpl<MCFixup> &Fixups,
81
                                    const MCSubtargetInfo &STI) const;
82
  uint64_t getBDLAddr12Len8Encoding(const MCInst &MI, unsigned OpNum,
83
                                    SmallVectorImpl<MCFixup> &Fixups,
84
                                    const MCSubtargetInfo &STI) const;
85
  uint64_t getBDRAddr12Encoding(const MCInst &MI, unsigned OpNum,
86
                                SmallVectorImpl<MCFixup> &Fixups,
87
                                const MCSubtargetInfo &STI) const;
88
  uint64_t getBDVAddr12Encoding(const MCInst &MI, unsigned OpNum,
89
                                SmallVectorImpl<MCFixup> &Fixups,
90
                                const MCSubtargetInfo &STI) const;
91
92
  // Operand OpNum of MI needs a PC-relative fixup of kind Kind at
93
  // Offset bytes from the start of MI.  Add the fixup to Fixups
94
  // and return the in-place addend, which since we're a RELA target
95
  // is always 0.  If AllowTLS is true and optional operand OpNum + 1
96
  // is present, also emit a TLS call fixup for it.
97
  uint64_t getPCRelEncoding(const MCInst &MI, unsigned OpNum,
98
                            SmallVectorImpl<MCFixup> &Fixups,
99
                            unsigned Kind, int64_t Offset,
100
                            bool AllowTLS) const;
101
102
  uint64_t getPC16DBLEncoding(const MCInst &MI, unsigned OpNum,
103
                              SmallVectorImpl<MCFixup> &Fixups,
104
635
                              const MCSubtargetInfo &STI) const {
105
635
    return getPCRelEncoding(MI, OpNum, Fixups,
106
635
                            SystemZ::FK_390_PC16DBL, 2, false);
107
635
  }
108
  uint64_t getPC32DBLEncoding(const MCInst &MI, unsigned OpNum,
109
                              SmallVectorImpl<MCFixup> &Fixups,
110
343
                              const MCSubtargetInfo &STI) const {
111
343
    return getPCRelEncoding(MI, OpNum, Fixups,
112
343
                            SystemZ::FK_390_PC32DBL, 2, false);
113
343
  }
114
  uint64_t getPC16DBLTLSEncoding(const MCInst &MI, unsigned OpNum,
115
                                 SmallVectorImpl<MCFixup> &Fixups,
116
21
                                 const MCSubtargetInfo &STI) const {
117
21
    return getPCRelEncoding(MI, OpNum, Fixups,
118
21
                            SystemZ::FK_390_PC16DBL, 2, true);
119
21
  }
120
  uint64_t getPC32DBLTLSEncoding(const MCInst &MI, unsigned OpNum,
121
                                 SmallVectorImpl<MCFixup> &Fixups,
122
27
                                 const MCSubtargetInfo &STI) const {
123
27
    return getPCRelEncoding(MI, OpNum, Fixups,
124
27
                            SystemZ::FK_390_PC32DBL, 2, true);
125
27
  }
126
  uint64_t getPC12DBLBPPEncoding(const MCInst &MI, unsigned OpNum,
127
                                 SmallVectorImpl<MCFixup> &Fixups,
128
38
                                 const MCSubtargetInfo &STI) const {
129
38
    return getPCRelEncoding(MI, OpNum, Fixups,
130
38
                            SystemZ::FK_390_PC12DBL, 1, false);
131
38
  }
132
  uint64_t getPC16DBLBPPEncoding(const MCInst &MI, unsigned OpNum,
133
                                 SmallVectorImpl<MCFixup> &Fixups,
134
24
                                 const MCSubtargetInfo &STI) const {
135
24
    return getPCRelEncoding(MI, OpNum, Fixups,
136
24
                            SystemZ::FK_390_PC16DBL, 4, false);
137
24
  }
138
  uint64_t getPC24DBLBPPEncoding(const MCInst &MI, unsigned OpNum,
139
                                 SmallVectorImpl<MCFixup> &Fixups,
140
38
                                 const MCSubtargetInfo &STI) const {
141
38
    return getPCRelEncoding(MI, OpNum, Fixups,
142
38
                            SystemZ::FK_390_PC24DBL, 3, false);
143
38
  }
144
145
private:
146
  FeatureBitset computeAvailableFeatures(const FeatureBitset &FB) const;
147
  void
148
  verifyInstructionPredicates(const MCInst &MI,
149
                              const FeatureBitset &AvailableFeatures) const;
150
};
151
152
} // end anonymous namespace
153
154
void SystemZMCCodeEmitter::
155
encodeInstruction(const MCInst &MI, raw_ostream &OS,
156
                  SmallVectorImpl<MCFixup> &Fixups,
157
18.1k
                  const MCSubtargetInfo &STI) const {
158
18.1k
  verifyInstructionPredicates(MI,
159
18.1k
                              computeAvailableFeatures(STI.getFeatureBits()));
160
18.1k
161
18.1k
  uint64_t Bits = getBinaryCodeForInstr(MI, Fixups, STI);
162
18.1k
  unsigned Size = MCII.get(MI.getOpcode()).getSize();
163
18.1k
  // Big-endian insertion of Size bytes.
164
18.1k
  unsigned ShiftValue = (Size * 8) - 8;
165
117k
  for (unsigned I = 0; I != Size; 
++I99.5k
) {
166
99.5k
    OS << uint8_t(Bits >> ShiftValue);
167
99.5k
    ShiftValue -= 8;
168
99.5k
  }
169
18.1k
}
170
171
uint64_t SystemZMCCodeEmitter::
172
getMachineOpValue(const MCInst &MI, const MCOperand &MO,
173
                  SmallVectorImpl<MCFixup> &Fixups,
174
61.6k
                  const MCSubtargetInfo &STI) const {
175
61.6k
  if (MO.isReg())
176
44.9k
    return Ctx.getRegisterInfo()->getEncodingValue(MO.getReg());
177
16.7k
  if (MO.isImm())
178
16.7k
    return static_cast<uint64_t>(MO.getImm());
179
0
  llvm_unreachable("Unexpected operand type!");
180
0
}
181
182
uint64_t SystemZMCCodeEmitter::
183
getBDAddr12Encoding(const MCInst &MI, unsigned OpNum,
184
                    SmallVectorImpl<MCFixup> &Fixups,
185
2.04k
                    const MCSubtargetInfo &STI) const {
186
2.04k
  uint64_t Base = getMachineOpValue(MI, MI.getOperand(OpNum), Fixups, STI);
187
2.04k
  uint64_t Disp = getMachineOpValue(MI, MI.getOperand(OpNum + 1), Fixups, STI);
188
2.04k
  assert(isUInt<4>(Base) && isUInt<12>(Disp));
189
2.04k
  return (Base << 12) | Disp;
190
2.04k
}
191
192
uint64_t SystemZMCCodeEmitter::
193
getBDAddr20Encoding(const MCInst &MI, unsigned OpNum,
194
                    SmallVectorImpl<MCFixup> &Fixups,
195
1.21k
                    const MCSubtargetInfo &STI) const {
196
1.21k
  uint64_t Base = getMachineOpValue(MI, MI.getOperand(OpNum), Fixups, STI);
197
1.21k
  uint64_t Disp = getMachineOpValue(MI, MI.getOperand(OpNum + 1), Fixups, STI);
198
1.21k
  assert(isUInt<4>(Base) && isInt<20>(Disp));
199
1.21k
  return (Base << 20) | ((Disp & 0xfff) << 8) | ((Disp & 0xff000) >> 12);
200
1.21k
}
201
202
uint64_t SystemZMCCodeEmitter::
203
getBDXAddr12Encoding(const MCInst &MI, unsigned OpNum,
204
                     SmallVectorImpl<MCFixup> &Fixups,
205
1.45k
                     const MCSubtargetInfo &STI) const {
206
1.45k
  uint64_t Base = getMachineOpValue(MI, MI.getOperand(OpNum), Fixups, STI);
207
1.45k
  uint64_t Disp = getMachineOpValue(MI, MI.getOperand(OpNum + 1), Fixups, STI);
208
1.45k
  uint64_t Index = getMachineOpValue(MI, MI.getOperand(OpNum + 2), Fixups, STI);
209
1.45k
  assert(isUInt<4>(Base) && isUInt<12>(Disp) && isUInt<4>(Index));
210
1.45k
  return (Index << 16) | (Base << 12) | Disp;
211
1.45k
}
212
213
uint64_t SystemZMCCodeEmitter::
214
getBDXAddr20Encoding(const MCInst &MI, unsigned OpNum,
215
                     SmallVectorImpl<MCFixup> &Fixups,
216
1.53k
                     const MCSubtargetInfo &STI) const {
217
1.53k
  uint64_t Base = getMachineOpValue(MI, MI.getOperand(OpNum), Fixups, STI);
218
1.53k
  uint64_t Disp = getMachineOpValue(MI, MI.getOperand(OpNum + 1), Fixups, STI);
219
1.53k
  uint64_t Index = getMachineOpValue(MI, MI.getOperand(OpNum + 2), Fixups, STI);
220
1.53k
  assert(isUInt<4>(Base) && isInt<20>(Disp) && isUInt<4>(Index));
221
1.53k
  return (Index << 24) | (Base << 20) | ((Disp & 0xfff) << 8)
222
1.53k
    | ((Disp & 0xff000) >> 12);
223
1.53k
}
224
225
uint64_t SystemZMCCodeEmitter::
226
getBDLAddr12Len4Encoding(const MCInst &MI, unsigned OpNum,
227
                         SmallVectorImpl<MCFixup> &Fixups,
228
272
                         const MCSubtargetInfo &STI) const {
229
272
  uint64_t Base = getMachineOpValue(MI, MI.getOperand(OpNum), Fixups, STI);
230
272
  uint64_t Disp = getMachineOpValue(MI, MI.getOperand(OpNum + 1), Fixups, STI);
231
272
  uint64_t Len  = getMachineOpValue(MI, MI.getOperand(OpNum + 2), Fixups, STI) - 1;
232
272
  assert(isUInt<4>(Base) && isUInt<12>(Disp) && isUInt<4>(Len));
233
272
  return (Len << 16) | (Base << 12) | Disp;
234
272
}
235
236
uint64_t SystemZMCCodeEmitter::
237
getBDLAddr12Len8Encoding(const MCInst &MI, unsigned OpNum,
238
                         SmallVectorImpl<MCFixup> &Fixups,
239
348
                         const MCSubtargetInfo &STI) const {
240
348
  uint64_t Base = getMachineOpValue(MI, MI.getOperand(OpNum), Fixups, STI);
241
348
  uint64_t Disp = getMachineOpValue(MI, MI.getOperand(OpNum + 1), Fixups, STI);
242
348
  uint64_t Len  = getMachineOpValue(MI, MI.getOperand(OpNum + 2), Fixups, STI) - 1;
243
348
  assert(isUInt<4>(Base) && isUInt<12>(Disp) && isUInt<8>(Len));
244
348
  return (Len << 16) | (Base << 12) | Disp;
245
348
}
246
247
uint64_t SystemZMCCodeEmitter::
248
getBDRAddr12Encoding(const MCInst &MI, unsigned OpNum,
249
                     SmallVectorImpl<MCFixup> &Fixups,
250
40
                     const MCSubtargetInfo &STI) const {
251
40
  uint64_t Base = getMachineOpValue(MI, MI.getOperand(OpNum), Fixups, STI);
252
40
  uint64_t Disp = getMachineOpValue(MI, MI.getOperand(OpNum + 1), Fixups, STI);
253
40
  uint64_t Len  = getMachineOpValue(MI, MI.getOperand(OpNum + 2), Fixups, STI);
254
40
  assert(isUInt<4>(Base) && isUInt<12>(Disp) && isUInt<4>(Len));
255
40
  return (Len << 16) | (Base << 12) | Disp;
256
40
}
257
258
uint64_t SystemZMCCodeEmitter::
259
getBDVAddr12Encoding(const MCInst &MI, unsigned OpNum,
260
                     SmallVectorImpl<MCFixup> &Fixups,
261
80
                     const MCSubtargetInfo &STI) const {
262
80
  uint64_t Base = getMachineOpValue(MI, MI.getOperand(OpNum), Fixups, STI);
263
80
  uint64_t Disp = getMachineOpValue(MI, MI.getOperand(OpNum + 1), Fixups, STI);
264
80
  uint64_t Index = getMachineOpValue(MI, MI.getOperand(OpNum + 2), Fixups, STI);
265
80
  assert(isUInt<4>(Base) && isUInt<12>(Disp) && isUInt<5>(Index));
266
80
  return (Index << 16) | (Base << 12) | Disp;
267
80
}
268
269
uint64_t
270
SystemZMCCodeEmitter::getPCRelEncoding(const MCInst &MI, unsigned OpNum,
271
                                       SmallVectorImpl<MCFixup> &Fixups,
272
                                       unsigned Kind, int64_t Offset,
273
1.12k
                                       bool AllowTLS) const {
274
1.12k
  const MCOperand &MO = MI.getOperand(OpNum);
275
1.12k
  const MCExpr *Expr;
276
1.12k
  if (MO.isImm())
277
0
    Expr = MCConstantExpr::create(MO.getImm() + Offset, Ctx);
278
1.12k
  else {
279
1.12k
    Expr = MO.getExpr();
280
1.12k
    if (Offset) {
281
1.12k
      // The operand value is relative to the start of MI, but the fixup
282
1.12k
      // is relative to the operand field itself, which is Offset bytes
283
1.12k
      // into MI.  Add Offset to the relocation value to cancel out
284
1.12k
      // this difference.
285
1.12k
      const MCExpr *OffsetExpr = MCConstantExpr::create(Offset, Ctx);
286
1.12k
      Expr = MCBinaryExpr::createAdd(Expr, OffsetExpr, Ctx);
287
1.12k
    }
288
1.12k
  }
289
1.12k
  Fixups.push_back(MCFixup::create(Offset, Expr, (MCFixupKind)Kind));
290
1.12k
291
1.12k
  // Output the fixup for the TLS marker if present.
292
1.12k
  if (AllowTLS && 
OpNum + 1 < MI.getNumOperands()48
) {
293
8
    const MCOperand &MOTLS = MI.getOperand(OpNum + 1);
294
8
    Fixups.push_back(MCFixup::create(0, MOTLS.getExpr(),
295
8
                                     (MCFixupKind)SystemZ::FK_390_TLS_CALL));
296
8
  }
297
1.12k
  return 0;
298
1.12k
}
299
300
#define ENABLE_INSTR_PREDICATE_VERIFIER
301
#include "SystemZGenMCCodeEmitter.inc"
302
303
MCCodeEmitter *llvm::createSystemZMCCodeEmitter(const MCInstrInfo &MCII,
304
                                                const MCRegisterInfo &MRI,
305
28
                                                MCContext &Ctx) {
306
28
  return new SystemZMCCodeEmitter(MCII, Ctx);
307
28
}