2014-05-24 20:50:23 +08:00
|
|
|
//=- AArch64/AArch64MCCodeEmitter.cpp - Convert AArch64 code to machine code-=//
|
2014-03-29 18:18:08 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2014-05-24 20:50:23 +08:00
|
|
|
// This file implements the AArch64MCCodeEmitter class.
|
2014-03-29 18:18:08 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-05-24 20:50:23 +08:00
|
|
|
#include "MCTargetDesc/AArch64AddressingModes.h"
|
|
|
|
#include "MCTargetDesc/AArch64FixupKinds.h"
|
|
|
|
#include "MCTargetDesc/AArch64MCExpr.h"
|
|
|
|
#include "Utils/AArch64BaseInfo.h"
|
2017-01-06 08:30:53 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2014-07-25 19:42:14 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2014-03-29 18:18:08 +08:00
|
|
|
#include "llvm/MC/MCCodeEmitter.h"
|
|
|
|
#include "llvm/MC/MCContext.h"
|
2017-01-06 08:30:53 +08:00
|
|
|
#include "llvm/MC/MCFixup.h"
|
2014-03-29 18:18:08 +08:00
|
|
|
#include "llvm/MC/MCInst.h"
|
|
|
|
#include "llvm/MC/MCInstrInfo.h"
|
|
|
|
#include "llvm/MC/MCRegisterInfo.h"
|
|
|
|
#include "llvm/MC/MCSubtargetInfo.h"
|
2017-01-06 08:30:53 +08:00
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/Support/Endian.h"
|
2015-06-04 23:03:02 +08:00
|
|
|
#include "llvm/Support/EndianStream.h"
|
2017-01-06 08:30:53 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2014-03-29 18:18:08 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-01-06 08:30:53 +08:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstdint>
|
|
|
|
|
2014-03-29 18:18:08 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:41:26 +08:00
|
|
|
#define DEBUG_TYPE "mccodeemitter"
|
|
|
|
|
2014-03-29 18:18:08 +08:00
|
|
|
STATISTIC(MCNumEmitted, "Number of MC instructions emitted.");
|
|
|
|
STATISTIC(MCNumFixups, "Number of MC fixups created.");
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2014-05-24 20:50:23 +08:00
|
|
|
class AArch64MCCodeEmitter : public MCCodeEmitter {
|
2014-03-29 18:18:08 +08:00
|
|
|
MCContext &Ctx;
|
Check that emitted instructions meet their predicates on all targets except ARM, Mips, and X86.
Summary:
* ARM is omitted from this patch because this check appears to expose bugs in this target.
* Mips is omitted from this patch because this check either detects bugs or deliberate
emission of instructions that don't satisfy their predicates. One deliberate
use is the SYNC instruction where the version with an operand is correctly
defined as requiring MIPS32 while the version without an operand is defined
as an alias of 'SYNC 0' and requires MIPS2.
* X86 is omitted from this patch because it doesn't use the tablegen-erated
MCCodeEmitter infrastructure.
Patches for ARM and Mips will follow.
Depends on D25617
Reviewers: tstellarAMD, jmolloy
Subscribers: wdng, jmolloy, aemerson, rengolin, arsenm, jyknight, nemanjai, nhaehnle, tstellarAMD, llvm-commits
Differential Revision: https://reviews.llvm.org/D25618
llvm-svn: 287439
2016-11-19 21:05:44 +08:00
|
|
|
const MCInstrInfo &MCII;
|
2014-03-29 18:18:08 +08:00
|
|
|
|
|
|
|
public:
|
Check that emitted instructions meet their predicates on all targets except ARM, Mips, and X86.
Summary:
* ARM is omitted from this patch because this check appears to expose bugs in this target.
* Mips is omitted from this patch because this check either detects bugs or deliberate
emission of instructions that don't satisfy their predicates. One deliberate
use is the SYNC instruction where the version with an operand is correctly
defined as requiring MIPS32 while the version without an operand is defined
as an alias of 'SYNC 0' and requires MIPS2.
* X86 is omitted from this patch because it doesn't use the tablegen-erated
MCCodeEmitter infrastructure.
Patches for ARM and Mips will follow.
Depends on D25617
Reviewers: tstellarAMD, jmolloy
Subscribers: wdng, jmolloy, aemerson, rengolin, arsenm, jyknight, nemanjai, nhaehnle, tstellarAMD, llvm-commits
Differential Revision: https://reviews.llvm.org/D25618
llvm-svn: 287439
2016-11-19 21:05:44 +08:00
|
|
|
AArch64MCCodeEmitter(const MCInstrInfo &mcii, MCContext &ctx)
|
|
|
|
: Ctx(ctx), MCII(mcii) {}
|
2017-01-06 08:30:53 +08:00
|
|
|
AArch64MCCodeEmitter(const AArch64MCCodeEmitter &) = delete;
|
|
|
|
void operator=(const AArch64MCCodeEmitter &) = delete;
|
|
|
|
~AArch64MCCodeEmitter() override = default;
|
2014-03-29 18:18:08 +08:00
|
|
|
|
|
|
|
// getBinaryCodeForInstr - TableGen'erated function for getting the
|
|
|
|
// binary encoding for an instruction.
|
|
|
|
uint64_t getBinaryCodeForInstr(const MCInst &MI,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const;
|
|
|
|
|
|
|
|
/// getMachineOpValue - Return binary encoding of operand. If the machine
|
|
|
|
/// operand requires relocation, record the relocation and return zero.
|
|
|
|
unsigned getMachineOpValue(const MCInst &MI, const MCOperand &MO,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const;
|
|
|
|
|
2014-05-22 19:56:09 +08:00
|
|
|
/// getLdStUImm12OpValue - Return encoding info for 12-bit unsigned immediate
|
|
|
|
/// attached to a load, store or prfm instruction. If operand requires a
|
|
|
|
/// relocation, record it and return zero in that part of the encoding.
|
2014-03-29 18:18:08 +08:00
|
|
|
template <uint32_t FixupKind>
|
2014-05-22 19:56:09 +08:00
|
|
|
uint32_t getLdStUImm12OpValue(const MCInst &MI, unsigned OpIdx,
|
2014-03-29 18:18:08 +08:00
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const;
|
|
|
|
|
|
|
|
/// getAdrLabelOpValue - Return encoding info for 21-bit immediate ADR label
|
|
|
|
/// target.
|
|
|
|
uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const;
|
|
|
|
|
|
|
|
/// getAddSubImmOpValue - Return encoding for the 12-bit immediate value and
|
|
|
|
/// the 2-bit shift field.
|
|
|
|
uint32_t getAddSubImmOpValue(const MCInst &MI, unsigned OpIdx,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const;
|
|
|
|
|
|
|
|
/// getCondBranchTargetOpValue - Return the encoded value for a conditional
|
|
|
|
/// branch target.
|
|
|
|
uint32_t getCondBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const;
|
|
|
|
|
2014-04-24 20:12:10 +08:00
|
|
|
/// getLoadLiteralOpValue - Return the encoded value for a load-literal
|
|
|
|
/// pc-relative address.
|
|
|
|
uint32_t getLoadLiteralOpValue(const MCInst &MI, unsigned OpIdx,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const;
|
|
|
|
|
2014-05-22 19:56:09 +08:00
|
|
|
/// getMemExtendOpValue - Return the encoded value for a reg-extend load/store
|
|
|
|
/// instruction: bit 0 is whether a shift is present, bit 1 is whether the
|
|
|
|
/// operation is a sign extend (as opposed to a zero extend).
|
|
|
|
uint32_t getMemExtendOpValue(const MCInst &MI, unsigned OpIdx,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const;
|
|
|
|
|
2014-03-29 18:18:08 +08:00
|
|
|
/// getTestBranchTargetOpValue - Return the encoded value for a test-bit-and-
|
|
|
|
/// branch target.
|
|
|
|
uint32_t getTestBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const;
|
|
|
|
|
|
|
|
/// getBranchTargetOpValue - Return the encoded value for an unconditional
|
|
|
|
/// branch target.
|
|
|
|
uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const;
|
|
|
|
|
|
|
|
/// getMoveWideImmOpValue - Return the encoded value for the immediate operand
|
|
|
|
/// of a MOVZ or MOVK instruction.
|
|
|
|
uint32_t getMoveWideImmOpValue(const MCInst &MI, unsigned OpIdx,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const;
|
|
|
|
|
|
|
|
/// getVecShifterOpValue - Return the encoded value for the vector shifter.
|
|
|
|
uint32_t getVecShifterOpValue(const MCInst &MI, unsigned OpIdx,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const;
|
|
|
|
|
|
|
|
/// getMoveVecShifterOpValue - Return the encoded value for the vector move
|
|
|
|
/// shifter (MSL).
|
|
|
|
uint32_t getMoveVecShifterOpValue(const MCInst &MI, unsigned OpIdx,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const;
|
|
|
|
|
|
|
|
/// getFixedPointScaleOpValue - Return the encoded value for the
|
|
|
|
// FP-to-fixed-point scale factor.
|
|
|
|
uint32_t getFixedPointScaleOpValue(const MCInst &MI, unsigned OpIdx,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const;
|
|
|
|
|
|
|
|
uint32_t getVecShiftR64OpValue(const MCInst &MI, unsigned OpIdx,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const;
|
|
|
|
uint32_t getVecShiftR32OpValue(const MCInst &MI, unsigned OpIdx,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const;
|
|
|
|
uint32_t getVecShiftR16OpValue(const MCInst &MI, unsigned OpIdx,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const;
|
|
|
|
uint32_t getVecShiftR8OpValue(const MCInst &MI, unsigned OpIdx,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const;
|
|
|
|
uint32_t getVecShiftL64OpValue(const MCInst &MI, unsigned OpIdx,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const;
|
|
|
|
uint32_t getVecShiftL32OpValue(const MCInst &MI, unsigned OpIdx,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const;
|
|
|
|
uint32_t getVecShiftL16OpValue(const MCInst &MI, unsigned OpIdx,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const;
|
|
|
|
uint32_t getVecShiftL8OpValue(const MCInst &MI, unsigned OpIdx,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const;
|
|
|
|
|
|
|
|
unsigned fixMOVZ(const MCInst &MI, unsigned EncodedValue,
|
|
|
|
const MCSubtargetInfo &STI) const;
|
|
|
|
|
2015-05-16 03:13:16 +08:00
|
|
|
void encodeInstruction(const MCInst &MI, raw_ostream &OS,
|
2014-03-29 18:18:08 +08:00
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
2014-04-29 15:58:25 +08:00
|
|
|
const MCSubtargetInfo &STI) const override;
|
2014-04-09 22:43:01 +08:00
|
|
|
|
2014-04-09 22:43:15 +08:00
|
|
|
unsigned fixMulHigh(const MCInst &MI, unsigned EncodedValue,
|
|
|
|
const MCSubtargetInfo &STI) const;
|
|
|
|
|
2014-04-09 22:43:01 +08:00
|
|
|
template<int hasRs, int hasRt2> unsigned
|
|
|
|
fixLoadStoreExclusive(const MCInst &MI, unsigned EncodedValue,
|
|
|
|
const MCSubtargetInfo &STI) const;
|
|
|
|
|
2014-04-09 22:43:40 +08:00
|
|
|
unsigned fixOneOperandFPComparison(const MCInst &MI, unsigned EncodedValue,
|
|
|
|
const MCSubtargetInfo &STI) const;
|
Check that emitted instructions meet their predicates on all targets except ARM, Mips, and X86.
Summary:
* ARM is omitted from this patch because this check appears to expose bugs in this target.
* Mips is omitted from this patch because this check either detects bugs or deliberate
emission of instructions that don't satisfy their predicates. One deliberate
use is the SYNC instruction where the version with an operand is correctly
defined as requiring MIPS32 while the version without an operand is defined
as an alias of 'SYNC 0' and requires MIPS2.
* X86 is omitted from this patch because it doesn't use the tablegen-erated
MCCodeEmitter infrastructure.
Patches for ARM and Mips will follow.
Depends on D25617
Reviewers: tstellarAMD, jmolloy
Subscribers: wdng, jmolloy, aemerson, rengolin, arsenm, jyknight, nemanjai, nhaehnle, tstellarAMD, llvm-commits
Differential Revision: https://reviews.llvm.org/D25618
llvm-svn: 287439
2016-11-19 21:05:44 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
uint64_t computeAvailableFeatures(const FeatureBitset &FB) const;
|
|
|
|
void verifyInstructionPredicates(const MCInst &MI,
|
|
|
|
uint64_t AvailableFeatures) const;
|
2014-03-29 18:18:08 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
/// getMachineOpValue - Return binary encoding of operand. If the machine
|
|
|
|
/// operand requires relocation, record the relocation and return zero.
|
|
|
|
unsigned
|
2014-05-24 20:50:23 +08:00
|
|
|
AArch64MCCodeEmitter::getMachineOpValue(const MCInst &MI, const MCOperand &MO,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const {
|
2014-03-29 18:18:08 +08:00
|
|
|
if (MO.isReg())
|
|
|
|
return Ctx.getRegisterInfo()->getEncodingValue(MO.getReg());
|
|
|
|
|
2014-06-19 14:10:58 +08:00
|
|
|
assert(MO.isImm() && "did not expect relocated expression");
|
|
|
|
return static_cast<unsigned>(MO.getImm());
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
|
|
|
|
2014-05-22 19:56:09 +08:00
|
|
|
template<unsigned FixupKind> uint32_t
|
2014-05-24 20:50:23 +08:00
|
|
|
AArch64MCCodeEmitter::getLdStUImm12OpValue(const MCInst &MI, unsigned OpIdx,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const {
|
2014-05-22 19:56:09 +08:00
|
|
|
const MCOperand &MO = MI.getOperand(OpIdx);
|
2014-03-29 18:18:08 +08:00
|
|
|
uint32_t ImmVal = 0;
|
|
|
|
|
|
|
|
if (MO.isImm())
|
|
|
|
ImmVal = static_cast<uint32_t>(MO.getImm());
|
|
|
|
else {
|
|
|
|
assert(MO.isExpr() && "unable to encode load/store imm operand");
|
|
|
|
MCFixupKind Kind = MCFixupKind(FixupKind);
|
2015-05-16 03:13:05 +08:00
|
|
|
Fixups.push_back(MCFixup::create(0, MO.getExpr(), Kind, MI.getLoc()));
|
2014-03-29 18:18:08 +08:00
|
|
|
++MCNumFixups;
|
|
|
|
}
|
|
|
|
|
2014-05-22 19:56:09 +08:00
|
|
|
return ImmVal;
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// getAdrLabelOpValue - Return encoding info for 21-bit immediate ADR label
|
|
|
|
/// target.
|
|
|
|
uint32_t
|
2014-05-24 20:50:23 +08:00
|
|
|
AArch64MCCodeEmitter::getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const {
|
2014-03-29 18:18:08 +08:00
|
|
|
const MCOperand &MO = MI.getOperand(OpIdx);
|
|
|
|
|
|
|
|
// If the destination is an immediate, we have nothing to do.
|
|
|
|
if (MO.isImm())
|
|
|
|
return MO.getImm();
|
2014-05-22 19:56:09 +08:00
|
|
|
assert(MO.isExpr() && "Unexpected target type!");
|
2014-03-29 18:18:08 +08:00
|
|
|
const MCExpr *Expr = MO.getExpr();
|
|
|
|
|
2014-05-24 20:50:23 +08:00
|
|
|
MCFixupKind Kind = MI.getOpcode() == AArch64::ADR
|
|
|
|
? MCFixupKind(AArch64::fixup_aarch64_pcrel_adr_imm21)
|
|
|
|
: MCFixupKind(AArch64::fixup_aarch64_pcrel_adrp_imm21);
|
2015-05-16 03:13:05 +08:00
|
|
|
Fixups.push_back(MCFixup::create(0, Expr, Kind, MI.getLoc()));
|
2014-03-29 18:18:08 +08:00
|
|
|
|
|
|
|
MCNumFixups += 1;
|
|
|
|
|
|
|
|
// All of the information is in the fixup.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getAddSubImmOpValue - Return encoding for the 12-bit immediate value and
|
|
|
|
/// the 2-bit shift field. The shift field is stored in bits 13-14 of the
|
|
|
|
/// return value.
|
|
|
|
uint32_t
|
2014-05-24 20:50:23 +08:00
|
|
|
AArch64MCCodeEmitter::getAddSubImmOpValue(const MCInst &MI, unsigned OpIdx,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const {
|
2014-03-29 18:18:08 +08:00
|
|
|
// Suboperands are [imm, shifter].
|
|
|
|
const MCOperand &MO = MI.getOperand(OpIdx);
|
|
|
|
const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
|
2014-05-24 20:50:23 +08:00
|
|
|
assert(AArch64_AM::getShiftType(MO1.getImm()) == AArch64_AM::LSL &&
|
2014-03-29 18:18:08 +08:00
|
|
|
"unexpected shift type for add/sub immediate");
|
2014-05-24 20:50:23 +08:00
|
|
|
unsigned ShiftVal = AArch64_AM::getShiftValue(MO1.getImm());
|
2014-03-29 18:18:08 +08:00
|
|
|
assert((ShiftVal == 0 || ShiftVal == 12) &&
|
|
|
|
"unexpected shift value for add/sub immediate");
|
|
|
|
if (MO.isImm())
|
2016-09-19 19:10:18 +08:00
|
|
|
return MO.getImm() | (ShiftVal == 0 ? 0 : (1 << ShiftVal));
|
2014-03-29 18:18:08 +08:00
|
|
|
assert(MO.isExpr() && "Unable to encode MCOperand!");
|
|
|
|
const MCExpr *Expr = MO.getExpr();
|
|
|
|
|
|
|
|
// Encode the 12 bits of the fixup.
|
2014-05-24 20:50:23 +08:00
|
|
|
MCFixupKind Kind = MCFixupKind(AArch64::fixup_aarch64_add_imm12);
|
2015-05-16 03:13:05 +08:00
|
|
|
Fixups.push_back(MCFixup::create(0, Expr, Kind, MI.getLoc()));
|
2014-03-29 18:18:08 +08:00
|
|
|
|
|
|
|
++MCNumFixups;
|
|
|
|
|
2016-09-29 09:05:48 +08:00
|
|
|
// Set the shift bit of the add instruction for relocation types
|
|
|
|
// R_AARCH64_TLSLE_ADD_TPREL_HI12 and R_AARCH64_TLSLD_ADD_DTPREL_HI12.
|
|
|
|
if (const AArch64MCExpr *A64E = dyn_cast<AArch64MCExpr>(Expr)) {
|
|
|
|
AArch64MCExpr::VariantKind RefKind = A64E->getKind();
|
|
|
|
if (RefKind == AArch64MCExpr::VK_TPREL_HI12 ||
|
|
|
|
RefKind == AArch64MCExpr::VK_DTPREL_HI12)
|
|
|
|
ShiftVal = 12;
|
|
|
|
}
|
2016-09-19 19:10:18 +08:00
|
|
|
return ShiftVal == 0 ? 0 : (1 << ShiftVal);
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// getCondBranchTargetOpValue - Return the encoded value for a conditional
|
|
|
|
/// branch target.
|
2014-05-24 20:50:23 +08:00
|
|
|
uint32_t AArch64MCCodeEmitter::getCondBranchTargetOpValue(
|
2014-03-29 18:18:08 +08:00
|
|
|
const MCInst &MI, unsigned OpIdx, SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const {
|
|
|
|
const MCOperand &MO = MI.getOperand(OpIdx);
|
|
|
|
|
|
|
|
// If the destination is an immediate, we have nothing to do.
|
|
|
|
if (MO.isImm())
|
|
|
|
return MO.getImm();
|
|
|
|
assert(MO.isExpr() && "Unexpected target type!");
|
|
|
|
|
2014-05-24 20:50:23 +08:00
|
|
|
MCFixupKind Kind = MCFixupKind(AArch64::fixup_aarch64_pcrel_branch19);
|
2015-05-16 03:13:05 +08:00
|
|
|
Fixups.push_back(MCFixup::create(0, MO.getExpr(), Kind, MI.getLoc()));
|
2014-04-24 20:12:10 +08:00
|
|
|
|
|
|
|
++MCNumFixups;
|
|
|
|
|
|
|
|
// All of the information is in the fixup.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getLoadLiteralOpValue - Return the encoded value for a load-literal
|
|
|
|
/// pc-relative address.
|
|
|
|
uint32_t
|
2014-05-24 20:50:23 +08:00
|
|
|
AArch64MCCodeEmitter::getLoadLiteralOpValue(const MCInst &MI, unsigned OpIdx,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const {
|
2014-04-24 20:12:10 +08:00
|
|
|
const MCOperand &MO = MI.getOperand(OpIdx);
|
|
|
|
|
|
|
|
// If the destination is an immediate, we have nothing to do.
|
|
|
|
if (MO.isImm())
|
|
|
|
return MO.getImm();
|
|
|
|
assert(MO.isExpr() && "Unexpected target type!");
|
|
|
|
|
2014-05-24 20:50:23 +08:00
|
|
|
MCFixupKind Kind = MCFixupKind(AArch64::fixup_aarch64_ldr_pcrel_imm19);
|
2015-05-16 03:13:05 +08:00
|
|
|
Fixups.push_back(MCFixup::create(0, MO.getExpr(), Kind, MI.getLoc()));
|
2014-03-29 18:18:08 +08:00
|
|
|
|
|
|
|
++MCNumFixups;
|
|
|
|
|
|
|
|
// All of the information is in the fixup.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-22 19:56:09 +08:00
|
|
|
uint32_t
|
2014-05-24 20:50:23 +08:00
|
|
|
AArch64MCCodeEmitter::getMemExtendOpValue(const MCInst &MI, unsigned OpIdx,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const {
|
2014-05-22 19:56:09 +08:00
|
|
|
unsigned SignExtend = MI.getOperand(OpIdx).getImm();
|
|
|
|
unsigned DoShift = MI.getOperand(OpIdx + 1).getImm();
|
|
|
|
return (SignExtend << 1) | DoShift;
|
|
|
|
}
|
|
|
|
|
2014-03-29 18:18:08 +08:00
|
|
|
uint32_t
|
2014-05-24 20:50:23 +08:00
|
|
|
AArch64MCCodeEmitter::getMoveWideImmOpValue(const MCInst &MI, unsigned OpIdx,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const {
|
2014-03-29 18:18:08 +08:00
|
|
|
const MCOperand &MO = MI.getOperand(OpIdx);
|
|
|
|
|
|
|
|
if (MO.isImm())
|
|
|
|
return MO.getImm();
|
|
|
|
assert(MO.isExpr() && "Unexpected movz/movk immediate");
|
|
|
|
|
2015-05-16 03:13:05 +08:00
|
|
|
Fixups.push_back(MCFixup::create(
|
2014-05-24 20:50:23 +08:00
|
|
|
0, MO.getExpr(), MCFixupKind(AArch64::fixup_aarch64_movw), MI.getLoc()));
|
2014-03-29 18:18:08 +08:00
|
|
|
|
|
|
|
++MCNumFixups;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getTestBranchTargetOpValue - Return the encoded value for a test-bit-and-
|
|
|
|
/// branch target.
|
2014-05-24 20:50:23 +08:00
|
|
|
uint32_t AArch64MCCodeEmitter::getTestBranchTargetOpValue(
|
2014-03-29 18:18:08 +08:00
|
|
|
const MCInst &MI, unsigned OpIdx, SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const {
|
|
|
|
const MCOperand &MO = MI.getOperand(OpIdx);
|
|
|
|
|
|
|
|
// If the destination is an immediate, we have nothing to do.
|
|
|
|
if (MO.isImm())
|
|
|
|
return MO.getImm();
|
|
|
|
assert(MO.isExpr() && "Unexpected ADR target type!");
|
|
|
|
|
2014-05-24 20:50:23 +08:00
|
|
|
MCFixupKind Kind = MCFixupKind(AArch64::fixup_aarch64_pcrel_branch14);
|
2015-05-16 03:13:05 +08:00
|
|
|
Fixups.push_back(MCFixup::create(0, MO.getExpr(), Kind, MI.getLoc()));
|
2014-03-29 18:18:08 +08:00
|
|
|
|
|
|
|
++MCNumFixups;
|
|
|
|
|
|
|
|
// All of the information is in the fixup.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getBranchTargetOpValue - Return the encoded value for an unconditional
|
|
|
|
/// branch target.
|
|
|
|
uint32_t
|
2014-05-24 20:50:23 +08:00
|
|
|
AArch64MCCodeEmitter::getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const {
|
2014-03-29 18:18:08 +08:00
|
|
|
const MCOperand &MO = MI.getOperand(OpIdx);
|
|
|
|
|
|
|
|
// If the destination is an immediate, we have nothing to do.
|
|
|
|
if (MO.isImm())
|
|
|
|
return MO.getImm();
|
|
|
|
assert(MO.isExpr() && "Unexpected ADR target type!");
|
|
|
|
|
2014-05-24 20:50:23 +08:00
|
|
|
MCFixupKind Kind = MI.getOpcode() == AArch64::BL
|
|
|
|
? MCFixupKind(AArch64::fixup_aarch64_pcrel_call26)
|
|
|
|
: MCFixupKind(AArch64::fixup_aarch64_pcrel_branch26);
|
2015-05-16 03:13:05 +08:00
|
|
|
Fixups.push_back(MCFixup::create(0, MO.getExpr(), Kind, MI.getLoc()));
|
2014-03-29 18:18:08 +08:00
|
|
|
|
|
|
|
++MCNumFixups;
|
|
|
|
|
|
|
|
// All of the information is in the fixup.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getVecShifterOpValue - Return the encoded value for the vector shifter:
|
|
|
|
///
|
|
|
|
/// 00 -> 0
|
|
|
|
/// 01 -> 8
|
|
|
|
/// 10 -> 16
|
|
|
|
/// 11 -> 24
|
|
|
|
uint32_t
|
2014-05-24 20:50:23 +08:00
|
|
|
AArch64MCCodeEmitter::getVecShifterOpValue(const MCInst &MI, unsigned OpIdx,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const {
|
2014-03-29 18:18:08 +08:00
|
|
|
const MCOperand &MO = MI.getOperand(OpIdx);
|
|
|
|
assert(MO.isImm() && "Expected an immediate value for the shift amount!");
|
|
|
|
|
|
|
|
switch (MO.getImm()) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
return 0;
|
|
|
|
case 8:
|
|
|
|
return 1;
|
|
|
|
case 16:
|
|
|
|
return 2;
|
|
|
|
case 24:
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2015-01-05 18:15:49 +08:00
|
|
|
llvm_unreachable("Invalid value for vector shift amount!");
|
2014-03-29 18:18:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// getFixedPointScaleOpValue - Return the encoded value for the
|
|
|
|
// FP-to-fixed-point scale factor.
|
2014-05-24 20:50:23 +08:00
|
|
|
uint32_t AArch64MCCodeEmitter::getFixedPointScaleOpValue(
|
2014-03-29 18:18:08 +08:00
|
|
|
const MCInst &MI, unsigned OpIdx, SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const {
|
|
|
|
const MCOperand &MO = MI.getOperand(OpIdx);
|
|
|
|
assert(MO.isImm() && "Expected an immediate value for the scale amount!");
|
|
|
|
return 64 - MO.getImm();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
2014-05-24 20:50:23 +08:00
|
|
|
AArch64MCCodeEmitter::getVecShiftR64OpValue(const MCInst &MI, unsigned OpIdx,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const {
|
2014-03-29 18:18:08 +08:00
|
|
|
const MCOperand &MO = MI.getOperand(OpIdx);
|
|
|
|
assert(MO.isImm() && "Expected an immediate value for the scale amount!");
|
|
|
|
return 64 - MO.getImm();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
2014-05-24 20:50:23 +08:00
|
|
|
AArch64MCCodeEmitter::getVecShiftR32OpValue(const MCInst &MI, unsigned OpIdx,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const {
|
2014-03-29 18:18:08 +08:00
|
|
|
const MCOperand &MO = MI.getOperand(OpIdx);
|
|
|
|
assert(MO.isImm() && "Expected an immediate value for the scale amount!");
|
|
|
|
return 32 - MO.getImm();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
2014-05-24 20:50:23 +08:00
|
|
|
AArch64MCCodeEmitter::getVecShiftR16OpValue(const MCInst &MI, unsigned OpIdx,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const {
|
2014-03-29 18:18:08 +08:00
|
|
|
const MCOperand &MO = MI.getOperand(OpIdx);
|
|
|
|
assert(MO.isImm() && "Expected an immediate value for the scale amount!");
|
|
|
|
return 16 - MO.getImm();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
2014-05-24 20:50:23 +08:00
|
|
|
AArch64MCCodeEmitter::getVecShiftR8OpValue(const MCInst &MI, unsigned OpIdx,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const {
|
2014-03-29 18:18:08 +08:00
|
|
|
const MCOperand &MO = MI.getOperand(OpIdx);
|
|
|
|
assert(MO.isImm() && "Expected an immediate value for the scale amount!");
|
|
|
|
return 8 - MO.getImm();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
2014-05-24 20:50:23 +08:00
|
|
|
AArch64MCCodeEmitter::getVecShiftL64OpValue(const MCInst &MI, unsigned OpIdx,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const {
|
2014-03-29 18:18:08 +08:00
|
|
|
const MCOperand &MO = MI.getOperand(OpIdx);
|
|
|
|
assert(MO.isImm() && "Expected an immediate value for the scale amount!");
|
|
|
|
return MO.getImm() - 64;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
2014-05-24 20:50:23 +08:00
|
|
|
AArch64MCCodeEmitter::getVecShiftL32OpValue(const MCInst &MI, unsigned OpIdx,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const {
|
2014-03-29 18:18:08 +08:00
|
|
|
const MCOperand &MO = MI.getOperand(OpIdx);
|
|
|
|
assert(MO.isImm() && "Expected an immediate value for the scale amount!");
|
|
|
|
return MO.getImm() - 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
2014-05-24 20:50:23 +08:00
|
|
|
AArch64MCCodeEmitter::getVecShiftL16OpValue(const MCInst &MI, unsigned OpIdx,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const {
|
2014-03-29 18:18:08 +08:00
|
|
|
const MCOperand &MO = MI.getOperand(OpIdx);
|
|
|
|
assert(MO.isImm() && "Expected an immediate value for the scale amount!");
|
|
|
|
return MO.getImm() - 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
2014-05-24 20:50:23 +08:00
|
|
|
AArch64MCCodeEmitter::getVecShiftL8OpValue(const MCInst &MI, unsigned OpIdx,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const {
|
2014-03-29 18:18:08 +08:00
|
|
|
const MCOperand &MO = MI.getOperand(OpIdx);
|
|
|
|
assert(MO.isImm() && "Expected an immediate value for the scale amount!");
|
|
|
|
return MO.getImm() - 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getMoveVecShifterOpValue - Return the encoded value for the vector move
|
|
|
|
/// shifter (MSL).
|
2014-05-24 20:50:23 +08:00
|
|
|
uint32_t AArch64MCCodeEmitter::getMoveVecShifterOpValue(
|
|
|
|
const MCInst &MI, unsigned OpIdx, SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const {
|
2014-03-29 18:18:08 +08:00
|
|
|
const MCOperand &MO = MI.getOperand(OpIdx);
|
|
|
|
assert(MO.isImm() &&
|
|
|
|
"Expected an immediate value for the move shift amount!");
|
2014-05-24 20:50:23 +08:00
|
|
|
unsigned ShiftVal = AArch64_AM::getShiftValue(MO.getImm());
|
2014-03-29 18:18:08 +08:00
|
|
|
assert((ShiftVal == 8 || ShiftVal == 16) && "Invalid shift amount!");
|
|
|
|
return ShiftVal == 8 ? 0 : 1;
|
|
|
|
}
|
|
|
|
|
2014-05-24 20:50:23 +08:00
|
|
|
unsigned AArch64MCCodeEmitter::fixMOVZ(const MCInst &MI, unsigned EncodedValue,
|
|
|
|
const MCSubtargetInfo &STI) const {
|
2014-03-29 18:18:08 +08:00
|
|
|
// If one of the signed fixup kinds is applied to a MOVZ instruction, the
|
|
|
|
// eventual result could be either a MOVZ or a MOVN. It's the MCCodeEmitter's
|
|
|
|
// job to ensure that any bits possibly affected by this are 0. This means we
|
|
|
|
// must zero out bit 30 (essentially emitting a MOVN).
|
|
|
|
MCOperand UImm16MO = MI.getOperand(1);
|
|
|
|
|
|
|
|
// Nothing to do if there's no fixup.
|
|
|
|
if (UImm16MO.isImm())
|
|
|
|
return EncodedValue;
|
|
|
|
|
2014-05-24 20:50:23 +08:00
|
|
|
const AArch64MCExpr *A64E = cast<AArch64MCExpr>(UImm16MO.getExpr());
|
2014-04-15 22:00:15 +08:00
|
|
|
switch (A64E->getKind()) {
|
2014-05-24 20:50:23 +08:00
|
|
|
case AArch64MCExpr::VK_DTPREL_G2:
|
|
|
|
case AArch64MCExpr::VK_DTPREL_G1:
|
|
|
|
case AArch64MCExpr::VK_DTPREL_G0:
|
|
|
|
case AArch64MCExpr::VK_GOTTPREL_G1:
|
|
|
|
case AArch64MCExpr::VK_TPREL_G2:
|
|
|
|
case AArch64MCExpr::VK_TPREL_G1:
|
|
|
|
case AArch64MCExpr::VK_TPREL_G0:
|
2014-04-15 22:00:15 +08:00
|
|
|
return EncodedValue & ~(1u << 30);
|
|
|
|
default:
|
|
|
|
// Nothing to do for an unsigned fixup.
|
|
|
|
return EncodedValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-29 18:18:08 +08:00
|
|
|
return EncodedValue & ~(1u << 30);
|
|
|
|
}
|
|
|
|
|
2015-05-16 03:13:16 +08:00
|
|
|
void AArch64MCCodeEmitter::encodeInstruction(const MCInst &MI, raw_ostream &OS,
|
2014-05-24 20:50:23 +08:00
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const {
|
Check that emitted instructions meet their predicates on all targets except ARM, Mips, and X86.
Summary:
* ARM is omitted from this patch because this check appears to expose bugs in this target.
* Mips is omitted from this patch because this check either detects bugs or deliberate
emission of instructions that don't satisfy their predicates. One deliberate
use is the SYNC instruction where the version with an operand is correctly
defined as requiring MIPS32 while the version without an operand is defined
as an alias of 'SYNC 0' and requires MIPS2.
* X86 is omitted from this patch because it doesn't use the tablegen-erated
MCCodeEmitter infrastructure.
Patches for ARM and Mips will follow.
Depends on D25617
Reviewers: tstellarAMD, jmolloy
Subscribers: wdng, jmolloy, aemerson, rengolin, arsenm, jyknight, nemanjai, nhaehnle, tstellarAMD, llvm-commits
Differential Revision: https://reviews.llvm.org/D25618
llvm-svn: 287439
2016-11-19 21:05:44 +08:00
|
|
|
verifyInstructionPredicates(MI,
|
|
|
|
computeAvailableFeatures(STI.getFeatureBits()));
|
|
|
|
|
2014-05-24 20:50:23 +08:00
|
|
|
if (MI.getOpcode() == AArch64::TLSDESCCALL) {
|
2014-03-29 18:18:08 +08:00
|
|
|
// This is a directive which applies an R_AARCH64_TLSDESC_CALL to the
|
|
|
|
// following (BLR) instruction. It doesn't emit any code itself so it
|
|
|
|
// doesn't go through the normal TableGenerated channels.
|
2014-05-24 20:50:23 +08:00
|
|
|
MCFixupKind Fixup = MCFixupKind(AArch64::fixup_aarch64_tlsdesc_call);
|
2015-05-16 03:13:05 +08:00
|
|
|
Fixups.push_back(MCFixup::create(0, MI.getOperand(0).getExpr(), Fixup));
|
2014-03-29 18:18:08 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t Binary = getBinaryCodeForInstr(MI, Fixups, STI);
|
2015-06-04 23:03:02 +08:00
|
|
|
support::endian::Writer<support::little>(OS).write<uint32_t>(Binary);
|
2014-03-29 18:18:08 +08:00
|
|
|
++MCNumEmitted; // Keep track of the # of mi's emitted.
|
|
|
|
}
|
|
|
|
|
2014-04-09 22:43:15 +08:00
|
|
|
unsigned
|
2014-05-24 20:50:23 +08:00
|
|
|
AArch64MCCodeEmitter::fixMulHigh(const MCInst &MI,
|
|
|
|
unsigned EncodedValue,
|
|
|
|
const MCSubtargetInfo &STI) const {
|
2014-04-09 22:43:15 +08:00
|
|
|
// The Ra field of SMULH and UMULH is unused: it should be assembled as 31
|
|
|
|
// (i.e. all bits 1) but is ignored by the processor.
|
|
|
|
EncodedValue |= 0x1f << 10;
|
|
|
|
return EncodedValue;
|
|
|
|
}
|
|
|
|
|
2014-04-09 22:43:01 +08:00
|
|
|
template<int hasRs, int hasRt2> unsigned
|
2014-05-24 20:50:23 +08:00
|
|
|
AArch64MCCodeEmitter::fixLoadStoreExclusive(const MCInst &MI,
|
|
|
|
unsigned EncodedValue,
|
|
|
|
const MCSubtargetInfo &STI) const {
|
2014-04-09 22:43:01 +08:00
|
|
|
if (!hasRs) EncodedValue |= 0x001F0000;
|
|
|
|
if (!hasRt2) EncodedValue |= 0x00007C00;
|
|
|
|
|
|
|
|
return EncodedValue;
|
|
|
|
}
|
|
|
|
|
2014-05-24 20:50:23 +08:00
|
|
|
unsigned AArch64MCCodeEmitter::fixOneOperandFPComparison(
|
|
|
|
const MCInst &MI, unsigned EncodedValue, const MCSubtargetInfo &STI) const {
|
2014-04-09 22:43:40 +08:00
|
|
|
// The Rm field of FCMP and friends is unused - it should be assembled
|
|
|
|
// as 0, but is ignored by the processor.
|
|
|
|
EncodedValue &= ~(0x1f << 16);
|
|
|
|
return EncodedValue;
|
|
|
|
}
|
|
|
|
|
Check that emitted instructions meet their predicates on all targets except ARM, Mips, and X86.
Summary:
* ARM is omitted from this patch because this check appears to expose bugs in this target.
* Mips is omitted from this patch because this check either detects bugs or deliberate
emission of instructions that don't satisfy their predicates. One deliberate
use is the SYNC instruction where the version with an operand is correctly
defined as requiring MIPS32 while the version without an operand is defined
as an alias of 'SYNC 0' and requires MIPS2.
* X86 is omitted from this patch because it doesn't use the tablegen-erated
MCCodeEmitter infrastructure.
Patches for ARM and Mips will follow.
Depends on D25617
Reviewers: tstellarAMD, jmolloy
Subscribers: wdng, jmolloy, aemerson, rengolin, arsenm, jyknight, nemanjai, nhaehnle, tstellarAMD, llvm-commits
Differential Revision: https://reviews.llvm.org/D25618
llvm-svn: 287439
2016-11-19 21:05:44 +08:00
|
|
|
#define ENABLE_INSTR_PREDICATE_VERIFIER
|
2014-05-24 20:50:23 +08:00
|
|
|
#include "AArch64GenMCCodeEmitter.inc"
|
2017-01-06 08:30:53 +08:00
|
|
|
|
|
|
|
MCCodeEmitter *llvm::createAArch64MCCodeEmitter(const MCInstrInfo &MCII,
|
|
|
|
const MCRegisterInfo &MRI,
|
|
|
|
MCContext &Ctx) {
|
|
|
|
return new AArch64MCCodeEmitter(MCII, Ctx);
|
|
|
|
}
|