split call operands out to their own encoding class, simplifying
[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 getCallTargetEncoding(const MachineInstr &MI, unsigned OpNo) const;
65     
66     const char *getPassName() const { return "PowerPC Machine Code Emitter"; }
67
68     /// runOnMachineFunction - emits the given MachineFunction to memory
69     ///
70     bool runOnMachineFunction(MachineFunction &MF);
71
72     /// emitBasicBlock - emits the given MachineBasicBlock to memory
73     ///
74     void emitBasicBlock(MachineBasicBlock &MBB);
75   };
76 }
77
78 char PPCCodeEmitter::ID = 0;
79
80 /// createPPCCodeEmitterPass - Return a pass that emits the collected PPC code
81 /// to the specified MCE object.
82 FunctionPass *llvm::createPPCJITCodeEmitterPass(PPCTargetMachine &TM,
83                                                 JITCodeEmitter &JCE) {
84   return new PPCCodeEmitter(TM, JCE);
85 }
86
87 bool PPCCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
88   assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
89           MF.getTarget().getRelocationModel() != Reloc::Static) &&
90          "JIT relocation model must be set to static or default!");
91
92   MMI = &getAnalysis<MachineModuleInfo>();
93   MCE.setModuleInfo(MMI);
94   do {
95     MovePCtoLROffset = 0;
96     MCE.startFunction(MF);
97     for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
98       emitBasicBlock(*BB);
99   } while (MCE.finishFunction(MF));
100
101   return false;
102 }
103
104 void PPCCodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
105   MCE.StartMachineBasicBlock(&MBB);
106
107   for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
108     const MachineInstr &MI = *I;
109     MCE.processDebugLoc(MI.getDebugLoc(), true);
110     switch (MI.getOpcode()) {
111     default:
112       MCE.emitWordBE(getBinaryCodeForInstr(MI));
113       break;
114     case TargetOpcode::PROLOG_LABEL:
115     case TargetOpcode::EH_LABEL:
116       MCE.emitLabel(MI.getOperand(0).getMCSymbol());
117       break;
118     case TargetOpcode::IMPLICIT_DEF:
119     case TargetOpcode::KILL:
120       break; // pseudo opcode, no side effects
121     case PPC::MovePCtoLR:
122     case PPC::MovePCtoLR8:
123       assert(TM.getRelocationModel() == Reloc::PIC_);
124       MovePCtoLROffset = (void*)MCE.getCurrentPCValue();
125       MCE.emitWordBE(0x48000005);   // bl 1
126       break;
127     }
128     MCE.processDebugLoc(MI.getDebugLoc(), false);
129   }
130 }
131
132 unsigned PPCCodeEmitter::get_crbitm_encoding(const MachineInstr &MI,
133                                              unsigned OpNo) const {
134   const MachineOperand &MO = MI.getOperand(OpNo);
135   assert((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MFOCRF) &&
136          (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7));
137   return 0x80 >> PPCRegisterInfo::getRegisterNumbering(MO.getReg());
138 }
139
140 MachineRelocation PPCCodeEmitter::GetRelocation(const MachineOperand &MO, 
141                                                 unsigned RelocID) const {
142   if (MO.isGlobal())
143     return MachineRelocation::getGV(MCE.getCurrentPCOffset(), RelocID,
144                                     const_cast<GlobalValue *>(MO.getGlobal()),0,
145                                     isa<Function>(MO.getGlobal()));
146   if (MO.isSymbol())
147     return MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
148                                         RelocID, MO.getSymbolName(), 0);
149   if (MO.isCPI())
150     return MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
151                                            RelocID, MO.getIndex(), 0);
152
153   if (MO.isMBB())
154     MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
155                                                RelocID, MO.getMBB()));
156   
157   assert(MO.isJTI());
158   return MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
159                                          RelocID, MO.getIndex(), 0);
160 }
161
162 unsigned PPCCodeEmitter::getCallTargetEncoding(const MachineInstr &MI,
163                                                unsigned OpNo) const {
164   const MachineOperand &MO = MI.getOperand(OpNo);
165   if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO);
166   
167   MCE.addRelocation(GetRelocation(MO, PPC::reloc_pcrel_bx));
168   return 0;
169 }
170
171
172 unsigned PPCCodeEmitter::getMachineOpValue(const MachineInstr &MI,
173                                            const MachineOperand &MO) const {
174
175   if (MO.isReg()) {
176     assert(MI.getOpcode() != PPC::MTCRF && MI.getOpcode() != PPC::MFOCRF);
177     return PPCRegisterInfo::getRegisterNumbering(MO.getReg());
178   }
179   
180   if (MO.isImm())
181     return MO.getImm();
182   
183   if (MO.isGlobal() || MO.isSymbol() || MO.isCPI() || MO.isJTI()) {
184     unsigned Reloc = 0;
185     assert((TM.getRelocationModel() != Reloc::PIC_ || MovePCtoLROffset) &&
186            "MovePCtoLR not seen yet?");
187     switch (MI.getOpcode()) {
188     default: MI.dump(); llvm_unreachable("Unknown instruction for relocation!");
189     case PPC::LIS:
190     case PPC::LIS8:
191     case PPC::ADDIS:
192     case PPC::ADDIS8:
193       Reloc = PPC::reloc_absolute_high;       // Pointer to symbol
194       break;
195     case PPC::LI:
196     case PPC::LI8:
197     case PPC::LA:
198     // Loads.
199     case PPC::LBZ:
200     case PPC::LBZ8:
201     case PPC::LHA:
202     case PPC::LHA8:
203     case PPC::LHZ:
204     case PPC::LHZ8:
205     case PPC::LWZ:
206     case PPC::LWZ8:
207     case PPC::LFS:
208     case PPC::LFD:
209
210     // Stores.
211     case PPC::STB:
212     case PPC::STB8:
213     case PPC::STH:
214     case PPC::STH8:
215     case PPC::STW:
216     case PPC::STW8:
217     case PPC::STFS:
218     case PPC::STFD:
219       Reloc = PPC::reloc_absolute_low;
220       break;
221
222     case PPC::LWA:
223     case PPC::LD:
224     case PPC::STD:
225     case PPC::STD_32:
226       Reloc = PPC::reloc_absolute_low_ix;
227       break;
228     }
229
230     MachineRelocation R = GetRelocation(MO, Reloc);
231
232     // If in PIC mode, we need to encode the negated address of the
233     // 'movepctolr' into the unrelocated field.  After relocation, we'll have
234     // &gv-&movepctolr-4 in the imm field.  Once &movepctolr is added to the imm
235     // field, we get &gv.  This doesn't happen for branch relocations, which are
236     // always implicitly pc relative.
237     if (TM.getRelocationModel() == Reloc::PIC_) {
238       assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
239       R.setConstantVal(-(intptr_t)MovePCtoLROffset - 4);
240     }
241     MCE.addRelocation(R);
242
243   } else if (MO.isMBB()) {
244     unsigned Reloc = 0;
245     unsigned Opcode = MI.getOpcode();
246     if (Opcode == PPC::B)
247       Reloc = PPC::reloc_pcrel_bx;
248     else // BCC instruction
249       Reloc = PPC::reloc_pcrel_bcx;
250
251     MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
252                                                Reloc, MO.getMBB()));
253   } else {
254 #ifndef NDEBUG
255     errs() << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
256 #endif
257     llvm_unreachable(0);
258   }
259
260   return 0;
261 }
262
263 #include "PPCGenCodeEmitter.inc"