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 12:16:32 +08: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"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "MCTargetDesc/PPCMCTargetDesc.h"
|
2011-07-26 03:53:23 +08:00
|
|
|
#include "MCTargetDesc/PPCFixupKinds.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/Statistic.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 12:16:32 +08:00
|
|
|
#include "llvm/MC/MCCodeEmitter.h"
|
2013-03-27 04:08:20 +08:00
|
|
|
#include "llvm/MC/MCContext.h"
|
This patch implements the general dynamic TLS model for 64-bit PowerPC.
Given a thread-local symbol x with global-dynamic access, the generated
code to obtain x's address is:
Instruction Relocation Symbol
addis ra,r2,x@got@tlsgd@ha R_PPC64_GOT_TLSGD16_HA x
addi r3,ra,x@got@tlsgd@l R_PPC64_GOT_TLSGD16_L x
bl __tls_get_addr(x@tlsgd) R_PPC64_TLSGD x
R_PPC64_REL24 __tls_get_addr
nop
<use address in r3>
The implementation borrows from the medium code model work for introducing
special forms of ADDIS and ADDI into the DAG representation. This is made
slightly more complicated by having to introduce a call to the external
function __tls_get_addr. Using the full call machinery is overkill and,
more importantly, makes it difficult to add a special relocation. So I've
introduced another opcode GET_TLS_ADDR to represent the function call, and
surrounded it with register copies to set up the parameter and return value.
Most of the code is pretty straightforward. I ran into one peculiarity
when I introduced a new PPC opcode BL8_NOP_ELF_TLSGD, which is just like
BL8_NOP_ELF except that it takes another parameter to represent the symbol
("x" above) that requires a relocation on the call. Something in the
TblGen machinery causes BL8_NOP_ELF and BL8_NOP_ELF_TLSGD to be treated
identically during the emit phase, so this second operand was never
visited to generate relocations. This is the reason for the slightly
messy workaround in PPCMCCodeEmitter.cpp:getDirectBrEncoding().
Two new tests are included to demonstrate correct external assembly and
correct generation of relocations using the integrated assembler.
Comments welcome!
Thanks,
Bill
llvm-svn: 169910
2012-12-12 04:30:11 +08:00
|
|
|
#include "llvm/MC/MCExpr.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 12:16:32 +08:00
|
|
|
#include "llvm/MC/MCInst.h"
|
2012-10-25 20:27:42 +08:00
|
|
|
#include "llvm/MC/MCInstrInfo.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/MC/MCSubtargetInfo.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 12:16:32 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2013-09-17 01:25:12 +08:00
|
|
|
#include "llvm/Target/TargetOpcodes.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 12:16:32 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class PPCMCCodeEmitter : public MCCodeEmitter {
|
2012-09-16 01:09:36 +08:00
|
|
|
PPCMCCodeEmitter(const PPCMCCodeEmitter &) LLVM_DELETED_FUNCTION;
|
|
|
|
void operator=(const PPCMCCodeEmitter &) LLVM_DELETED_FUNCTION;
|
|
|
|
|
2013-03-27 04:08:20 +08:00
|
|
|
const MCContext &CTX;
|
2012-10-25 20:27:42 +08:00
|
|
|
|
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 12:16:32 +08:00
|
|
|
public:
|
2014-01-29 07:13:25 +08:00
|
|
|
PPCMCCodeEmitter(const MCInstrInfo &mcii, MCContext &ctx) : CTX(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 12:16:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
~PPCMCCodeEmitter() {}
|
2010-11-15 13:19:25 +08:00
|
|
|
|
2010-11-15 14:09:35 +08:00
|
|
|
unsigned getDirectBrEncoding(const MCInst &MI, unsigned OpNo,
|
2014-01-29 07:13:18 +08:00
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const;
|
2010-11-15 14:09:35 +08:00
|
|
|
unsigned getCondBrEncoding(const MCInst &MI, unsigned OpNo,
|
2014-01-29 07:13:18 +08:00
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const;
|
2013-06-24 19:03:33 +08:00
|
|
|
unsigned getAbsDirectBrEncoding(const MCInst &MI, unsigned OpNo,
|
2014-01-29 07:13:18 +08:00
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const;
|
2013-06-24 19:03:33 +08:00
|
|
|
unsigned getAbsCondBrEncoding(const MCInst &MI, unsigned OpNo,
|
2014-01-29 07:13:18 +08:00
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const;
|
2013-06-26 21:49:15 +08:00
|
|
|
unsigned getImm16Encoding(const MCInst &MI, unsigned OpNo,
|
2014-01-29 07:13:18 +08:00
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) 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 16:22:03 +08:00
|
|
|
unsigned getMemRIEncoding(const MCInst &MI, unsigned OpNo,
|
2014-01-29 07:13:18 +08:00
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const;
|
2010-11-15 16:02:41 +08:00
|
|
|
unsigned getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
|
2014-01-29 07:13:18 +08:00
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const;
|
2012-12-05 00:18:08 +08:00
|
|
|
unsigned getTLSRegEncoding(const MCInst &MI, unsigned OpNo,
|
2014-01-29 07:13:18 +08:00
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const;
|
2013-07-03 05:31:04 +08:00
|
|
|
unsigned getTLSCallEncoding(const MCInst &MI, unsigned OpNo,
|
2014-01-29 07:13:18 +08:00
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const;
|
2010-11-15 13:19:25 +08:00
|
|
|
unsigned get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
|
2014-01-29 07:13:18 +08:00
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const;
|
2010-11-15 13:19:25 +08:00
|
|
|
|
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 12:16:32 +08: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,
|
2014-01-29 07:13:18 +08:00
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) 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 12:16:32 +08:00
|
|
|
|
|
|
|
// getBinaryCodeForInstr - TableGen'erated function for getting the
|
|
|
|
// binary encoding for an instruction.
|
2012-01-25 02:37:29 +08:00
|
|
|
uint64_t getBinaryCodeForInstr(const MCInst &MI,
|
2014-01-29 07:13:18 +08:00
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) 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 12:16:32 +08:00
|
|
|
void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
|
2014-01-29 07:13:07 +08:00
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const {
|
2013-09-17 01:25:12 +08:00
|
|
|
// For fast-isel, a float COPY_TO_REGCLASS can survive this long.
|
|
|
|
// It's just a nop to keep the register classes happy, so don't
|
|
|
|
// generate anything.
|
|
|
|
unsigned Opcode = MI.getOpcode();
|
|
|
|
if (Opcode == TargetOpcode::COPY_TO_REGCLASS)
|
|
|
|
return;
|
|
|
|
|
2014-01-29 07:13:18 +08:00
|
|
|
uint64_t Bits = getBinaryCodeForInstr(MI, Fixups, STI);
|
2012-10-25 22:29:13 +08:00
|
|
|
|
2013-03-22 23:24:13 +08:00
|
|
|
// BL8_NOP etc. all have a size of 8 because of the following 'nop'.
|
2012-10-25 22:29:13 +08:00
|
|
|
unsigned Size = 4; // FIXME: Have Desc.getSize() return the correct value!
|
2013-03-22 23:24:13 +08:00
|
|
|
if (Opcode == PPC::BL8_NOP || Opcode == PPC::BLA8_NOP ||
|
2013-07-03 05:31:04 +08:00
|
|
|
Opcode == PPC::BL8_NOP_TLS)
|
2012-10-25 22:29:13 +08:00
|
|
|
Size = 8;
|
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 12:16:32 +08:00
|
|
|
|
|
|
|
// Output the constant in big endian byte order.
|
2012-10-25 22:29:13 +08:00
|
|
|
int ShiftValue = (Size * 8) - 8;
|
|
|
|
for (unsigned i = 0; i != Size; ++i) {
|
|
|
|
OS << (char)(Bits >> ShiftValue);
|
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 12:16:32 +08:00
|
|
|
Bits <<= 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
++MCNumEmitted; // Keep track of the # of mi's emitted.
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2011-07-11 11:57:24 +08:00
|
|
|
MCCodeEmitter *llvm::createPPCMCCodeEmitter(const MCInstrInfo &MCII,
|
2012-05-16 01:35:52 +08:00
|
|
|
const MCRegisterInfo &MRI,
|
2011-07-11 11:57:24 +08:00
|
|
|
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 12:16:32 +08:00
|
|
|
MCContext &Ctx) {
|
2014-01-29 07:13:25 +08:00
|
|
|
return new PPCMCCodeEmitter(MCII, 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 12:16:32 +08:00
|
|
|
}
|
|
|
|
|
2010-11-15 13:57:53 +08:00
|
|
|
unsigned PPCMCCodeEmitter::
|
2010-11-15 14:09:35 +08:00
|
|
|
getDirectBrEncoding(const MCInst &MI, unsigned OpNo,
|
2014-01-29 07:13:18 +08:00
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const {
|
2010-11-15 13:57:53 +08:00
|
|
|
const MCOperand &MO = MI.getOperand(OpNo);
|
2014-01-29 07:13:18 +08:00
|
|
|
if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI);
|
2010-11-15 13:57:53 +08:00
|
|
|
|
|
|
|
// 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 14:09:35 +08:00
|
|
|
unsigned PPCMCCodeEmitter::getCondBrEncoding(const MCInst &MI, unsigned OpNo,
|
2014-01-29 07:13:18 +08:00
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const {
|
2010-11-15 14:09:35 +08:00
|
|
|
const MCOperand &MO = MI.getOperand(OpNo);
|
2014-01-29 07:13:18 +08:00
|
|
|
if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI);
|
2010-11-15 14:09:35 +08:00
|
|
|
|
2010-11-15 14:12:22 +08:00
|
|
|
// Add a fixup for the branch target.
|
|
|
|
Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
|
|
|
|
(MCFixupKind)PPC::fixup_ppc_brcond14));
|
2010-11-15 14:09:35 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-24 19:03:33 +08:00
|
|
|
unsigned PPCMCCodeEmitter::
|
|
|
|
getAbsDirectBrEncoding(const MCInst &MI, unsigned OpNo,
|
2014-01-29 07:13:18 +08:00
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const {
|
2013-06-24 19:03:33 +08:00
|
|
|
const MCOperand &MO = MI.getOperand(OpNo);
|
2014-01-29 07:13:18 +08:00
|
|
|
if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI);
|
2013-06-24 19:03:33 +08:00
|
|
|
|
|
|
|
// Add a fixup for the branch target.
|
|
|
|
Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
|
|
|
|
(MCFixupKind)PPC::fixup_ppc_br24abs));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned PPCMCCodeEmitter::
|
|
|
|
getAbsCondBrEncoding(const MCInst &MI, unsigned OpNo,
|
2014-01-29 07:13:18 +08:00
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const {
|
2013-06-24 19:03:33 +08:00
|
|
|
const MCOperand &MO = MI.getOperand(OpNo);
|
2014-01-29 07:13:18 +08:00
|
|
|
if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI);
|
2013-06-24 19:03:33 +08:00
|
|
|
|
|
|
|
// Add a fixup for the branch target.
|
|
|
|
Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
|
|
|
|
(MCFixupKind)PPC::fixup_ppc_brcond14abs));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-26 21:49:15 +08:00
|
|
|
unsigned PPCMCCodeEmitter::getImm16Encoding(const MCInst &MI, unsigned OpNo,
|
2014-01-29 07:13:18 +08:00
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const {
|
2010-11-15 14:33:39 +08:00
|
|
|
const MCOperand &MO = MI.getOperand(OpNo);
|
2014-01-29 07:13:18 +08:00
|
|
|
if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI);
|
2010-11-15 14:33:39 +08:00
|
|
|
|
2013-06-26 21:49:15 +08:00
|
|
|
// Add a fixup for the immediate field.
|
2013-05-15 23:07:06 +08:00
|
|
|
Fixups.push_back(MCFixup::Create(2, MO.getExpr(),
|
2013-05-17 20:37:21 +08:00
|
|
|
(MCFixupKind)PPC::fixup_ppc_half16));
|
2010-11-15 14:33:39 +08:00
|
|
|
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 16:22:03 +08:00
|
|
|
unsigned PPCMCCodeEmitter::getMemRIEncoding(const MCInst &MI, unsigned OpNo,
|
2014-01-29 07:13:18 +08:00
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) 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 16:22:03 +08:00
|
|
|
// 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());
|
2014-01-29 07:13:18 +08:00
|
|
|
unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 16;
|
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 16:22:03 +08:00
|
|
|
|
|
|
|
const MCOperand &MO = MI.getOperand(OpNo);
|
|
|
|
if (MO.isImm())
|
2014-01-29 07:13:18 +08:00
|
|
|
return (getMachineOpValue(MI, MO, Fixups, STI) & 0xFFFF) | RegBits;
|
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 16:22:03 +08:00
|
|
|
|
|
|
|
// Add a fixup for the displacement field.
|
2013-05-15 23:07:06 +08:00
|
|
|
Fixups.push_back(MCFixup::Create(2, MO.getExpr(),
|
2013-05-17 20:37:21 +08:00
|
|
|
(MCFixupKind)PPC::fixup_ppc_half16));
|
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 16:22:03 +08:00
|
|
|
return RegBits;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-15 16:02:41 +08:00
|
|
|
unsigned PPCMCCodeEmitter::getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
|
2014-01-29 07:13:18 +08:00
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const {
|
2010-11-15 16:02:41 +08: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 16:22:03 +08:00
|
|
|
assert(MI.getOperand(OpNo+1).isReg());
|
2014-01-29 07:13:18 +08:00
|
|
|
unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 14;
|
2010-11-15 16:02:41 +08:00
|
|
|
|
2010-11-15 14:33:39 +08:00
|
|
|
const MCOperand &MO = MI.getOperand(OpNo);
|
2010-11-15 16:02:41 +08:00
|
|
|
if (MO.isImm())
|
2014-01-29 07:13:18 +08:00
|
|
|
return ((getMachineOpValue(MI, MO, Fixups, STI) >> 2) & 0x3FFF) | RegBits;
|
2010-11-15 14:33:39 +08:00
|
|
|
|
PowerPC: Simplify handling of fixups.
MCTargetDesc/PPCMCCodeEmitter.cpp current has code like:
if (isSVR4ABI() && is64BitMode())
Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
(MCFixupKind)PPC::fixup_ppc_toc16));
else
Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
(MCFixupKind)PPC::fixup_ppc_lo16));
This is a problem for the asm parser, since it requires knowledge of
the ABI / 64-bit mode to be set up. However, more fundamentally,
at this point we shouldn't make such distinctions anyway; in an assembler
file, it always ought to be possible to e.g. generate TOC relocations even
when the main ABI is one that doesn't use TOC.
Fortunately, this is actually completely unnecessary; that code was added
to decide whether to generate TOC relocations, but that information is in
fact already encoded in the VariantKind of the underlying symbol.
This commit therefore merges those fixup types into one, and then decides
which relocation to use based on the VariantKind.
No changes in generated code.
llvm-svn: 178007
2013-03-26 18:56:47 +08:00
|
|
|
// Add a fixup for the displacement field.
|
2013-05-15 23:07:06 +08:00
|
|
|
Fixups.push_back(MCFixup::Create(2, MO.getExpr(),
|
2013-05-17 20:37:21 +08:00
|
|
|
(MCFixupKind)PPC::fixup_ppc_half16ds));
|
2010-11-15 16:02:41 +08:00
|
|
|
return RegBits;
|
2010-11-15 14:33:39 +08:00
|
|
|
}
|
|
|
|
|
2010-11-15 14:09:35 +08:00
|
|
|
|
2012-12-05 00:18:08 +08:00
|
|
|
unsigned PPCMCCodeEmitter::getTLSRegEncoding(const MCInst &MI, unsigned OpNo,
|
2014-01-29 07:13:18 +08:00
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const {
|
2012-12-05 00:18:08 +08:00
|
|
|
const MCOperand &MO = MI.getOperand(OpNo);
|
2014-01-29 07:13:18 +08:00
|
|
|
if (MO.isReg()) return getMachineOpValue(MI, MO, Fixups, STI);
|
2012-12-05 00:18:08 +08:00
|
|
|
|
|
|
|
// Add a fixup for the TLS register, which simply provides a relocation
|
|
|
|
// hint to the linker that this statement is part of a relocation sequence.
|
|
|
|
// Return the thread-pointer register's encoding.
|
|
|
|
Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
|
2013-07-05 20:22:36 +08:00
|
|
|
(MCFixupKind)PPC::fixup_ppc_nofixup));
|
2014-01-29 07:13:25 +08:00
|
|
|
Triple TT(STI.getTargetTriple());
|
2013-12-22 18:45:37 +08:00
|
|
|
bool isPPC64 = TT.getArch() == Triple::ppc64 || TT.getArch() == Triple::ppc64le;
|
|
|
|
return CTX.getRegisterInfo()->getEncodingValue(isPPC64 ? PPC::X13 : PPC::R2);
|
2012-12-05 00:18:08 +08:00
|
|
|
}
|
|
|
|
|
2013-07-03 05:31:04 +08:00
|
|
|
unsigned PPCMCCodeEmitter::getTLSCallEncoding(const MCInst &MI, unsigned OpNo,
|
2014-01-29 07:13:18 +08:00
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const {
|
2013-07-03 05:31:04 +08:00
|
|
|
// For special TLS calls, we need two fixups; one for the branch target
|
|
|
|
// (__tls_get_addr), which we create via getDirectBrEncoding as usual,
|
|
|
|
// and one for the TLSGD or TLSLD symbol, which is emitted here.
|
|
|
|
const MCOperand &MO = MI.getOperand(OpNo+1);
|
|
|
|
Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
|
|
|
|
(MCFixupKind)PPC::fixup_ppc_nofixup));
|
2014-01-29 07:13:18 +08:00
|
|
|
return getDirectBrEncoding(MI, OpNo, Fixups, STI);
|
2013-07-03 05:31:04 +08:00
|
|
|
}
|
|
|
|
|
2010-11-15 13:19:25 +08:00
|
|
|
unsigned PPCMCCodeEmitter::
|
|
|
|
get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
|
2014-01-29 07:13:18 +08:00
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const {
|
2010-11-15 13:19:25 +08:00
|
|
|
const MCOperand &MO = MI.getOperand(OpNo);
|
2013-07-04 01:59:07 +08:00
|
|
|
assert((MI.getOpcode() == PPC::MTOCRF || MI.getOpcode() == PPC::MTOCRF8 ||
|
[PowerPC] Always use mfocrf if available
When accessing just a single CR register, it is always preferable to
use mfocrf instead of mfcr, if the former is available on the CPU.
Current code makes that distinction in many, but not all places
where a single CR register value is retrieved. One missing
location is PPCRegisterInfo::lowerCRSpilling.
To fix this and make this simpler in the future, this patch changes
the bulk of the back-end to always assume mfocrf is available and
simply generate it when needed.
On machines that actually do not support mfocrf, the instruction
is replaced by mfcr at the very end, in EmitInstruction.
This has the additional benefit that we no longer need the
MFCRpseud hack, since before EmitInstruction we always have
a MFOCRF instruction pattern, which already models data flow
as required.
The patch also adds the MFOCRF8 version of the instruction,
which was missing so far.
Except for the PPCRegisterInfo::lowerCRSpilling case, no change
in generated code intended.
llvm-svn: 185556
2013-07-04 01:05:42 +08:00
|
|
|
MI.getOpcode() == PPC::MFOCRF || MI.getOpcode() == PPC::MFOCRF8) &&
|
2010-11-15 13:19:25 +08:00
|
|
|
(MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7));
|
2013-06-18 15:20:20 +08:00
|
|
|
return 0x80 >> CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
|
2010-11-15 13:19:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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 12:16:32 +08:00
|
|
|
unsigned PPCMCCodeEmitter::
|
|
|
|
getMachineOpValue(const MCInst &MI, const MCOperand &MO,
|
2014-01-29 07:13:18 +08:00
|
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
|
|
const MCSubtargetInfo &STI) const {
|
2010-11-15 13:19:25 +08:00
|
|
|
if (MO.isReg()) {
|
2013-07-04 01:59:07 +08:00
|
|
|
// MTOCRF/MFOCRF should go through get_crbitm_encoding for the CR operand.
|
2010-11-16 08:57:32 +08:00
|
|
|
// The GPR operand should come through here though.
|
2013-07-04 01:59:07 +08:00
|
|
|
assert((MI.getOpcode() != PPC::MTOCRF && MI.getOpcode() != PPC::MTOCRF8 &&
|
[PowerPC] Always use mfocrf if available
When accessing just a single CR register, it is always preferable to
use mfocrf instead of mfcr, if the former is available on the CPU.
Current code makes that distinction in many, but not all places
where a single CR register value is retrieved. One missing
location is PPCRegisterInfo::lowerCRSpilling.
To fix this and make this simpler in the future, this patch changes
the bulk of the back-end to always assume mfocrf is available and
simply generate it when needed.
On machines that actually do not support mfocrf, the instruction
is replaced by mfcr at the very end, in EmitInstruction.
This has the additional benefit that we no longer need the
MFCRpseud hack, since before EmitInstruction we always have
a MFOCRF instruction pattern, which already models data flow
as required.
The patch also adds the MFOCRF8 version of the instruction,
which was missing so far.
Except for the PPCRegisterInfo::lowerCRSpilling case, no change
in generated code intended.
llvm-svn: 185556
2013-07-04 01:05:42 +08:00
|
|
|
MI.getOpcode() != PPC::MFOCRF && MI.getOpcode() != PPC::MFOCRF8) ||
|
2010-11-16 08:55:51 +08:00
|
|
|
MO.getReg() < PPC::CR0 || MO.getReg() > PPC::CR7);
|
2013-06-18 15:20:20 +08:00
|
|
|
return CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
|
2010-11-15 13:19:25 +08: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 12:51:55 +08: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 16:22:03 +08: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 12:16:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#include "PPCGenMCCodeEmitter.inc"
|