1 //===-- PPC32CodeEmitter.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 "PPC32TargetMachine.h"
16 #include "PPC32Relocations.h"
18 #include "llvm/Module.h"
19 #include "llvm/CodeGen/MachineCodeEmitter.h"
20 #include "llvm/CodeGen/MachineFunctionPass.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/Passes.h"
23 #include "llvm/Support/Debug.h"
27 class PPC32CodeEmitter : public MachineFunctionPass {
29 MachineCodeEmitter &MCE;
31 /// MovePCtoLROffset - When/if we see a MovePCtoLR instruction, we record
32 /// its address in the function into this pointer.
33 void *MovePCtoLROffset;
35 // Tracks which instruction references which BasicBlock
36 std::vector<std::pair<const BasicBlock*, unsigned*> > BBRefs;
37 // Tracks where each BasicBlock starts
38 std::map<const BasicBlock*, long> BBLocations;
40 /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
42 int getMachineOpValue(MachineInstr &MI, MachineOperand &MO);
45 PPC32CodeEmitter(TargetMachine &T, MachineCodeEmitter &M)
48 const char *getPassName() const { return "PowerPC Machine Code Emitter"; }
50 /// runOnMachineFunction - emits the given MachineFunction to memory
52 bool runOnMachineFunction(MachineFunction &MF);
54 /// emitBasicBlock - emits the given MachineBasicBlock to memory
56 void emitBasicBlock(MachineBasicBlock &MBB);
58 /// emitWord - write a 32-bit word to memory at the current PC
60 void emitWord(unsigned w) { MCE.emitWord(w); }
62 /// getValueBit - return the particular bit of Val
64 unsigned getValueBit(int64_t Val, unsigned bit) { return (Val >> bit) & 1; }
66 /// getBinaryCodeForInstr - This function, generated by the
67 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
68 /// machine instructions.
70 unsigned getBinaryCodeForInstr(MachineInstr &MI);
74 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to get
75 /// machine code emitted. This uses a MachineCodeEmitter object to handle
76 /// actually outputting the machine code and resolving things like the address
77 /// of functions. This method should returns true if machine code emission is
80 bool PPC32TargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
81 MachineCodeEmitter &MCE) {
82 // Machine code emitter pass for PowerPC
83 PM.add(new PPC32CodeEmitter(*this, MCE));
84 // Delete machine code for this function after emitting it
85 PM.add(createMachineCodeDeleter());
89 bool PPC32CodeEmitter::runOnMachineFunction(MachineFunction &MF) {
91 MCE.startFunction(MF);
92 MCE.emitConstantPool(MF.getConstantPool());
93 for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
95 MCE.finishFunction(MF);
97 // Resolve branches to BasicBlocks for the entire function
98 for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
99 intptr_t Location = BBLocations[BBRefs[i].first];
100 unsigned *Ref = BBRefs[i].second;
101 DEBUG(std::cerr << "Fixup @ " << (void*)Ref << " to " << (void*)Location
103 unsigned Instr = *Ref;
104 intptr_t BranchTargetDisp = (Location - (intptr_t)Ref) >> 2;
106 switch (Instr >> 26) {
107 default: assert(0 && "Unknown branch user!");
108 case 18: // This is B or BL
109 *Ref |= (BranchTargetDisp & ((1 << 24)-1)) << 2;
111 case 16: // This is BLT,BLE,BEQ,BGE,BGT,BNE, or other bcx instruction
112 *Ref |= (BranchTargetDisp & ((1 << 14)-1)) << 2;
122 void PPC32CodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
123 BBLocations[MBB.getBasicBlock()] = MCE.getCurrentPCValue();
124 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
125 MachineInstr &MI = *I;
126 unsigned Opcode = MI.getOpcode();
127 switch (MI.getOpcode()) {
129 emitWord(getBinaryCodeForInstr(*I));
131 case PPC::IMPLICIT_DEF:
132 break; // pseudo opcode, no side effects
133 case PPC::MovePCtoLR:
134 assert(MovePCtoLROffset == 0 &&
135 "Multiple MovePCtoLR instructions in the function?");
136 MovePCtoLROffset = (void*)(intptr_t)MCE.getCurrentPCValue();
137 emitWord(0x48000005); // bl 1
143 static unsigned enumRegToMachineReg(unsigned enumReg) {
145 case PPC::R0 : case PPC::F0 : case PPC::CR0: return 0;
146 case PPC::R1 : case PPC::F1 : case PPC::CR1: return 1;
147 case PPC::R2 : case PPC::F2 : case PPC::CR2: return 2;
148 case PPC::R3 : case PPC::F3 : case PPC::CR3: return 3;
149 case PPC::R4 : case PPC::F4 : case PPC::CR4: return 4;
150 case PPC::R5 : case PPC::F5 : case PPC::CR5: return 5;
151 case PPC::R6 : case PPC::F6 : case PPC::CR6: return 6;
152 case PPC::R7 : case PPC::F7 : case PPC::CR7: return 7;
153 case PPC::R8 : case PPC::F8 : return 8;
154 case PPC::R9 : case PPC::F9 : return 9;
155 case PPC::R10: case PPC::F10: return 10;
156 case PPC::R11: case PPC::F11: return 11;
157 case PPC::R12: case PPC::F12: return 12;
158 case PPC::R13: case PPC::F13: return 13;
159 case PPC::R14: case PPC::F14: return 14;
160 case PPC::R15: case PPC::F15: return 15;
161 case PPC::R16: case PPC::F16: return 16;
162 case PPC::R17: case PPC::F17: return 17;
163 case PPC::R18: case PPC::F18: return 18;
164 case PPC::R19: case PPC::F19: return 19;
165 case PPC::R20: case PPC::F20: return 20;
166 case PPC::R21: case PPC::F21: return 21;
167 case PPC::R22: case PPC::F22: return 22;
168 case PPC::R23: case PPC::F23: return 23;
169 case PPC::R24: case PPC::F24: return 24;
170 case PPC::R25: case PPC::F25: return 25;
171 case PPC::R26: case PPC::F26: return 26;
172 case PPC::R27: case PPC::F27: return 27;
173 case PPC::R28: case PPC::F28: return 28;
174 case PPC::R29: case PPC::F29: return 29;
175 case PPC::R30: case PPC::F30: return 30;
176 case PPC::R31: case PPC::F31: return 31;
178 std::cerr << "Unhandled reg in enumRegToRealReg!\n";
183 int PPC32CodeEmitter::getMachineOpValue(MachineInstr &MI, MachineOperand &MO) {
185 int rv = 0; // Return value; defaults to 0 for unhandled cases
186 // or things that get fixed up later by the JIT.
187 if (MO.isRegister()) {
188 rv = enumRegToMachineReg(MO.getReg());
189 } else if (MO.isImmediate()) {
190 rv = MO.getImmedValue();
191 } else if (MO.isGlobalAddress()) {
192 GlobalValue *GV = MO.getGlobal();
195 if (MI.getOpcode() == PPC::CALLpcrel)
196 Reloc = PPC::reloc_pcrel_bx;
198 assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
199 Offset = -((intptr_t)MovePCtoLROffset+4);
201 if (MI.getOpcode() == PPC::LOADHiAddr) {
202 if (GV->hasWeakLinkage() || GV->isExternal() || isa<Function>(GV))
203 Reloc = PPC::reloc_absolute_ptr_high; // Pointer to stub
205 Reloc = PPC::reloc_absolute_high; // Pointer to symbol
207 } else if (MI.getOpcode() == PPC::LA) {
208 assert(!(GV->hasWeakLinkage() || GV->isExternal() || isa<Function>(GV))
209 && "Something in the ISEL changed\n");
211 Reloc = PPC::reloc_absolute_low;
212 } else if (MI.getOpcode() == PPC::LWZ) {
213 Reloc = PPC::reloc_absolute_ptr_low;
215 assert((GV->hasWeakLinkage() || GV->isExternal() || isa<Function>(GV))&&
216 "Something in the ISEL changed\n");
218 // These don't show up for global value references AFAIK, only for
219 // constant pool refs: PPC::LFS, PPC::LFD
220 assert(0 && "Unknown instruction for relocation!");
223 MCE.addRelocation(MachineRelocation(MCE.getCurrentPCOffset(),
224 Reloc, MO.getGlobal(), Offset));
225 } else if (MO.isMachineBasicBlock()) {
226 const BasicBlock *BB = MO.getMachineBasicBlock()->getBasicBlock();
227 unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
228 BBRefs.push_back(std::make_pair(BB, CurrPC));
229 } else if (MO.isConstantPoolIndex()) {
230 unsigned index = MO.getConstantPoolIndex();
231 assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
232 rv = MCE.getConstantPoolEntryAddress(index) - (intptr_t)MovePCtoLROffset-4;
234 unsigned Opcode = MI.getOpcode();
235 if (Opcode == PPC::LOADHiAddr) {
236 // LoadHiAddr wants hi16(addr - &MovePCtoLR)
237 if ((short)rv < 0) rv += 1 << 16;
239 } else if (Opcode == PPC::LWZ || Opcode == PPC::LA ||
240 Opcode == PPC::LFS || Opcode == PPC::LFD) {
241 // These load opcodes want lo16(addr - &MovePCtoLR)
244 assert(0 && "Unknown constant pool using instruction!");
247 std::cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
254 #include "PPC32GenCodeEmitter.inc"