add support for encoding the lo14 forms used for a few PPC64 addressing
[oota-llvm.git] / lib / Target / PowerPC / PPCCodeEmitter.cpp
1 //===-- PPCCodeEmitter.cpp - JIT Code Emitter for PowerPC32 -------*- C++ -*-=//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the PowerPC 32-bit CodeEmitter and associated machinery to
11 // JIT-compile bitcode to native PowerPC.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "PPCTargetMachine.h"
16 #include "PPCRelocations.h"
17 #include "PPC.h"
18 #include "llvm/Module.h"
19 #include "llvm/PassManager.h"
20 #include "llvm/CodeGen/JITCodeEmitter.h"
21 #include "llvm/CodeGen/MachineFunctionPass.h"
22 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 #include "llvm/CodeGen/MachineModuleInfo.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Target/TargetOptions.h"
27 using namespace llvm;
28
29 namespace {
30   class PPCCodeEmitter : public MachineFunctionPass {
31     TargetMachine &TM;
32     JITCodeEmitter &MCE;
33     MachineModuleInfo *MMI;
34     
35     void getAnalysisUsage(AnalysisUsage &AU) const {
36       AU.addRequired<MachineModuleInfo>();
37       MachineFunctionPass::getAnalysisUsage(AU);
38     }
39     
40     static char ID;
41     
42     /// MovePCtoLROffset - When/if we see a MovePCtoLR instruction, we record
43     /// its address in the function into this pointer.
44     void *MovePCtoLROffset;
45   public:
46     
47     PPCCodeEmitter(TargetMachine &tm, JITCodeEmitter &mce)
48       : MachineFunctionPass(ID), TM(tm), MCE(mce) {}
49
50     /// getBinaryCodeForInstr - This function, generated by the
51     /// CodeEmitterGenerator using TableGen, produces the binary encoding for
52     /// machine instructions.
53     unsigned getBinaryCodeForInstr(const MachineInstr &MI) const;
54
55     
56     MachineRelocation GetRelocation(const MachineOperand &MO,
57                                     unsigned RelocID) const;
58     
59     /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
60     unsigned getMachineOpValue(const MachineInstr &MI,
61                                const MachineOperand &MO) const;
62
63     unsigned get_crbitm_encoding(const MachineInstr &MI, unsigned OpNo) const;
64     unsigned getDirectBrEncoding(const MachineInstr &MI, unsigned OpNo) const;
65     unsigned getCondBrEncoding(const MachineInstr &MI, unsigned OpNo) const;
66
67     unsigned getHA16Encoding(const MachineInstr &MI, unsigned OpNo) const;
68     unsigned getLO16Encoding(const MachineInstr &MI, unsigned OpNo) const;
69     unsigned getMemRIXEncoding(const MachineInstr &MI, unsigned OpNo) const;
70
71     const char *getPassName() const { return "PowerPC Machine Code Emitter"; }
72
73     /// runOnMachineFunction - emits the given MachineFunction to memory
74     ///
75     bool runOnMachineFunction(MachineFunction &MF);
76
77     /// emitBasicBlock - emits the given MachineBasicBlock to memory
78     ///
79     void emitBasicBlock(MachineBasicBlock &MBB);
80   };
81 }
82
83 char PPCCodeEmitter::ID = 0;
84
85 /// createPPCCodeEmitterPass - Return a pass that emits the collected PPC code
86 /// to the specified MCE object.
87 FunctionPass *llvm::createPPCJITCodeEmitterPass(PPCTargetMachine &TM,
88                                                 JITCodeEmitter &JCE) {
89   return new PPCCodeEmitter(TM, JCE);
90 }
91
92 bool PPCCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
93   assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
94           MF.getTarget().getRelocationModel() != Reloc::Static) &&
95          "JIT relocation model must be set to static or default!");
96
97   MMI = &getAnalysis<MachineModuleInfo>();
98   MCE.setModuleInfo(MMI);
99   do {
100     MovePCtoLROffset = 0;
101     MCE.startFunction(MF);
102     for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
103       emitBasicBlock(*BB);
104   } while (MCE.finishFunction(MF));
105
106   return false;
107 }
108
109 void PPCCodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
110   MCE.StartMachineBasicBlock(&MBB);
111
112   for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
113     const MachineInstr &MI = *I;
114     MCE.processDebugLoc(MI.getDebugLoc(), true);
115     switch (MI.getOpcode()) {
116     default:
117       MCE.emitWordBE(getBinaryCodeForInstr(MI));
118       break;
119     case TargetOpcode::PROLOG_LABEL:
120     case TargetOpcode::EH_LABEL:
121       MCE.emitLabel(MI.getOperand(0).getMCSymbol());
122       break;
123     case TargetOpcode::IMPLICIT_DEF:
124     case TargetOpcode::KILL:
125       break; // pseudo opcode, no side effects
126     case PPC::MovePCtoLR:
127     case PPC::MovePCtoLR8:
128       assert(TM.getRelocationModel() == Reloc::PIC_);
129       MovePCtoLROffset = (void*)MCE.getCurrentPCValue();
130       MCE.emitWordBE(0x48000005);   // bl 1
131       break;
132     }
133     MCE.processDebugLoc(MI.getDebugLoc(), false);
134   }
135 }
136
137 unsigned PPCCodeEmitter::get_crbitm_encoding(const MachineInstr &MI,
138                                              unsigned OpNo) const {
139   const MachineOperand &MO = MI.getOperand(OpNo);
140   assert((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MFOCRF) &&
141          (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7));
142   return 0x80 >> PPCRegisterInfo::getRegisterNumbering(MO.getReg());
143 }
144
145 MachineRelocation PPCCodeEmitter::GetRelocation(const MachineOperand &MO, 
146                                                 unsigned RelocID) const {
147   // If in PIC mode, we need to encode the negated address of the
148   // 'movepctolr' into the unrelocated field.  After relocation, we'll have
149   // &gv-&movepctolr-4 in the imm field.  Once &movepctolr is added to the imm
150   // field, we get &gv.  This doesn't happen for branch relocations, which are
151   // always implicitly pc relative.
152   intptr_t Cst = 0;
153   if (TM.getRelocationModel() == Reloc::PIC_) {
154     assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
155     Cst = -(intptr_t)MovePCtoLROffset - 4;
156   }
157   
158   if (MO.isGlobal())
159     return MachineRelocation::getGV(MCE.getCurrentPCOffset(), RelocID,
160                                     const_cast<GlobalValue *>(MO.getGlobal()),
161                                     Cst, isa<Function>(MO.getGlobal()));
162   if (MO.isSymbol())
163     return MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
164                                         RelocID, MO.getSymbolName(), Cst);
165   if (MO.isCPI())
166     return MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
167                                            RelocID, MO.getIndex(), Cst);
168
169   if (MO.isMBB())
170     MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
171                                                RelocID, MO.getMBB()));
172   
173   assert(MO.isJTI());
174   return MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
175                                          RelocID, MO.getIndex(), Cst);
176 }
177
178 unsigned PPCCodeEmitter::getDirectBrEncoding(const MachineInstr &MI,
179                                              unsigned OpNo) const {
180   const MachineOperand &MO = MI.getOperand(OpNo);
181   if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO);
182   
183   MCE.addRelocation(GetRelocation(MO, PPC::reloc_pcrel_bx));
184   return 0;
185 }
186
187 unsigned PPCCodeEmitter::getCondBrEncoding(const MachineInstr &MI,
188                                            unsigned OpNo) const {
189   const MachineOperand &MO = MI.getOperand(OpNo);
190   MCE.addRelocation(GetRelocation(MO, PPC::reloc_pcrel_bcx));
191   return 0;
192 }
193
194 unsigned PPCCodeEmitter::getHA16Encoding(const MachineInstr &MI,
195                                          unsigned OpNo) const {
196   const MachineOperand &MO = MI.getOperand(OpNo);
197   if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO);
198
199   MCE.addRelocation(GetRelocation(MO, PPC::reloc_absolute_high));
200   return 0;
201 }
202
203 unsigned PPCCodeEmitter::getLO16Encoding(const MachineInstr &MI,
204                                          unsigned OpNo) const {
205   const MachineOperand &MO = MI.getOperand(OpNo);
206   if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO);
207   
208   MCE.addRelocation(GetRelocation(MO, PPC::reloc_absolute_low));
209   return 0;
210 }
211
212 unsigned PPCCodeEmitter::getMemRIXEncoding(const MachineInstr &MI,
213                                            unsigned OpNo) const {
214   // Encode (imm, reg) as a memrix, which has the low 14-bits as the
215   // displacement and the next 5 bits as the register #.
216   assert(MI.getOperand(OpNo+1).isReg());
217   unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1)) << 14;
218   
219   const MachineOperand &MO = MI.getOperand(OpNo);
220   if (MO.isImm())
221     return (getMachineOpValue(MI, MO) & 0x3FFF) | RegBits;
222   
223   MCE.addRelocation(GetRelocation(MO, PPC::reloc_absolute_low_ix));
224   return RegBits;
225 }
226
227
228 unsigned PPCCodeEmitter::getMachineOpValue(const MachineInstr &MI,
229                                            const MachineOperand &MO) const {
230
231   if (MO.isReg()) {
232     assert(MI.getOpcode() != PPC::MTCRF && MI.getOpcode() != PPC::MFOCRF);
233     return PPCRegisterInfo::getRegisterNumbering(MO.getReg());
234   }
235   
236   if (MO.isImm())
237     return MO.getImm();
238   
239   if (MO.isGlobal() || MO.isSymbol() || MO.isCPI() || MO.isJTI()) {
240     unsigned Reloc = 0;
241     assert((TM.getRelocationModel() != Reloc::PIC_ || MovePCtoLROffset) &&
242            "MovePCtoLR not seen yet?");
243     switch (MI.getOpcode()) {
244     default: MI.dump(); llvm_unreachable("Unknown instruction for relocation!");
245     // Loads.
246     case PPC::LBZ:
247     case PPC::LBZ8:
248     case PPC::LHA:
249     case PPC::LHA8:
250     case PPC::LHZ:
251     case PPC::LHZ8:
252     case PPC::LWZ:
253     case PPC::LWZ8:
254     case PPC::LFS:
255     case PPC::LFD:
256
257     // Stores.
258     case PPC::STB:
259     case PPC::STB8:
260     case PPC::STH:
261     case PPC::STH8:
262     case PPC::STW:
263     case PPC::STW8:
264     case PPC::STFS:
265     case PPC::STFD:
266       Reloc = PPC::reloc_absolute_low;
267       break;
268     }
269
270     MCE.addRelocation(GetRelocation(MO, Reloc));
271   } else {
272 #ifndef NDEBUG
273     errs() << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
274 #endif
275     llvm_unreachable(0);
276   }
277
278   return 0;
279 }
280
281 #include "PPCGenCodeEmitter.inc"