Implement a basic MCCodeEmitter for PPC. This doesn't handle
fixups yet, and doesn't handle actually encoding operand values,
but this is enough for llc -show-mc-encoding to show the base
instruction encoding information, e.g.:
mflr r0 ; encoding: [0x7c,0x08,0x02,0xa6]
stw r0, 8(r1) ; encoding: [0x90,0x00,0x00,0x00]
stwu r1, -64(r1) ; encoding: [0x94,0x00,0x00,0x00]
Ltmp0:
lhz r4, 4(r3) ; encoding: [0xa0,0x00,0x00,0x00]
cmplwi cr0, r4, 8 ; encoding: [0x28,0x00,0x00,0x00]
beq cr0, LBB0_2 ; encoding: [0x40,0x00,0x00,0x00]
llvm-svn: 119116
2010-11-15 05:16:32 +01:00
|
|
|
//===-- PPCMCCodeEmitter.cpp - Convert PPC code to machine code -----------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the PPCMCCodeEmitter class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "mccodeemitter"
|
|
|
|
#include "PPC.h"
|
add basic encoding support for immediates and registers, allowing us
to encode all of these instructions correctly (for example):
mflr r0 ; encoding: [0x7c,0x08,0x02,0xa6]
stw r0, 8(r1) ; encoding: [0x90,0x01,0x00,0x08]
stwu r1, -64(r1) ; encoding: [0x94,0x21,0xff,0xc0]
llvm-svn: 119118
2010-11-15 05:51:55 +01:00
|
|
|
#include "PPCRegisterInfo.h"
|
2010-11-15 06:57:53 +01:00
|
|
|
#include "PPCFixupKinds.h"
|
Implement a basic MCCodeEmitter for PPC. This doesn't handle
fixups yet, and doesn't handle actually encoding operand values,
but this is enough for llc -show-mc-encoding to show the base
instruction encoding information, e.g.:
mflr r0 ; encoding: [0x7c,0x08,0x02,0xa6]
stw r0, 8(r1) ; encoding: [0x90,0x00,0x00,0x00]
stwu r1, -64(r1) ; encoding: [0x94,0x00,0x00,0x00]
Ltmp0:
lhz r4, 4(r3) ; encoding: [0xa0,0x00,0x00,0x00]
cmplwi cr0, r4, 8 ; encoding: [0x28,0x00,0x00,0x00]
beq cr0, LBB0_2 ; encoding: [0x40,0x00,0x00,0x00]
llvm-svn: 119116
2010-11-15 05:16:32 +01:00
|
|
|
#include "llvm/MC/MCCodeEmitter.h"
|
|
|
|
#include "llvm/MC/MCInst.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class PPCMCCodeEmitter : public MCCodeEmitter {
|
|
|
|
PPCMCCodeEmitter(const PPCMCCodeEmitter &); // DO NOT IMPLEMENT
|
|
|
|
void operator=(const PPCMCCodeEmitter &); // DO NOT IMPLEMENT
|
|
|
|
|
|
|
|
public:
|
2011-07-11 05:57:24 +02:00
|
|
|
PPCMCCodeEmitter(const MCInstrInfo &mcii, const MCSubtargetInfo &sti,
|
2011-07-11 23:24:15 +02:00
|
|
|
MCContext &ctx) {
|
Implement a basic MCCodeEmitter for PPC. This doesn't handle
fixups yet, and doesn't handle actually encoding operand values,
but this is enough for llc -show-mc-encoding to show the base
instruction encoding information, e.g.:
mflr r0 ; encoding: [0x7c,0x08,0x02,0xa6]
stw r0, 8(r1) ; encoding: [0x90,0x00,0x00,0x00]
stwu r1, -64(r1) ; encoding: [0x94,0x00,0x00,0x00]
Ltmp0:
lhz r4, 4(r3) ; encoding: [0xa0,0x00,0x00,0x00]
cmplwi cr0, r4, 8 ; encoding: [0x28,0x00,0x00,0x00]
beq cr0, LBB0_2 ; encoding: [0x40,0x00,0x00,0x00]
llvm-svn: 119116
2010-11-15 05:16:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
~PPCMCCodeEmitter() {}
|
2010-11-15 06:19:25 +01:00
|
|
|
|
2010-11-15 07:09:35 +01:00
|
|
|
unsigned getDirectBrEncoding(const MCInst &MI, unsigned OpNo,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups) const;
|
|
|
|
unsigned getCondBrEncoding(const MCInst &MI, unsigned OpNo,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups) const;
|
2010-11-15 07:33:39 +01:00
|
|
|
unsigned getHA16Encoding(const MCInst &MI, unsigned OpNo,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups) const;
|
|
|
|
unsigned getLO16Encoding(const MCInst &MI, unsigned OpNo,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups) const;
|
split out an encoder for memri operands, allowing a relocation to be plopped
into the immediate field. This allows us to encode stuff like this:
lbz r3, lo16(__ZL4init)(r4) ; globalopt.cpp:5
; encoding: [0x88,0x64,A,A]
; fixup A - offset: 0, value: lo16(__ZL4init), kind: fixup_ppc_lo16
stw r3, lo16(__ZL1s)(r5) ; globalopt.cpp:6
; encoding: [0x90,0x65,A,A]
; fixup A - offset: 0, value: lo16(__ZL1s), kind: fixup_ppc_lo16
With this, we should have a completely function MCCodeEmitter for PPC, wewt.
llvm-svn: 119134
2010-11-15 09:22:03 +01:00
|
|
|
unsigned getMemRIEncoding(const MCInst &MI, unsigned OpNo,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups) const;
|
2010-11-15 09:02:41 +01:00
|
|
|
unsigned getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups) const;
|
2010-11-15 06:19:25 +01:00
|
|
|
unsigned get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups) const;
|
|
|
|
|
Implement a basic MCCodeEmitter for PPC. This doesn't handle
fixups yet, and doesn't handle actually encoding operand values,
but this is enough for llc -show-mc-encoding to show the base
instruction encoding information, e.g.:
mflr r0 ; encoding: [0x7c,0x08,0x02,0xa6]
stw r0, 8(r1) ; encoding: [0x90,0x00,0x00,0x00]
stwu r1, -64(r1) ; encoding: [0x94,0x00,0x00,0x00]
Ltmp0:
lhz r4, 4(r3) ; encoding: [0xa0,0x00,0x00,0x00]
cmplwi cr0, r4, 8 ; encoding: [0x28,0x00,0x00,0x00]
beq cr0, LBB0_2 ; encoding: [0x40,0x00,0x00,0x00]
llvm-svn: 119116
2010-11-15 05:16:32 +01:00
|
|
|
/// 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;
|
|
|
|
|
|
|
|
// getBinaryCodeForInstr - TableGen'erated function for getting the
|
|
|
|
// binary encoding for an instruction.
|
|
|
|
unsigned getBinaryCodeForInstr(const MCInst &MI,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups) const;
|
|
|
|
void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups) const {
|
|
|
|
unsigned Bits = getBinaryCodeForInstr(MI, Fixups);
|
|
|
|
|
|
|
|
// Output the constant in big endian byte order.
|
|
|
|
for (unsigned i = 0; i != 4; ++i) {
|
|
|
|
OS << (char)(Bits >> 24);
|
|
|
|
Bits <<= 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
++MCNumEmitted; // Keep track of the # of mi's emitted.
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2011-07-11 05:57:24 +02:00
|
|
|
MCCodeEmitter *llvm::createPPCMCCodeEmitter(const MCInstrInfo &MCII,
|
|
|
|
const MCSubtargetInfo &STI,
|
Implement a basic MCCodeEmitter for PPC. This doesn't handle
fixups yet, and doesn't handle actually encoding operand values,
but this is enough for llc -show-mc-encoding to show the base
instruction encoding information, e.g.:
mflr r0 ; encoding: [0x7c,0x08,0x02,0xa6]
stw r0, 8(r1) ; encoding: [0x90,0x00,0x00,0x00]
stwu r1, -64(r1) ; encoding: [0x94,0x00,0x00,0x00]
Ltmp0:
lhz r4, 4(r3) ; encoding: [0xa0,0x00,0x00,0x00]
cmplwi cr0, r4, 8 ; encoding: [0x28,0x00,0x00,0x00]
beq cr0, LBB0_2 ; encoding: [0x40,0x00,0x00,0x00]
llvm-svn: 119116
2010-11-15 05:16:32 +01:00
|
|
|
MCContext &Ctx) {
|
2011-07-11 05:57:24 +02:00
|
|
|
return new PPCMCCodeEmitter(MCII, STI, Ctx);
|
Implement a basic MCCodeEmitter for PPC. This doesn't handle
fixups yet, and doesn't handle actually encoding operand values,
but this is enough for llc -show-mc-encoding to show the base
instruction encoding information, e.g.:
mflr r0 ; encoding: [0x7c,0x08,0x02,0xa6]
stw r0, 8(r1) ; encoding: [0x90,0x00,0x00,0x00]
stwu r1, -64(r1) ; encoding: [0x94,0x00,0x00,0x00]
Ltmp0:
lhz r4, 4(r3) ; encoding: [0xa0,0x00,0x00,0x00]
cmplwi cr0, r4, 8 ; encoding: [0x28,0x00,0x00,0x00]
beq cr0, LBB0_2 ; encoding: [0x40,0x00,0x00,0x00]
llvm-svn: 119116
2010-11-15 05:16:32 +01:00
|
|
|
}
|
|
|
|
|
2010-11-15 06:57:53 +01:00
|
|
|
unsigned PPCMCCodeEmitter::
|
2010-11-15 07:09:35 +01:00
|
|
|
getDirectBrEncoding(const MCInst &MI, unsigned OpNo,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups) const {
|
2010-11-15 06:57:53 +01:00
|
|
|
const MCOperand &MO = MI.getOperand(OpNo);
|
|
|
|
if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
|
|
|
|
|
|
|
|
// Add a fixup for the branch target.
|
|
|
|
Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
|
|
|
|
(MCFixupKind)PPC::fixup_ppc_br24));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-11-15 07:09:35 +01:00
|
|
|
unsigned PPCMCCodeEmitter::getCondBrEncoding(const MCInst &MI, unsigned OpNo,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups) const {
|
|
|
|
const MCOperand &MO = MI.getOperand(OpNo);
|
|
|
|
if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
|
|
|
|
|
2010-11-15 07:12:22 +01:00
|
|
|
// Add a fixup for the branch target.
|
|
|
|
Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
|
|
|
|
(MCFixupKind)PPC::fixup_ppc_brcond14));
|
2010-11-15 07:09:35 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-11-15 07:33:39 +01:00
|
|
|
unsigned PPCMCCodeEmitter::getHA16Encoding(const MCInst &MI, unsigned OpNo,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups) const {
|
|
|
|
const MCOperand &MO = MI.getOperand(OpNo);
|
|
|
|
if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
|
|
|
|
|
|
|
|
// Add a fixup for the branch target.
|
|
|
|
Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
|
|
|
|
(MCFixupKind)PPC::fixup_ppc_ha16));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned PPCMCCodeEmitter::getLO16Encoding(const MCInst &MI, unsigned OpNo,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups) const {
|
|
|
|
const MCOperand &MO = MI.getOperand(OpNo);
|
|
|
|
if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
|
|
|
|
|
|
|
|
// Add a fixup for the branch target.
|
|
|
|
Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
|
|
|
|
(MCFixupKind)PPC::fixup_ppc_lo16));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
split out an encoder for memri operands, allowing a relocation to be plopped
into the immediate field. This allows us to encode stuff like this:
lbz r3, lo16(__ZL4init)(r4) ; globalopt.cpp:5
; encoding: [0x88,0x64,A,A]
; fixup A - offset: 0, value: lo16(__ZL4init), kind: fixup_ppc_lo16
stw r3, lo16(__ZL1s)(r5) ; globalopt.cpp:6
; encoding: [0x90,0x65,A,A]
; fixup A - offset: 0, value: lo16(__ZL1s), kind: fixup_ppc_lo16
With this, we should have a completely function MCCodeEmitter for PPC, wewt.
llvm-svn: 119134
2010-11-15 09:22:03 +01:00
|
|
|
unsigned PPCMCCodeEmitter::getMemRIEncoding(const MCInst &MI, unsigned OpNo,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups) const {
|
|
|
|
// Encode (imm, reg) as a memri, which has the low 16-bits as the
|
|
|
|
// displacement and the next 5 bits as the register #.
|
|
|
|
assert(MI.getOperand(OpNo+1).isReg());
|
|
|
|
unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups) << 16;
|
|
|
|
|
|
|
|
const MCOperand &MO = MI.getOperand(OpNo);
|
|
|
|
if (MO.isImm())
|
|
|
|
return (getMachineOpValue(MI, MO, Fixups) & 0xFFFF) | RegBits;
|
|
|
|
|
|
|
|
// Add a fixup for the displacement field.
|
|
|
|
Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
|
|
|
|
(MCFixupKind)PPC::fixup_ppc_lo16));
|
|
|
|
return RegBits;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-15 09:02:41 +01:00
|
|
|
unsigned PPCMCCodeEmitter::getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
|
2010-11-15 07:33:39 +01:00
|
|
|
SmallVectorImpl<MCFixup> &Fixups) const {
|
2010-11-15 09:02:41 +01:00
|
|
|
// Encode (imm, reg) as a memrix, which has the low 14-bits as the
|
|
|
|
// displacement and the next 5 bits as the register #.
|
split out an encoder for memri operands, allowing a relocation to be plopped
into the immediate field. This allows us to encode stuff like this:
lbz r3, lo16(__ZL4init)(r4) ; globalopt.cpp:5
; encoding: [0x88,0x64,A,A]
; fixup A - offset: 0, value: lo16(__ZL4init), kind: fixup_ppc_lo16
stw r3, lo16(__ZL1s)(r5) ; globalopt.cpp:6
; encoding: [0x90,0x65,A,A]
; fixup A - offset: 0, value: lo16(__ZL1s), kind: fixup_ppc_lo16
With this, we should have a completely function MCCodeEmitter for PPC, wewt.
llvm-svn: 119134
2010-11-15 09:22:03 +01:00
|
|
|
assert(MI.getOperand(OpNo+1).isReg());
|
2010-11-15 09:02:41 +01:00
|
|
|
unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups) << 14;
|
|
|
|
|
2010-11-15 07:33:39 +01:00
|
|
|
const MCOperand &MO = MI.getOperand(OpNo);
|
2010-11-15 09:02:41 +01:00
|
|
|
if (MO.isImm())
|
|
|
|
return (getMachineOpValue(MI, MO, Fixups) & 0x3FFF) | RegBits;
|
2010-11-15 07:33:39 +01:00
|
|
|
|
|
|
|
// Add a fixup for the branch target.
|
|
|
|
Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
|
|
|
|
(MCFixupKind)PPC::fixup_ppc_lo14));
|
2010-11-15 09:02:41 +01:00
|
|
|
return RegBits;
|
2010-11-15 07:33:39 +01:00
|
|
|
}
|
|
|
|
|
2010-11-15 07:09:35 +01:00
|
|
|
|
2010-11-15 06:19:25 +01:00
|
|
|
unsigned PPCMCCodeEmitter::
|
|
|
|
get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups) const {
|
|
|
|
const MCOperand &MO = MI.getOperand(OpNo);
|
|
|
|
assert((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MFOCRF) &&
|
|
|
|
(MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7));
|
|
|
|
return 0x80 >> PPCRegisterInfo::getRegisterNumbering(MO.getReg());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
Implement a basic MCCodeEmitter for PPC. This doesn't handle
fixups yet, and doesn't handle actually encoding operand values,
but this is enough for llc -show-mc-encoding to show the base
instruction encoding information, e.g.:
mflr r0 ; encoding: [0x7c,0x08,0x02,0xa6]
stw r0, 8(r1) ; encoding: [0x90,0x00,0x00,0x00]
stwu r1, -64(r1) ; encoding: [0x94,0x00,0x00,0x00]
Ltmp0:
lhz r4, 4(r3) ; encoding: [0xa0,0x00,0x00,0x00]
cmplwi cr0, r4, 8 ; encoding: [0x28,0x00,0x00,0x00]
beq cr0, LBB0_2 ; encoding: [0x40,0x00,0x00,0x00]
llvm-svn: 119116
2010-11-15 05:16:32 +01:00
|
|
|
unsigned PPCMCCodeEmitter::
|
|
|
|
getMachineOpValue(const MCInst &MI, const MCOperand &MO,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups) const {
|
2010-11-15 06:19:25 +01:00
|
|
|
if (MO.isReg()) {
|
2010-11-16 01:57:32 +01:00
|
|
|
// MTCRF/MFOCRF should go through get_crbitm_encoding for the CR operand.
|
|
|
|
// The GPR operand should come through here though.
|
2010-11-16 01:55:51 +01:00
|
|
|
assert((MI.getOpcode() != PPC::MTCRF && MI.getOpcode() != PPC::MFOCRF) ||
|
|
|
|
MO.getReg() < PPC::CR0 || MO.getReg() > PPC::CR7);
|
add basic encoding support for immediates and registers, allowing us
to encode all of these instructions correctly (for example):
mflr r0 ; encoding: [0x7c,0x08,0x02,0xa6]
stw r0, 8(r1) ; encoding: [0x90,0x01,0x00,0x08]
stwu r1, -64(r1) ; encoding: [0x94,0x21,0xff,0xc0]
llvm-svn: 119118
2010-11-15 05:51:55 +01:00
|
|
|
return PPCRegisterInfo::getRegisterNumbering(MO.getReg());
|
2010-11-15 06:19:25 +01:00
|
|
|
}
|
add basic encoding support for immediates and registers, allowing us
to encode all of these instructions correctly (for example):
mflr r0 ; encoding: [0x7c,0x08,0x02,0xa6]
stw r0, 8(r1) ; encoding: [0x90,0x01,0x00,0x08]
stwu r1, -64(r1) ; encoding: [0x94,0x21,0xff,0xc0]
llvm-svn: 119118
2010-11-15 05:51:55 +01:00
|
|
|
|
split out an encoder for memri operands, allowing a relocation to be plopped
into the immediate field. This allows us to encode stuff like this:
lbz r3, lo16(__ZL4init)(r4) ; globalopt.cpp:5
; encoding: [0x88,0x64,A,A]
; fixup A - offset: 0, value: lo16(__ZL4init), kind: fixup_ppc_lo16
stw r3, lo16(__ZL1s)(r5) ; globalopt.cpp:6
; encoding: [0x90,0x65,A,A]
; fixup A - offset: 0, value: lo16(__ZL1s), kind: fixup_ppc_lo16
With this, we should have a completely function MCCodeEmitter for PPC, wewt.
llvm-svn: 119134
2010-11-15 09:22:03 +01:00
|
|
|
assert(MO.isImm() &&
|
|
|
|
"Relocation required in an instruction that we cannot encode!");
|
|
|
|
return MO.getImm();
|
Implement a basic MCCodeEmitter for PPC. This doesn't handle
fixups yet, and doesn't handle actually encoding operand values,
but this is enough for llc -show-mc-encoding to show the base
instruction encoding information, e.g.:
mflr r0 ; encoding: [0x7c,0x08,0x02,0xa6]
stw r0, 8(r1) ; encoding: [0x90,0x00,0x00,0x00]
stwu r1, -64(r1) ; encoding: [0x94,0x00,0x00,0x00]
Ltmp0:
lhz r4, 4(r3) ; encoding: [0xa0,0x00,0x00,0x00]
cmplwi cr0, r4, 8 ; encoding: [0x28,0x00,0x00,0x00]
beq cr0, LBB0_2 ; encoding: [0x40,0x00,0x00,0x00]
llvm-svn: 119116
2010-11-15 05:16:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#include "PPCGenMCCodeEmitter.inc"
|