10b57cec5SDimitry Andric //===-- SystemZMCCodeEmitter.cpp - Convert SystemZ code to machine code ---===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements the SystemZMCCodeEmitter class.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric #include "MCTargetDesc/SystemZMCFixups.h"
140b57cec5SDimitry Andric #include "MCTargetDesc/SystemZMCTargetDesc.h"
150b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
160b57cec5SDimitry Andric #include "llvm/MC/MCCodeEmitter.h"
170b57cec5SDimitry Andric #include "llvm/MC/MCContext.h"
180b57cec5SDimitry Andric #include "llvm/MC/MCExpr.h"
190b57cec5SDimitry Andric #include "llvm/MC/MCFixup.h"
200b57cec5SDimitry Andric #include "llvm/MC/MCInst.h"
210b57cec5SDimitry Andric #include "llvm/MC/MCInstrInfo.h"
220b57cec5SDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
230b57cec5SDimitry Andric #include "llvm/MC/MCSubtargetInfo.h"
240b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
250b57cec5SDimitry Andric #include <cassert>
260b57cec5SDimitry Andric #include <cstdint>
270b57cec5SDimitry Andric
280b57cec5SDimitry Andric using namespace llvm;
290b57cec5SDimitry Andric
300b57cec5SDimitry Andric #define DEBUG_TYPE "mccodeemitter"
310b57cec5SDimitry Andric
320b57cec5SDimitry Andric namespace {
330b57cec5SDimitry Andric
340b57cec5SDimitry Andric class SystemZMCCodeEmitter : public MCCodeEmitter {
350b57cec5SDimitry Andric const MCInstrInfo &MCII;
360b57cec5SDimitry Andric MCContext &Ctx;
370b57cec5SDimitry Andric
380b57cec5SDimitry Andric public:
SystemZMCCodeEmitter(const MCInstrInfo & MCII,MCContext & Ctx)39*5f757f3fSDimitry Andric SystemZMCCodeEmitter(const MCInstrInfo &MCII, MCContext &Ctx)
40*5f757f3fSDimitry Andric : MCII(MCII), Ctx(Ctx) {}
410b57cec5SDimitry Andric
420b57cec5SDimitry Andric ~SystemZMCCodeEmitter() override = default;
430b57cec5SDimitry Andric
440b57cec5SDimitry Andric // OVerride MCCodeEmitter.
4506c3fb27SDimitry Andric void encodeInstruction(const MCInst &MI, SmallVectorImpl<char> &CB,
460b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
470b57cec5SDimitry Andric const MCSubtargetInfo &STI) const override;
480b57cec5SDimitry Andric
490b57cec5SDimitry Andric private:
500b57cec5SDimitry Andric // Automatically generated by TableGen.
510b57cec5SDimitry Andric uint64_t getBinaryCodeForInstr(const MCInst &MI,
520b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
530b57cec5SDimitry Andric const MCSubtargetInfo &STI) const;
5406c3fb27SDimitry Andric uint32_t getOperandBitOffset(const MCInst &MI, unsigned OpNum,
5506c3fb27SDimitry Andric const MCSubtargetInfo &STI) const;
560b57cec5SDimitry Andric
570b57cec5SDimitry Andric // Called by the TableGen code to get the binary encoding of operand
580b57cec5SDimitry Andric // MO in MI. Fixups is the list of fixups against MI.
590b57cec5SDimitry Andric uint64_t getMachineOpValue(const MCInst &MI, const MCOperand &MO,
600b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
610b57cec5SDimitry Andric const MCSubtargetInfo &STI) const;
620b57cec5SDimitry Andric
6306c3fb27SDimitry Andric // Return the encoded immediate value for the OpNum operand. If it is a
6406c3fb27SDimitry Andric // symbol, add a fixup for it and return 0.
6506c3fb27SDimitry Andric template <SystemZ::FixupKind Kind>
6606c3fb27SDimitry Andric uint64_t getImmOpValue(const MCInst &MI, unsigned OpNum,
67349cc55cSDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
6806c3fb27SDimitry Andric const MCSubtargetInfo &STI) const;
69349cc55cSDimitry Andric
7006c3fb27SDimitry Andric // Called by the TableGen code to get the binary encoding of a length value.
7106c3fb27SDimitry Andric // Length values are encoded by subtracting 1 from the actual value.
7206c3fb27SDimitry Andric template <SystemZ::FixupKind Kind>
7306c3fb27SDimitry Andric uint64_t getLenEncoding(const MCInst &MI, unsigned OpNum,
740b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
750b57cec5SDimitry Andric const MCSubtargetInfo &STI) const;
760b57cec5SDimitry Andric
770b57cec5SDimitry Andric // Operand OpNum of MI needs a PC-relative fixup of kind Kind at
780b57cec5SDimitry Andric // Offset bytes from the start of MI. Add the fixup to Fixups
790b57cec5SDimitry Andric // and return the in-place addend, which since we're a RELA target
800b57cec5SDimitry Andric // is always 0. If AllowTLS is true and optional operand OpNum + 1
810b57cec5SDimitry Andric // is present, also emit a TLS call fixup for it.
820b57cec5SDimitry Andric uint64_t getPCRelEncoding(const MCInst &MI, unsigned OpNum,
830b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
840b57cec5SDimitry Andric unsigned Kind, int64_t Offset,
850b57cec5SDimitry Andric bool AllowTLS) const;
860b57cec5SDimitry Andric
getPC16DBLEncoding(const MCInst & MI,unsigned OpNum,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const870b57cec5SDimitry Andric uint64_t getPC16DBLEncoding(const MCInst &MI, unsigned OpNum,
880b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
890b57cec5SDimitry Andric const MCSubtargetInfo &STI) const {
900b57cec5SDimitry Andric return getPCRelEncoding(MI, OpNum, Fixups,
910b57cec5SDimitry Andric SystemZ::FK_390_PC16DBL, 2, false);
920b57cec5SDimitry Andric }
getPC32DBLEncoding(const MCInst & MI,unsigned OpNum,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const930b57cec5SDimitry Andric uint64_t getPC32DBLEncoding(const MCInst &MI, unsigned OpNum,
940b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
950b57cec5SDimitry Andric const MCSubtargetInfo &STI) const {
960b57cec5SDimitry Andric return getPCRelEncoding(MI, OpNum, Fixups,
970b57cec5SDimitry Andric SystemZ::FK_390_PC32DBL, 2, false);
980b57cec5SDimitry Andric }
getPC16DBLTLSEncoding(const MCInst & MI,unsigned OpNum,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const990b57cec5SDimitry Andric uint64_t getPC16DBLTLSEncoding(const MCInst &MI, unsigned OpNum,
1000b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
1010b57cec5SDimitry Andric const MCSubtargetInfo &STI) const {
1020b57cec5SDimitry Andric return getPCRelEncoding(MI, OpNum, Fixups,
1030b57cec5SDimitry Andric SystemZ::FK_390_PC16DBL, 2, true);
1040b57cec5SDimitry Andric }
getPC32DBLTLSEncoding(const MCInst & MI,unsigned OpNum,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const1050b57cec5SDimitry Andric uint64_t getPC32DBLTLSEncoding(const MCInst &MI, unsigned OpNum,
1060b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
1070b57cec5SDimitry Andric const MCSubtargetInfo &STI) const {
1080b57cec5SDimitry Andric return getPCRelEncoding(MI, OpNum, Fixups,
1090b57cec5SDimitry Andric SystemZ::FK_390_PC32DBL, 2, true);
1100b57cec5SDimitry Andric }
getPC12DBLBPPEncoding(const MCInst & MI,unsigned OpNum,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const1110b57cec5SDimitry Andric uint64_t getPC12DBLBPPEncoding(const MCInst &MI, unsigned OpNum,
1120b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
1130b57cec5SDimitry Andric const MCSubtargetInfo &STI) const {
1140b57cec5SDimitry Andric return getPCRelEncoding(MI, OpNum, Fixups,
1150b57cec5SDimitry Andric SystemZ::FK_390_PC12DBL, 1, false);
1160b57cec5SDimitry Andric }
getPC16DBLBPPEncoding(const MCInst & MI,unsigned OpNum,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const1170b57cec5SDimitry Andric uint64_t getPC16DBLBPPEncoding(const MCInst &MI, unsigned OpNum,
1180b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
1190b57cec5SDimitry Andric const MCSubtargetInfo &STI) const {
1200b57cec5SDimitry Andric return getPCRelEncoding(MI, OpNum, Fixups,
1210b57cec5SDimitry Andric SystemZ::FK_390_PC16DBL, 4, false);
1220b57cec5SDimitry Andric }
getPC24DBLBPPEncoding(const MCInst & MI,unsigned OpNum,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const1230b57cec5SDimitry Andric uint64_t getPC24DBLBPPEncoding(const MCInst &MI, unsigned OpNum,
1240b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
1250b57cec5SDimitry Andric const MCSubtargetInfo &STI) const {
1260b57cec5SDimitry Andric return getPCRelEncoding(MI, OpNum, Fixups,
1270b57cec5SDimitry Andric SystemZ::FK_390_PC24DBL, 3, false);
1280b57cec5SDimitry Andric }
1290b57cec5SDimitry Andric };
1300b57cec5SDimitry Andric
1310b57cec5SDimitry Andric } // end anonymous namespace
1320b57cec5SDimitry Andric
encodeInstruction(const MCInst & MI,SmallVectorImpl<char> & CB,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const13306c3fb27SDimitry Andric void SystemZMCCodeEmitter::encodeInstruction(const MCInst &MI,
13406c3fb27SDimitry Andric SmallVectorImpl<char> &CB,
1350b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
1360b57cec5SDimitry Andric const MCSubtargetInfo &STI) const {
1370b57cec5SDimitry Andric uint64_t Bits = getBinaryCodeForInstr(MI, Fixups, STI);
1380b57cec5SDimitry Andric unsigned Size = MCII.get(MI.getOpcode()).getSize();
1390b57cec5SDimitry Andric // Big-endian insertion of Size bytes.
1400b57cec5SDimitry Andric unsigned ShiftValue = (Size * 8) - 8;
1410b57cec5SDimitry Andric for (unsigned I = 0; I != Size; ++I) {
14206c3fb27SDimitry Andric CB.push_back(uint8_t(Bits >> ShiftValue));
1430b57cec5SDimitry Andric ShiftValue -= 8;
1440b57cec5SDimitry Andric }
1450b57cec5SDimitry Andric }
1460b57cec5SDimitry Andric
1470b57cec5SDimitry Andric uint64_t SystemZMCCodeEmitter::
getMachineOpValue(const MCInst & MI,const MCOperand & MO,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const1480b57cec5SDimitry Andric getMachineOpValue(const MCInst &MI, const MCOperand &MO,
1490b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
1500b57cec5SDimitry Andric const MCSubtargetInfo &STI) const {
1510b57cec5SDimitry Andric if (MO.isReg())
1520b57cec5SDimitry Andric return Ctx.getRegisterInfo()->getEncodingValue(MO.getReg());
15306c3fb27SDimitry Andric // SystemZAsmParser::parseAnyRegister() produces KindImm when registers are
15406c3fb27SDimitry Andric // specified as integers.
1550b57cec5SDimitry Andric if (MO.isImm())
1560b57cec5SDimitry Andric return static_cast<uint64_t>(MO.getImm());
1570b57cec5SDimitry Andric llvm_unreachable("Unexpected operand type!");
1580b57cec5SDimitry Andric }
1590b57cec5SDimitry Andric
16006c3fb27SDimitry Andric template <SystemZ::FixupKind Kind>
getImmOpValue(const MCInst & MI,unsigned OpNum,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const16106c3fb27SDimitry Andric uint64_t SystemZMCCodeEmitter::getImmOpValue(const MCInst &MI, unsigned OpNum,
162349cc55cSDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
16306c3fb27SDimitry Andric const MCSubtargetInfo &STI) const {
164349cc55cSDimitry Andric const MCOperand &MO = MI.getOperand(OpNum);
16506c3fb27SDimitry Andric if (MO.isImm())
166349cc55cSDimitry Andric return static_cast<uint64_t>(MO.getImm());
167349cc55cSDimitry Andric if (MO.isExpr()) {
16806c3fb27SDimitry Andric unsigned MIBitSize = MCII.get(MI.getOpcode()).getSize() * 8;
16906c3fb27SDimitry Andric uint32_t RawBitOffset = getOperandBitOffset(MI, OpNum, STI);
17006c3fb27SDimitry Andric unsigned OpBitSize =
17106c3fb27SDimitry Andric SystemZ::MCFixupKindInfos[Kind - FirstTargetFixupKind].TargetSize;
17206c3fb27SDimitry Andric uint32_t BitOffset = MIBitSize - RawBitOffset - OpBitSize;
17306c3fb27SDimitry Andric Fixups.push_back(MCFixup::create(BitOffset >> 3, MO.getExpr(),
17406c3fb27SDimitry Andric (MCFixupKind)Kind, MI.getLoc()));
175349cc55cSDimitry Andric return 0;
176349cc55cSDimitry Andric }
177349cc55cSDimitry Andric llvm_unreachable("Unexpected operand type!");
178349cc55cSDimitry Andric }
179349cc55cSDimitry Andric
18006c3fb27SDimitry Andric template <SystemZ::FixupKind Kind>
18106c3fb27SDimitry Andric uint64_t
getLenEncoding(const MCInst & MI,unsigned OpNum,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const18206c3fb27SDimitry Andric SystemZMCCodeEmitter::getLenEncoding(const MCInst &MI, unsigned OpNum,
1830b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
1840b57cec5SDimitry Andric const MCSubtargetInfo &STI) const {
18506c3fb27SDimitry Andric return getImmOpValue<Kind>(MI, OpNum, Fixups, STI) - 1;
1860b57cec5SDimitry Andric }
1870b57cec5SDimitry Andric
1880b57cec5SDimitry Andric uint64_t
getPCRelEncoding(const MCInst & MI,unsigned OpNum,SmallVectorImpl<MCFixup> & Fixups,unsigned Kind,int64_t Offset,bool AllowTLS) const1890b57cec5SDimitry Andric SystemZMCCodeEmitter::getPCRelEncoding(const MCInst &MI, unsigned OpNum,
1900b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
1910b57cec5SDimitry Andric unsigned Kind, int64_t Offset,
1920b57cec5SDimitry Andric bool AllowTLS) const {
1930eae32dcSDimitry Andric SMLoc Loc = MI.getLoc();
1940b57cec5SDimitry Andric const MCOperand &MO = MI.getOperand(OpNum);
1950b57cec5SDimitry Andric const MCExpr *Expr;
1960b57cec5SDimitry Andric if (MO.isImm())
1970b57cec5SDimitry Andric Expr = MCConstantExpr::create(MO.getImm() + Offset, Ctx);
1980b57cec5SDimitry Andric else {
1990b57cec5SDimitry Andric Expr = MO.getExpr();
2000b57cec5SDimitry Andric if (Offset) {
2010b57cec5SDimitry Andric // The operand value is relative to the start of MI, but the fixup
2020b57cec5SDimitry Andric // is relative to the operand field itself, which is Offset bytes
2030b57cec5SDimitry Andric // into MI. Add Offset to the relocation value to cancel out
2040b57cec5SDimitry Andric // this difference.
2050b57cec5SDimitry Andric const MCExpr *OffsetExpr = MCConstantExpr::create(Offset, Ctx);
2060b57cec5SDimitry Andric Expr = MCBinaryExpr::createAdd(Expr, OffsetExpr, Ctx);
2070b57cec5SDimitry Andric }
2080b57cec5SDimitry Andric }
2090eae32dcSDimitry Andric Fixups.push_back(MCFixup::create(Offset, Expr, (MCFixupKind)Kind, Loc));
2100b57cec5SDimitry Andric
2110b57cec5SDimitry Andric // Output the fixup for the TLS marker if present.
2120b57cec5SDimitry Andric if (AllowTLS && OpNum + 1 < MI.getNumOperands()) {
2130b57cec5SDimitry Andric const MCOperand &MOTLS = MI.getOperand(OpNum + 1);
2140eae32dcSDimitry Andric Fixups.push_back(MCFixup::create(
2150eae32dcSDimitry Andric 0, MOTLS.getExpr(), (MCFixupKind)SystemZ::FK_390_TLS_CALL, Loc));
2160b57cec5SDimitry Andric }
2170b57cec5SDimitry Andric return 0;
2180b57cec5SDimitry Andric }
2190b57cec5SDimitry Andric
22006c3fb27SDimitry Andric #define GET_OPERAND_BIT_OFFSET
2210b57cec5SDimitry Andric #include "SystemZGenMCCodeEmitter.inc"
2220b57cec5SDimitry Andric
createSystemZMCCodeEmitter(const MCInstrInfo & MCII,MCContext & Ctx)2230b57cec5SDimitry Andric MCCodeEmitter *llvm::createSystemZMCCodeEmitter(const MCInstrInfo &MCII,
2240b57cec5SDimitry Andric MCContext &Ctx) {
2250b57cec5SDimitry Andric return new SystemZMCCodeEmitter(MCII, Ctx);
2260b57cec5SDimitry Andric }
227