2005-10-16 07:39:50 +02:00
|
|
|
//===-- PPCCodeEmitter.cpp - JIT Code Emitter for PowerPC32 -------*- C++ -*-=//
|
2005-04-22 01:30:14 +02:00
|
|
|
//
|
2004-06-21 18:55:25 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 21:36:04 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 01:30:14 +02:00
|
|
|
//
|
2004-06-21 18:55:25 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2005-04-22 01:30:14 +02:00
|
|
|
//
|
2004-10-14 08:07:25 +02:00
|
|
|
// This file defines the PowerPC 32-bit CodeEmitter and associated machinery to
|
2007-07-05 19:07:56 +02:00
|
|
|
// JIT-compile bitcode to native PowerPC.
|
2004-06-21 18:55:25 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2005-10-15 01:59:06 +02:00
|
|
|
#include "PPCTargetMachine.h"
|
|
|
|
#include "PPCRelocations.h"
|
2005-10-15 01:51:18 +02:00
|
|
|
#include "PPC.h"
|
2004-10-21 03:42:02 +02:00
|
|
|
#include "llvm/Module.h"
|
2005-10-15 23:58:54 +02:00
|
|
|
#include "llvm/PassManager.h"
|
2009-05-30 22:51:52 +02:00
|
|
|
#include "llvm/CodeGen/JITCodeEmitter.h"
|
2004-08-10 01:03:59 +02:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2004-10-24 01:47:34 +02:00
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2008-02-13 19:39:37 +01:00
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
2009-07-08 22:53:28 +02:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2006-02-18 01:08:58 +01:00
|
|
|
#include "llvm/Target/TargetOptions.h"
|
2004-11-16 05:47:33 +01:00
|
|
|
using namespace llvm;
|
2004-06-21 18:55:25 +02:00
|
|
|
|
2004-08-10 01:03:59 +02:00
|
|
|
namespace {
|
2010-02-02 22:55:58 +01:00
|
|
|
class PPCCodeEmitter : public MachineFunctionPass {
|
2004-08-10 01:03:59 +02:00
|
|
|
TargetMachine &TM;
|
2010-02-02 22:55:58 +01:00
|
|
|
JITCodeEmitter &MCE;
|
2010-03-14 02:41:15 +01:00
|
|
|
MachineModuleInfo *MMI;
|
2010-02-02 22:55:58 +01:00
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired<MachineModuleInfo>();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char ID;
|
|
|
|
|
|
|
|
/// MovePCtoLROffset - When/if we see a MovePCtoLR instruction, we record
|
|
|
|
/// its address in the function into this pointer.
|
|
|
|
void *MovePCtoLROffset;
|
2009-05-30 22:51:52 +02:00
|
|
|
public:
|
2010-02-02 22:55:58 +01:00
|
|
|
|
|
|
|
PPCCodeEmitter(TargetMachine &tm, JITCodeEmitter &mce)
|
2010-08-06 20:33:48 +02:00
|
|
|
: MachineFunctionPass(ID), TM(tm), MCE(mce) {}
|
2009-05-30 22:51:52 +02:00
|
|
|
|
|
|
|
/// getBinaryCodeForInstr - This function, generated by the
|
|
|
|
/// CodeEmitterGenerator using TableGen, produces the binary encoding for
|
|
|
|
/// machine instructions.
|
2010-10-08 02:21:28 +02:00
|
|
|
unsigned getBinaryCodeForInstr(const MachineInstr &MI) const;
|
2009-05-30 22:51:52 +02:00
|
|
|
|
2010-11-15 06:57:53 +01:00
|
|
|
|
|
|
|
MachineRelocation GetRelocation(const MachineOperand &MO,
|
|
|
|
unsigned RelocID) const;
|
|
|
|
|
2009-05-30 22:51:52 +02:00
|
|
|
/// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
|
2009-06-01 21:57:37 +02:00
|
|
|
unsigned getMachineOpValue(const MachineInstr &MI,
|
2010-10-08 02:21:28 +02:00
|
|
|
const MachineOperand &MO) const;
|
2004-08-10 01:03:59 +02:00
|
|
|
|
2010-11-15 06:19:25 +01:00
|
|
|
unsigned get_crbitm_encoding(const MachineInstr &MI, unsigned OpNo) const;
|
2010-11-15 07:09:35 +01:00
|
|
|
unsigned getDirectBrEncoding(const MachineInstr &MI, unsigned OpNo) const;
|
|
|
|
unsigned getCondBrEncoding(const MachineInstr &MI, unsigned OpNo) const;
|
2010-11-15 07:33:39 +01:00
|
|
|
|
|
|
|
unsigned getHA16Encoding(const MachineInstr &MI, unsigned OpNo) const;
|
|
|
|
unsigned getLO16Encoding(const MachineInstr &MI, unsigned OpNo) 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 MachineInstr &MI, unsigned OpNo) const;
|
2010-11-15 09:02:41 +01:00
|
|
|
unsigned getMemRIXEncoding(const MachineInstr &MI, unsigned OpNo) const;
|
2010-11-15 07:33:39 +01:00
|
|
|
|
2004-08-10 01:03:59 +02:00
|
|
|
const char *getPassName() const { return "PowerPC Machine Code Emitter"; }
|
|
|
|
|
|
|
|
/// runOnMachineFunction - emits the given MachineFunction to memory
|
|
|
|
///
|
|
|
|
bool runOnMachineFunction(MachineFunction &MF);
|
|
|
|
|
|
|
|
/// emitBasicBlock - emits the given MachineBasicBlock to memory
|
|
|
|
///
|
|
|
|
void emitBasicBlock(MachineBasicBlock &MBB);
|
|
|
|
};
|
2009-05-30 22:51:52 +02:00
|
|
|
}
|
2009-09-20 04:20:51 +02:00
|
|
|
|
2010-02-02 22:55:58 +01:00
|
|
|
char PPCCodeEmitter::ID = 0;
|
|
|
|
|
2006-08-23 23:08:52 +02:00
|
|
|
/// createPPCCodeEmitterPass - Return a pass that emits the collected PPC code
|
|
|
|
/// to the specified MCE object.
|
2009-05-30 22:51:52 +02:00
|
|
|
FunctionPass *llvm::createPPCJITCodeEmitterPass(PPCTargetMachine &TM,
|
2009-06-01 21:57:37 +02:00
|
|
|
JITCodeEmitter &JCE) {
|
2010-02-02 22:55:58 +01:00
|
|
|
return new PPCCodeEmitter(TM, JCE);
|
2004-08-10 01:03:59 +02:00
|
|
|
}
|
|
|
|
|
2010-02-02 22:55:58 +01:00
|
|
|
bool PPCCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
|
2006-02-22 21:19:42 +01:00
|
|
|
assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
|
|
|
|
MF.getTarget().getRelocationModel() != Reloc::Static) &&
|
|
|
|
"JIT relocation model must be set to static or default!");
|
2008-02-13 19:39:37 +01:00
|
|
|
|
2010-03-14 02:41:15 +01:00
|
|
|
MMI = &getAnalysis<MachineModuleInfo>();
|
|
|
|
MCE.setModuleInfo(MMI);
|
2006-05-02 20:27:26 +02:00
|
|
|
do {
|
2006-12-08 05:54:03 +01:00
|
|
|
MovePCtoLROffset = 0;
|
2006-05-02 20:27:26 +02:00
|
|
|
MCE.startFunction(MF);
|
|
|
|
for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
|
|
|
|
emitBasicBlock(*BB);
|
|
|
|
} while (MCE.finishFunction(MF));
|
2004-10-21 03:42:02 +02:00
|
|
|
|
2004-08-10 01:03:59 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-02-02 22:55:58 +01:00
|
|
|
void PPCCodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
|
2006-05-03 19:10:41 +02:00
|
|
|
MCE.StartMachineBasicBlock(&MBB);
|
2009-09-20 04:20:51 +02:00
|
|
|
|
2004-10-23 20:28:01 +02:00
|
|
|
for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
|
2008-09-02 08:51:36 +02:00
|
|
|
const MachineInstr &MI = *I;
|
2009-10-06 04:19:11 +02:00
|
|
|
MCE.processDebugLoc(MI.getDebugLoc(), true);
|
2004-11-23 06:59:53 +01:00
|
|
|
switch (MI.getOpcode()) {
|
|
|
|
default:
|
2008-09-02 08:51:36 +02:00
|
|
|
MCE.emitWordBE(getBinaryCodeForInstr(MI));
|
2004-11-23 06:59:53 +01:00
|
|
|
break;
|
2010-07-17 00:20:36 +02:00
|
|
|
case TargetOpcode::PROLOG_LABEL:
|
2010-03-14 03:33:54 +01:00
|
|
|
case TargetOpcode::EH_LABEL:
|
|
|
|
MCE.emitLabel(MI.getOperand(0).getMCSymbol());
|
|
|
|
break;
|
2010-02-09 20:54:29 +01:00
|
|
|
case TargetOpcode::IMPLICIT_DEF:
|
|
|
|
case TargetOpcode::KILL:
|
2008-03-17 07:56:52 +01:00
|
|
|
break; // pseudo opcode, no side effects
|
2004-11-23 06:59:53 +01:00
|
|
|
case PPC::MovePCtoLR:
|
2006-11-14 19:44:47 +01:00
|
|
|
case PPC::MovePCtoLR8:
|
2006-12-08 05:54:03 +01:00
|
|
|
assert(TM.getRelocationModel() == Reloc::PIC_);
|
|
|
|
MovePCtoLROffset = (void*)MCE.getCurrentPCValue();
|
|
|
|
MCE.emitWordBE(0x48000005); // bl 1
|
2004-11-23 06:59:53 +01:00
|
|
|
break;
|
|
|
|
}
|
2009-10-06 04:19:11 +02:00
|
|
|
MCE.processDebugLoc(MI.getDebugLoc(), false);
|
2004-10-23 20:28:01 +02:00
|
|
|
}
|
2004-08-10 01:03:59 +02:00
|
|
|
}
|
|
|
|
|
2010-11-15 06:19:25 +01:00
|
|
|
unsigned PPCCodeEmitter::get_crbitm_encoding(const MachineInstr &MI,
|
|
|
|
unsigned OpNo) const {
|
|
|
|
const MachineOperand &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());
|
|
|
|
}
|
|
|
|
|
2010-11-15 06:57:53 +01:00
|
|
|
MachineRelocation PPCCodeEmitter::GetRelocation(const MachineOperand &MO,
|
|
|
|
unsigned RelocID) const {
|
2010-11-15 07:33:39 +01:00
|
|
|
// If in PIC mode, we need to encode the negated address of the
|
|
|
|
// 'movepctolr' into the unrelocated field. After relocation, we'll have
|
|
|
|
// &gv-&movepctolr-4 in the imm field. Once &movepctolr is added to the imm
|
|
|
|
// field, we get &gv. This doesn't happen for branch relocations, which are
|
|
|
|
// always implicitly pc relative.
|
|
|
|
intptr_t Cst = 0;
|
|
|
|
if (TM.getRelocationModel() == Reloc::PIC_) {
|
|
|
|
assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
|
|
|
|
Cst = -(intptr_t)MovePCtoLROffset - 4;
|
|
|
|
}
|
|
|
|
|
2010-11-15 06:57:53 +01:00
|
|
|
if (MO.isGlobal())
|
|
|
|
return MachineRelocation::getGV(MCE.getCurrentPCOffset(), RelocID,
|
2010-11-15 07:33:39 +01:00
|
|
|
const_cast<GlobalValue *>(MO.getGlobal()),
|
|
|
|
Cst, isa<Function>(MO.getGlobal()));
|
2010-11-15 06:57:53 +01:00
|
|
|
if (MO.isSymbol())
|
|
|
|
return MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
|
2010-11-15 07:33:39 +01:00
|
|
|
RelocID, MO.getSymbolName(), Cst);
|
2010-11-15 06:57:53 +01:00
|
|
|
if (MO.isCPI())
|
|
|
|
return MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
|
2010-11-15 07:33:39 +01:00
|
|
|
RelocID, MO.getIndex(), Cst);
|
2010-11-15 06:57:53 +01:00
|
|
|
|
|
|
|
if (MO.isMBB())
|
2010-11-15 23:50:50 +01:00
|
|
|
return MachineRelocation::getBB(MCE.getCurrentPCOffset(),
|
|
|
|
RelocID, MO.getMBB());
|
2010-11-15 06:57:53 +01:00
|
|
|
|
|
|
|
assert(MO.isJTI());
|
|
|
|
return MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
|
2010-11-15 07:33:39 +01:00
|
|
|
RelocID, MO.getIndex(), Cst);
|
2010-11-15 06:57:53 +01:00
|
|
|
}
|
|
|
|
|
2010-11-15 07:09:35 +01:00
|
|
|
unsigned PPCCodeEmitter::getDirectBrEncoding(const MachineInstr &MI,
|
|
|
|
unsigned OpNo) const {
|
2010-11-15 06:57:53 +01:00
|
|
|
const MachineOperand &MO = MI.getOperand(OpNo);
|
|
|
|
if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO);
|
|
|
|
|
|
|
|
MCE.addRelocation(GetRelocation(MO, PPC::reloc_pcrel_bx));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-11-15 07:09:35 +01:00
|
|
|
unsigned PPCCodeEmitter::getCondBrEncoding(const MachineInstr &MI,
|
|
|
|
unsigned OpNo) const {
|
|
|
|
const MachineOperand &MO = MI.getOperand(OpNo);
|
|
|
|
MCE.addRelocation(GetRelocation(MO, PPC::reloc_pcrel_bcx));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-11-15 07:33:39 +01:00
|
|
|
unsigned PPCCodeEmitter::getHA16Encoding(const MachineInstr &MI,
|
|
|
|
unsigned OpNo) const {
|
|
|
|
const MachineOperand &MO = MI.getOperand(OpNo);
|
|
|
|
if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO);
|
|
|
|
|
|
|
|
MCE.addRelocation(GetRelocation(MO, PPC::reloc_absolute_high));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned PPCCodeEmitter::getLO16Encoding(const MachineInstr &MI,
|
|
|
|
unsigned OpNo) const {
|
|
|
|
const MachineOperand &MO = MI.getOperand(OpNo);
|
|
|
|
if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO);
|
|
|
|
|
|
|
|
MCE.addRelocation(GetRelocation(MO, PPC::reloc_absolute_low));
|
|
|
|
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 PPCCodeEmitter::getMemRIEncoding(const MachineInstr &MI,
|
|
|
|
unsigned OpNo) 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)) << 16;
|
|
|
|
|
|
|
|
const MachineOperand &MO = MI.getOperand(OpNo);
|
|
|
|
if (MO.isImm())
|
|
|
|
return (getMachineOpValue(MI, MO) & 0xFFFF) | RegBits;
|
|
|
|
|
|
|
|
// Add a fixup for the displacement field.
|
|
|
|
MCE.addRelocation(GetRelocation(MO, PPC::reloc_absolute_low));
|
|
|
|
return RegBits;
|
|
|
|
}
|
|
|
|
|
2010-11-15 09:02:41 +01:00
|
|
|
unsigned PPCCodeEmitter::getMemRIXEncoding(const MachineInstr &MI,
|
|
|
|
unsigned OpNo) const {
|
|
|
|
// Encode (imm, reg) as a memrix, which has the low 14-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)) << 14;
|
|
|
|
|
2010-11-15 07:33:39 +01:00
|
|
|
const MachineOperand &MO = MI.getOperand(OpNo);
|
2010-11-15 09:02:41 +01:00
|
|
|
if (MO.isImm())
|
|
|
|
return (getMachineOpValue(MI, MO) & 0x3FFF) | RegBits;
|
2010-11-15 07:33:39 +01:00
|
|
|
|
|
|
|
MCE.addRelocation(GetRelocation(MO, PPC::reloc_absolute_low_ix));
|
2010-11-15 09:02:41 +01:00
|
|
|
return RegBits;
|
2010-11-15 07:33:39 +01:00
|
|
|
}
|
|
|
|
|
2010-11-15 06:19:25 +01:00
|
|
|
|
2008-09-02 08:51:36 +02:00
|
|
|
unsigned PPCCodeEmitter::getMachineOpValue(const MachineInstr &MI,
|
2010-10-08 02:21:28 +02:00
|
|
|
const MachineOperand &MO) const {
|
2005-04-22 01:30:14 +02:00
|
|
|
|
2008-10-03 17:45:36 +02:00
|
|
|
if (MO.isReg()) {
|
2010-11-16 01:55:51 +01:00
|
|
|
// MTCRF/MFOCRF should go through get_crbitm_encoding for the CR operand.
|
|
|
|
// The GPR operand should come through here though.
|
|
|
|
assert((MI.getOpcode() != PPC::MTCRF && MI.getOpcode() != PPC::MFOCRF) ||
|
|
|
|
MO.getReg() < PPC::CR0 || MO.getReg() > PPC::CR7);
|
2010-11-15 06:19:25 +01:00
|
|
|
return PPCRegisterInfo::getRegisterNumbering(MO.getReg());
|
|
|
|
}
|
|
|
|
|
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();
|
2004-06-21 18:55:25 +02:00
|
|
|
}
|
|
|
|
|
2005-10-15 01:37:35 +02:00
|
|
|
#include "PPCGenCodeEmitter.inc"
|