#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/Passes.h"
#include "llvm/Support/Debug.h"
+#include "llvm/Support/Visibility.h"
#include "llvm/Target/TargetOptions.h"
#include <iostream>
using namespace llvm;
namespace {
- class PPCCodeEmitter : public MachineFunctionPass {
+ class VISIBILITY_HIDDEN PPCCodeEmitter : public MachineFunctionPass {
TargetMachine &TM;
MachineCodeEmitter &MCE;
// Tracks which instruction references which BasicBlock
std::vector<std::pair<MachineBasicBlock*, unsigned*> > BBRefs;
- // Tracks where each BasicBlock starts
- std::map<MachineBasicBlock*, uint64_t> BBLocations;
-
+
/// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
///
int getMachineOpValue(MachineInstr &MI, MachineOperand &MO);
///
void emitBasicBlock(MachineBasicBlock &MBB);
- /// emitWord - write a 32-bit word to memory at the current PC
- ///
- void emitWord(unsigned w) { MCE.emitWord(w); }
-
/// getValueBit - return the particular bit of Val
///
unsigned getValueBit(int64_t Val, unsigned bit) { return (Val >> bit) & 1; }
assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
MF.getTarget().getRelocationModel() != Reloc::Static) &&
"JIT relocation model must be set to static or default!");
- MCE.startFunction(MF);
- MCE.emitConstantPool(MF.getConstantPool());
- MCE.initJumpTableInfo(MF.getJumpTableInfo());
- for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
- emitBasicBlock(*BB);
- MCE.emitJumpTableInfo(MF.getJumpTableInfo(), BBLocations);
- MCE.finishFunction(MF);
+ do {
+ BBRefs.clear();
+
+ MCE.startFunction(MF);
+ for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
+ emitBasicBlock(*BB);
+ } while (MCE.finishFunction(MF));
// Resolve branches to BasicBlocks for the entire function
for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
- intptr_t Location = BBLocations[BBRefs[i].first];
+ intptr_t Location = MCE.getMachineBasicBlockAddress(BBRefs[i].first);
unsigned *Ref = BBRefs[i].second;
DEBUG(std::cerr << "Fixup @ " << (void*)Ref << " to " << (void*)Location
<< "\n");
}
}
BBRefs.clear();
- BBLocations.clear();
return false;
}
void PPCCodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
- BBLocations[&MBB] = MCE.getCurrentPCValue();
+ MCE.StartMachineBasicBlock(&MBB);
+
for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
MachineInstr &MI = *I;
unsigned Opcode = MI.getOpcode();
switch (MI.getOpcode()) {
default:
- emitWord(getBinaryCodeForInstr(*I));
+ MCE.emitWordBE(getBinaryCodeForInstr(*I));
break;
- case PPC::IMPLICIT_DEF_GPR:
+ case PPC::IMPLICIT_DEF_GPRC:
+ case PPC::IMPLICIT_DEF_G8RC:
case PPC::IMPLICIT_DEF_F8:
case PPC::IMPLICIT_DEF_F4:
case PPC::IMPLICIT_DEF_VRRC:
int PPCCodeEmitter::getMachineOpValue(MachineInstr &MI, MachineOperand &MO) {
- int rv = 0; // Return value; defaults to 0 for unhandled cases
- // or things that get fixed up later by the JIT.
+ intptr_t rv = 0; // Return value; defaults to 0 for unhandled cases
+ // or things that get fixed up later by the JIT.
if (MO.isRegister()) {
rv = PPCRegisterInfo::getRegisterNumbering(MO.getReg());
Reloc = PPC::reloc_pcrel_bx;
else {
switch (MI.getOpcode()) {
- default: MI.dump(); assert(0 && "Unknown instruction for relocation!");
+ default: DEBUG(MI.dump()); assert(0 && "Unknown instruction for relocation!");
case PPC::LIS:
+ case PPC::LIS8:
+ case PPC::ADDIS8:
Reloc = PPC::reloc_absolute_high; // Pointer to symbol
break;
case PPC::LI:
+ case PPC::LI8:
case PPC::LA:
+ // Loads.
case PPC::LBZ:
case PPC::LHA:
case PPC::LHZ:
case PPC::LWZ:
case PPC::LFS:
case PPC::LFD:
+ case PPC::LWZ8:
+
+ // Stores.
case PPC::STB:
case PPC::STH:
case PPC::STW:
case PPC::STFD:
Reloc = PPC::reloc_absolute_low;
break;
+
+ case PPC::LWA:
+ case PPC::LD:
+ case PPC::STD:
+ case PPC::STD_32:
+ Reloc = PPC::reloc_absolute_low_ix;
+ break;
}
}
if (MO.isGlobalAddress())
- MCE.addRelocation(MachineRelocation(MCE.getCurrentPCOffset(),
+ MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(),
Reloc, MO.getGlobal(), 0));
else
- MCE.addRelocation(MachineRelocation(MCE.getCurrentPCOffset(),
+ MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
Reloc, MO.getSymbolName(), 0));
} else if (MO.isMachineBasicBlock()) {
unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
rv = MCE.getJumpTableEntryAddress(MO.getJumpTableIndex());
unsigned Opcode = MI.getOpcode();
- if (Opcode == PPC::LIS || Opcode == PPC::ADDIS) {
+ if (Opcode == PPC::LIS || Opcode == PPC::LIS8 ||
+ Opcode == PPC::ADDIS || Opcode == PPC::ADDIS8) {
// lis wants hi16(addr)
if ((short)rv < 0) rv += 1 << 16;
rv >>= 16;
- } else if (Opcode == PPC::LWZ || Opcode == PPC::LA ||
- Opcode == PPC::LI ||
+ } else if (Opcode == PPC::LWZ || Opcode == PPC::LWZ8 ||
+ Opcode == PPC::LA ||
+ Opcode == PPC::LI || Opcode == PPC::LI8 ||
Opcode == PPC::LFS || Opcode == PPC::LFD) {
// These load opcodes want lo16(addr)
rv &= 0xffff;
} else {
+ MI.dump();
assert(0 && "Unknown constant pool or jump table using instruction!");
}
} else {