LLVM 22.0.0git
VEMCCodeEmitter.cpp
Go to the documentation of this file.
1//===-- VEMCCodeEmitter.cpp - Convert VE 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 VEMCCodeEmitter class.
10//
11//===----------------------------------------------------------------------===//
12
14#include "VE.h"
15#include "VEMCAsmInfo.h"
17#include "llvm/ADT/Statistic.h"
19#include "llvm/MC/MCContext.h"
20#include "llvm/MC/MCExpr.h"
21#include "llvm/MC/MCFixup.h"
22#include "llvm/MC/MCInst.h"
23#include "llvm/MC/MCInstrInfo.h"
26#include "llvm/MC/MCSymbol.h"
29#include <cassert>
30#include <cstdint>
31
32using namespace llvm;
33
34#define DEBUG_TYPE "mccodeemitter"
35
36STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
37
38namespace {
39
40class VEMCCodeEmitter : public MCCodeEmitter {
41 MCContext &Ctx;
42
43public:
44 VEMCCodeEmitter(const MCInstrInfo &, MCContext &ctx)
45 : Ctx(ctx) {}
46 VEMCCodeEmitter(const VEMCCodeEmitter &) = delete;
47 VEMCCodeEmitter &operator=(const VEMCCodeEmitter &) = delete;
48 ~VEMCCodeEmitter() override = default;
49
52 const MCSubtargetInfo &STI) const override;
53
54 // getBinaryCodeForInstr - TableGen'erated function for getting the
55 // binary encoding for an instruction.
56 uint64_t getBinaryCodeForInstr(const MCInst &MI,
58 const MCSubtargetInfo &STI) const;
59
60 /// getMachineOpValue - Return binary encoding of operand. If the machine
61 /// operand requires relocation, record the relocation and return zero.
62 unsigned getMachineOpValue(const MCInst &MI, const MCOperand &MO,
64 const MCSubtargetInfo &STI) const;
65
66 uint64_t getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
68 const MCSubtargetInfo &STI) const;
69 uint64_t getCCOpValue(const MCInst &MI, unsigned OpNo,
71 const MCSubtargetInfo &STI) const;
72 uint64_t getRDOpValue(const MCInst &MI, unsigned OpNo,
74 const MCSubtargetInfo &STI) const;
75};
76
77} // end anonymous namespace
78
80 const MCExpr *Value, uint16_t Kind) {
81 bool PCRel = false;
82 switch (Kind) {
86 PCRel = true;
87 }
88 Fixups.push_back(MCFixup::create(Offset, Value, Kind, PCRel));
89}
90
91void VEMCCodeEmitter::encodeInstruction(const MCInst &MI,
94 const MCSubtargetInfo &STI) const {
95 uint64_t Bits = getBinaryCodeForInstr(MI, Fixups, STI);
96 support::endian::write<uint64_t>(CB, Bits, llvm::endianness::little);
97
98 ++MCNumEmitted; // Keep track of the # of mi's emitted.
99}
100
101unsigned VEMCCodeEmitter::getMachineOpValue(const MCInst &MI,
102 const MCOperand &MO,
104 const MCSubtargetInfo &STI) const {
105 if (MO.isReg())
106 return Ctx.getRegisterInfo()->getEncodingValue(MO.getReg());
107 if (MO.isImm())
108 return static_cast<unsigned>(MO.getImm());
109
110 assert(MO.isExpr());
111
112 const MCExpr *Expr = MO.getExpr();
113 if (const auto *SExpr = dyn_cast<MCSpecifierExpr>(Expr)) {
114 auto Kind = VE::getFixupKind(SExpr->getSpecifier());
115 addFixup(Fixups, 0, Expr, Kind);
116 return 0;
117 }
118
119 int64_t Res;
120 if (Expr->evaluateAsAbsolute(Res))
121 return Res;
122
123 llvm_unreachable("Unhandled expression!");
124 return 0;
125}
126
128VEMCCodeEmitter::getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
130 const MCSubtargetInfo &STI) const {
131 const MCOperand &MO = MI.getOperand(OpNo);
132 if (MO.isReg() || MO.isImm())
133 return getMachineOpValue(MI, MO, Fixups, STI);
134
135 addFixup(Fixups, 0, MO.getExpr(), VE::fixup_ve_srel32);
136 return 0;
137}
138
139uint64_t VEMCCodeEmitter::getCCOpValue(const MCInst &MI, unsigned OpNo,
141 const MCSubtargetInfo &STI) const {
142 const MCOperand &MO = MI.getOperand(OpNo);
143 if (MO.isImm())
144 return VECondCodeToVal(
145 static_cast<VECC::CondCode>(getMachineOpValue(MI, MO, Fixups, STI)));
146 return 0;
147}
148
149uint64_t VEMCCodeEmitter::getRDOpValue(const MCInst &MI, unsigned OpNo,
151 const MCSubtargetInfo &STI) const {
152 const MCOperand &MO = MI.getOperand(OpNo);
153 if (MO.isImm())
154 return VERDToVal(static_cast<VERD::RoundingMode>(
155 getMachineOpValue(MI, MO, Fixups, STI)));
156 return 0;
157}
158
159#include "VEGenMCCodeEmitter.inc"
160
162 MCContext &Ctx) {
163 return new VEMCCodeEmitter(MCII, Ctx);
164}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx, unsigned FixupKind, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI)
getBranchTargetOpValue - Helper function to get the branch target operand, which is either an immedia...
IRTranslator LLVM IR MI
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition: Statistic.h:167
static void addFixup(SmallVectorImpl< MCFixup > &Fixups, uint32_t Offset, const MCExpr *Value, uint16_t Kind)
MCCodeEmitter - Generic instruction encoding interface.
Definition: MCCodeEmitter.h:23
virtual void encodeInstruction(const MCInst &Inst, SmallVectorImpl< char > &CB, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const =0
Encode the given Inst to bytes and append to CB.
MCCodeEmitter & operator=(const MCCodeEmitter &)=delete
Context object for machine code objects.
Definition: MCContext.h:83
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
static MCFixup create(uint32_t Offset, const MCExpr *Value, MCFixupKind Kind, bool PCRel=false)
Consider bit fields if we need more flags.
Definition: MCFixup.h:86
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:188
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:27
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:40
int64_t getImm() const
Definition: MCInst.h:84
bool isImm() const
Definition: MCInst.h:66
bool isReg() const
Definition: MCInst.h:65
MCRegister getReg() const
Returns the register number.
Definition: MCInst.h:73
const MCExpr * getExpr() const
Definition: MCInst.h:118
bool isExpr() const
Definition: MCInst.h:69
Generic base class for all target subtargets.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:574
LLVM Value Representation.
Definition: Value.h:75
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
CondCode
Definition: VE.h:43
RoundingMode
Definition: VE.h:75
@ fixup_ve_srel32
fixup_ve_srel32 - 32-bit fixup corresponding to foo for relative branch
Definition: VEFixupKinds.h:21
@ fixup_ve_pc_hi32
fixup_ve_pc_hi32 - 32-bit fixup corresponding to foo@pc_hi
Definition: VEFixupKinds.h:30
@ fixup_ve_pc_lo32
fixup_ve_pc_lo32 - 32-bit fixup corresponding to foo@pc_lo
Definition: VEFixupKinds.h:33
VE::Fixups getFixupKind(uint8_t S)
Definition: VEMCAsmInfo.cpp:38
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:477
static unsigned VECondCodeToVal(VECC::CondCode CC)
Definition: VE.h:155
static unsigned VERDToVal(VERD::RoundingMode R)
Definition: VE.h:295
MCCodeEmitter * createVEMCCodeEmitter(const MCInstrInfo &MCII, MCContext &Ctx)