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 "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 PPCCodeEmitter : public MachineFunctionPass {
29 MachineCodeEmitter &MCE;
31 // Tracks which instruction references which BasicBlock
32 std::vector<std::pair<MachineBasicBlock*, unsigned*> > BBRefs;
33 // Tracks where each BasicBlock starts
34 std::map<MachineBasicBlock*, long> BBLocations;
36 /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
38 int getMachineOpValue(MachineInstr &MI, MachineOperand &MO);
41 PPCCodeEmitter(TargetMachine &T, MachineCodeEmitter &M)
44 const char *getPassName() const { return "PowerPC Machine Code Emitter"; }
46 /// runOnMachineFunction - emits the given MachineFunction to memory
48 bool runOnMachineFunction(MachineFunction &MF);
50 /// emitBasicBlock - emits the given MachineBasicBlock to memory
52 void emitBasicBlock(MachineBasicBlock &MBB);
54 /// emitWord - write a 32-bit word to memory at the current PC
56 void emitWord(unsigned w) { MCE.emitWord(w); }
58 /// getValueBit - return the particular bit of Val
60 unsigned getValueBit(int64_t Val, unsigned bit) { return (Val >> bit) & 1; }
62 /// getBinaryCodeForInstr - This function, generated by the
63 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
64 /// machine instructions.
66 unsigned getBinaryCodeForInstr(MachineInstr &MI);
70 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to get
71 /// machine code emitted. This uses a MachineCodeEmitter object to handle
72 /// actually outputting the machine code and resolving things like the address
73 /// of functions. This method should returns true if machine code emission is
76 bool PPC32TargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
77 MachineCodeEmitter &MCE) {
78 // Machine code emitter pass for PowerPC
79 PM.add(new PPCCodeEmitter(*this, MCE));
80 // Delete machine code for this function after emitting it
81 PM.add(createMachineCodeDeleter());
85 bool PPCCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
86 MCE.startFunction(MF);
87 MCE.emitConstantPool(MF.getConstantPool());
88 for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
90 MCE.finishFunction(MF);
92 // Resolve branches to BasicBlocks for the entire function
93 for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
94 intptr_t Location = BBLocations[BBRefs[i].first];
95 unsigned *Ref = BBRefs[i].second;
96 DEBUG(std::cerr << "Fixup @ " << (void*)Ref << " to " << (void*)Location
98 unsigned Instr = *Ref;
99 intptr_t BranchTargetDisp = (Location - (intptr_t)Ref) >> 2;
101 switch (Instr >> 26) {
102 default: assert(0 && "Unknown branch user!");
103 case 18: // This is B or BL
104 *Ref |= (BranchTargetDisp & ((1 << 24)-1)) << 2;
106 case 16: // This is BLT,BLE,BEQ,BGE,BGT,BNE, or other bcx instruction
107 *Ref |= (BranchTargetDisp & ((1 << 14)-1)) << 2;
117 void PPCCodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
118 assert(!PICEnabled && "CodeEmitter does not support PIC!");
119 BBLocations[&MBB] = MCE.getCurrentPCValue();
120 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
121 MachineInstr &MI = *I;
122 unsigned Opcode = MI.getOpcode();
123 switch (MI.getOpcode()) {
125 emitWord(getBinaryCodeForInstr(*I));
127 case PPC::IMPLICIT_DEF_GPR:
128 case PPC::IMPLICIT_DEF_F8:
129 case PPC::IMPLICIT_DEF_F4:
130 break; // pseudo opcode, no side effects
131 case PPC::MovePCtoLR:
132 assert(0 && "CodeEmitter does not support MovePCtoLR instruction");
138 static unsigned enumRegToMachineReg(unsigned enumReg) {
140 case PPC::R0 : case PPC::F0 : case PPC::CR0: return 0;
141 case PPC::R1 : case PPC::F1 : case PPC::CR1: return 1;
142 case PPC::R2 : case PPC::F2 : case PPC::CR2: return 2;
143 case PPC::R3 : case PPC::F3 : case PPC::CR3: return 3;
144 case PPC::R4 : case PPC::F4 : case PPC::CR4: return 4;
145 case PPC::R5 : case PPC::F5 : case PPC::CR5: return 5;
146 case PPC::R6 : case PPC::F6 : case PPC::CR6: return 6;
147 case PPC::R7 : case PPC::F7 : case PPC::CR7: return 7;
148 case PPC::R8 : case PPC::F8 : return 8;
149 case PPC::R9 : case PPC::F9 : return 9;
150 case PPC::R10: case PPC::F10: return 10;
151 case PPC::R11: case PPC::F11: return 11;
152 case PPC::R12: case PPC::F12: return 12;
153 case PPC::R13: case PPC::F13: return 13;
154 case PPC::R14: case PPC::F14: return 14;
155 case PPC::R15: case PPC::F15: return 15;
156 case PPC::R16: case PPC::F16: return 16;
157 case PPC::R17: case PPC::F17: return 17;
158 case PPC::R18: case PPC::F18: return 18;
159 case PPC::R19: case PPC::F19: return 19;
160 case PPC::R20: case PPC::F20: return 20;
161 case PPC::R21: case PPC::F21: return 21;
162 case PPC::R22: case PPC::F22: return 22;
163 case PPC::R23: case PPC::F23: return 23;
164 case PPC::R24: case PPC::F24: return 24;
165 case PPC::R25: case PPC::F25: return 25;
166 case PPC::R26: case PPC::F26: return 26;
167 case PPC::R27: case PPC::F27: return 27;
168 case PPC::R28: case PPC::F28: return 28;
169 case PPC::R29: case PPC::F29: return 29;
170 case PPC::R30: case PPC::F30: return 30;
171 case PPC::R31: case PPC::F31: return 31;
173 std::cerr << "Unhandled reg in enumRegToRealReg!\n";
178 int PPCCodeEmitter::getMachineOpValue(MachineInstr &MI, MachineOperand &MO) {
180 int rv = 0; // Return value; defaults to 0 for unhandled cases
181 // or things that get fixed up later by the JIT.
182 if (MO.isRegister()) {
183 rv = enumRegToMachineReg(MO.getReg());
185 // Special encoding for MTCRF and MFOCRF, which uses a bit mask for the
186 // register, not the register number directly.
187 if ((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MFOCRF) &&
188 (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7)) {
191 } else if (MO.isImmediate()) {
192 rv = MO.getImmedValue();
193 } else if (MO.isGlobalAddress() || MO.isExternalSymbol()) {
194 bool isExternal = MO.isExternalSymbol() ||
195 MO.getGlobal()->hasWeakLinkage() ||
196 MO.getGlobal()->isExternal();
198 if (MI.getOpcode() == PPC::CALLpcrel)
199 Reloc = PPC::reloc_pcrel_bx;
201 switch (MI.getOpcode()) {
202 default: MI.dump(); assert(0 && "Unknown instruction for relocation!");
205 Reloc = PPC::reloc_absolute_ptr_high; // Pointer to stub
207 Reloc = PPC::reloc_absolute_high; // Pointer to symbol
210 assert(!isExternal && "Something in the ISEL changed\n");
211 Reloc = PPC::reloc_absolute_low;
225 Reloc = PPC::reloc_absolute_ptr_low;
227 Reloc = PPC::reloc_absolute_low;
231 if (MO.isGlobalAddress())
232 MCE.addRelocation(MachineRelocation(MCE.getCurrentPCOffset(),
233 Reloc, MO.getGlobal(), 0));
235 MCE.addRelocation(MachineRelocation(MCE.getCurrentPCOffset(),
236 Reloc, MO.getSymbolName(), 0));
237 } else if (MO.isMachineBasicBlock()) {
238 unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
239 BBRefs.push_back(std::make_pair(MO.getMachineBasicBlock(), CurrPC));
240 } else if (MO.isConstantPoolIndex()) {
241 unsigned index = MO.getConstantPoolIndex();
242 unsigned Opcode = MI.getOpcode();
243 rv = MCE.getConstantPoolEntryAddress(index);
244 if (Opcode == PPC::LIS) {
245 // lis wants hi16(addr)
246 if ((short)rv < 0) rv += 1 << 16;
248 } else if (Opcode == PPC::LWZ || Opcode == PPC::LA ||
249 Opcode == PPC::LFS || Opcode == PPC::LFD) {
250 // These load opcodes want lo16(addr)
253 assert(0 && "Unknown constant pool using instruction!");
256 std::cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
263 #include "PPCGenCodeEmitter.inc"