1 //===-- PPCCodeEmitter.cpp - JIT Code Emitter for PowerPC32 -------*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the PowerPC 32-bit CodeEmitter and associated machinery to
11 // JIT-compile bytecode to native PowerPC.
13 //===----------------------------------------------------------------------===//
15 #include "PPCTargetMachine.h"
16 #include "PPCRelocations.h"
18 #include "llvm/Module.h"
19 #include "llvm/PassManager.h"
20 #include "llvm/CodeGen/MachineCodeEmitter.h"
21 #include "llvm/CodeGen/MachineFunctionPass.h"
22 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 #include "llvm/CodeGen/Passes.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Target/TargetOptions.h"
30 class PPCCodeEmitter : public MachineFunctionPass {
32 MachineCodeEmitter &MCE;
34 // Tracks which instruction references which BasicBlock
35 std::vector<std::pair<MachineBasicBlock*, unsigned*> > BBRefs;
37 /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
39 int getMachineOpValue(MachineInstr &MI, MachineOperand &MO);
42 PPCCodeEmitter(TargetMachine &T, MachineCodeEmitter &M)
45 const char *getPassName() const { return "PowerPC Machine Code Emitter"; }
47 /// runOnMachineFunction - emits the given MachineFunction to memory
49 bool runOnMachineFunction(MachineFunction &MF);
51 /// emitBasicBlock - emits the given MachineBasicBlock to memory
53 void emitBasicBlock(MachineBasicBlock &MBB);
55 /// getValueBit - return the particular bit of Val
57 unsigned getValueBit(int64_t Val, unsigned bit) { return (Val >> bit) & 1; }
59 /// getBinaryCodeForInstr - This function, generated by the
60 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
61 /// machine instructions.
63 unsigned getBinaryCodeForInstr(MachineInstr &MI);
67 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to get
68 /// machine code emitted. This uses a MachineCodeEmitter object to handle
69 /// actually outputting the machine code and resolving things like the address
70 /// of functions. This method should returns true if machine code emission is
73 bool PPCTargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
74 MachineCodeEmitter &MCE) {
75 // Machine code emitter pass for PowerPC
76 PM.add(new PPCCodeEmitter(*this, MCE));
77 // Delete machine code for this function after emitting it
78 PM.add(createMachineCodeDeleter());
82 bool PPCCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
83 assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
84 MF.getTarget().getRelocationModel() != Reloc::Static) &&
85 "JIT relocation model must be set to static or default!");
89 MCE.startFunction(MF);
90 for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
92 } while (MCE.finishFunction(MF));
94 // Resolve branches to BasicBlocks for the entire function
95 for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
96 intptr_t Location = MCE.getMachineBasicBlockAddress(BBRefs[i].first);
97 unsigned *Ref = BBRefs[i].second;
98 DEBUG(std::cerr << "Fixup @ " << (void*)Ref << " to " << (void*)Location
100 unsigned Instr = *Ref;
101 intptr_t BranchTargetDisp = (Location - (intptr_t)Ref) >> 2;
103 switch (Instr >> 26) {
104 default: assert(0 && "Unknown branch user!");
105 case 18: // This is B or BL
106 *Ref |= (BranchTargetDisp & ((1 << 24)-1)) << 2;
108 case 16: // This is BLT,BLE,BEQ,BGE,BGT,BNE, or other bcx instruction
109 *Ref |= (BranchTargetDisp & ((1 << 14)-1)) << 2;
118 void PPCCodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
119 MCE.StartMachineBasicBlock(&MBB);
121 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
122 MachineInstr &MI = *I;
123 unsigned Opcode = MI.getOpcode();
124 switch (MI.getOpcode()) {
126 MCE.emitWordBE(getBinaryCodeForInstr(*I));
128 case PPC::IMPLICIT_DEF_GPR:
129 case PPC::IMPLICIT_DEF_F8:
130 case PPC::IMPLICIT_DEF_F4:
131 case PPC::IMPLICIT_DEF_VRRC:
132 break; // pseudo opcode, no side effects
133 case PPC::MovePCtoLR:
134 assert(0 && "CodeEmitter does not support MovePCtoLR instruction");
140 int PPCCodeEmitter::getMachineOpValue(MachineInstr &MI, MachineOperand &MO) {
142 intptr_t rv = 0; // Return value; defaults to 0 for unhandled cases
143 // or things that get fixed up later by the JIT.
144 if (MO.isRegister()) {
145 rv = PPCRegisterInfo::getRegisterNumbering(MO.getReg());
147 // Special encoding for MTCRF and MFOCRF, which uses a bit mask for the
148 // register, not the register number directly.
149 if ((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MFOCRF) &&
150 (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7)) {
153 } else if (MO.isImmediate()) {
154 rv = MO.getImmedValue();
155 } else if (MO.isGlobalAddress() || MO.isExternalSymbol()) {
157 if (MI.getOpcode() == PPC::BL)
158 Reloc = PPC::reloc_pcrel_bx;
160 switch (MI.getOpcode()) {
161 default: MI.dump(); assert(0 && "Unknown instruction for relocation!");
163 Reloc = PPC::reloc_absolute_high; // Pointer to symbol
178 Reloc = PPC::reloc_absolute_low;
182 if (MO.isGlobalAddress())
183 MCE.addRelocation(MachineRelocation(MCE.getCurrentPCOffset(),
184 Reloc, MO.getGlobal(), 0));
186 MCE.addRelocation(MachineRelocation(MCE.getCurrentPCOffset(),
187 Reloc, MO.getSymbolName(), 0));
188 } else if (MO.isMachineBasicBlock()) {
189 unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
190 BBRefs.push_back(std::make_pair(MO.getMachineBasicBlock(), CurrPC));
191 } else if (MO.isConstantPoolIndex() || MO.isJumpTableIndex()) {
192 if (MO.isConstantPoolIndex())
193 rv = MCE.getConstantPoolEntryAddress(MO.getConstantPoolIndex());
195 rv = MCE.getJumpTableEntryAddress(MO.getJumpTableIndex());
197 unsigned Opcode = MI.getOpcode();
198 if (Opcode == PPC::LIS || Opcode == PPC::ADDIS) {
199 // lis wants hi16(addr)
200 if ((short)rv < 0) rv += 1 << 16;
202 } else if (Opcode == PPC::LWZ || Opcode == PPC::LA ||
204 Opcode == PPC::LFS || Opcode == PPC::LFD) {
205 // These load opcodes want lo16(addr)
208 assert(0 && "Unknown constant pool or jump table using instruction!");
211 std::cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
218 #include "PPCGenCodeEmitter.inc"