[PowerPC] Support generic conditional branches in asm parser
[oota-llvm.git] / lib / Target / PowerPC / PPCCodeEmitter.cpp
1 //===-- PPCCodeEmitter.cpp - JIT Code Emitter for PowerPC -----------------===//
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 "PPC.h"
16 #include "PPCRelocations.h"
17 #include "PPCTargetMachine.h"
18 #include "llvm/CodeGen/JITCodeEmitter.h"
19 #include "llvm/CodeGen/MachineFunctionPass.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 #include "llvm/CodeGen/MachineModuleInfo.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/PassManager.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     uint64_t 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     unsigned getAbsDirectBrEncoding(const MachineInstr &MI,
67                                     unsigned OpNo) const;
68     unsigned getAbsCondBrEncoding(const MachineInstr &MI, unsigned OpNo) const;
69
70     unsigned getS16ImmEncoding(const MachineInstr &MI, unsigned OpNo) const;
71     unsigned getMemRIEncoding(const MachineInstr &MI, unsigned OpNo) const;
72     unsigned getMemRIXEncoding(const MachineInstr &MI, unsigned OpNo) const;
73     unsigned getTLSRegEncoding(const MachineInstr &MI, unsigned OpNo) const;
74
75     const char *getPassName() const { return "PowerPC Machine Code Emitter"; }
76
77     /// runOnMachineFunction - emits the given MachineFunction to memory
78     ///
79     bool runOnMachineFunction(MachineFunction &MF);
80
81     /// emitBasicBlock - emits the given MachineBasicBlock to memory
82     ///
83     void emitBasicBlock(MachineBasicBlock &MBB);
84   };
85 }
86
87 char PPCCodeEmitter::ID = 0;
88
89 /// createPPCCodeEmitterPass - Return a pass that emits the collected PPC code
90 /// to the specified MCE object.
91 FunctionPass *llvm::createPPCJITCodeEmitterPass(PPCTargetMachine &TM,
92                                                 JITCodeEmitter &JCE) {
93   return new PPCCodeEmitter(TM, JCE);
94 }
95
96 bool PPCCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
97   assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
98           MF.getTarget().getRelocationModel() != Reloc::Static) &&
99          "JIT relocation model must be set to static or default!");
100
101   MMI = &getAnalysis<MachineModuleInfo>();
102   MCE.setModuleInfo(MMI);
103   do {
104     MovePCtoLROffset = 0;
105     MCE.startFunction(MF);
106     for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
107       emitBasicBlock(*BB);
108   } while (MCE.finishFunction(MF));
109
110   return false;
111 }
112
113 void PPCCodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
114   MCE.StartMachineBasicBlock(&MBB);
115
116   for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
117     const MachineInstr &MI = *I;
118     MCE.processDebugLoc(MI.getDebugLoc(), true);
119     switch (MI.getOpcode()) {
120     default:
121       MCE.emitWordBE(getBinaryCodeForInstr(MI));
122       break;
123     case TargetOpcode::PROLOG_LABEL:
124     case TargetOpcode::EH_LABEL:
125       MCE.emitLabel(MI.getOperand(0).getMCSymbol());
126       break;
127     case TargetOpcode::IMPLICIT_DEF:
128     case TargetOpcode::KILL:
129       break; // pseudo opcode, no side effects
130     case PPC::MovePCtoLR:
131     case PPC::MovePCtoLR8:
132       assert(TM.getRelocationModel() == Reloc::PIC_);
133       MovePCtoLROffset = (void*)MCE.getCurrentPCValue();
134       MCE.emitWordBE(0x48000005);   // bl 1
135       break;
136     }
137     MCE.processDebugLoc(MI.getDebugLoc(), false);
138   }
139 }
140
141 unsigned PPCCodeEmitter::get_crbitm_encoding(const MachineInstr &MI,
142                                              unsigned OpNo) const {
143   const MachineOperand &MO = MI.getOperand(OpNo);
144   assert((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MTCRF8 ||
145             MI.getOpcode() == PPC::MFOCRF) &&
146          (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7));
147   return 0x80 >> TM.getRegisterInfo()->getEncodingValue(MO.getReg());
148 }
149
150 MachineRelocation PPCCodeEmitter::GetRelocation(const MachineOperand &MO, 
151                                                 unsigned RelocID) const {
152   // If in PIC mode, we need to encode the negated address of the
153   // 'movepctolr' into the unrelocated field.  After relocation, we'll have
154   // &gv-&movepctolr-4 in the imm field.  Once &movepctolr is added to the imm
155   // field, we get &gv.  This doesn't happen for branch relocations, which are
156   // always implicitly pc relative.
157   intptr_t Cst = 0;
158   if (TM.getRelocationModel() == Reloc::PIC_) {
159     assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
160     Cst = -(intptr_t)MovePCtoLROffset - 4;
161   }
162   
163   if (MO.isGlobal())
164     return MachineRelocation::getGV(MCE.getCurrentPCOffset(), RelocID,
165                                     const_cast<GlobalValue *>(MO.getGlobal()),
166                                     Cst, isa<Function>(MO.getGlobal()));
167   if (MO.isSymbol())
168     return MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
169                                         RelocID, MO.getSymbolName(), Cst);
170   if (MO.isCPI())
171     return MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
172                                            RelocID, MO.getIndex(), Cst);
173
174   if (MO.isMBB())
175     return MachineRelocation::getBB(MCE.getCurrentPCOffset(),
176                                     RelocID, MO.getMBB());
177   
178   assert(MO.isJTI());
179   return MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
180                                          RelocID, MO.getIndex(), Cst);
181 }
182
183 unsigned PPCCodeEmitter::getDirectBrEncoding(const MachineInstr &MI,
184                                              unsigned OpNo) const {
185   const MachineOperand &MO = MI.getOperand(OpNo);
186   if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO);
187   
188   MCE.addRelocation(GetRelocation(MO, PPC::reloc_pcrel_bx));
189   return 0;
190 }
191
192 unsigned PPCCodeEmitter::getCondBrEncoding(const MachineInstr &MI,
193                                            unsigned OpNo) const {
194   const MachineOperand &MO = MI.getOperand(OpNo);
195   MCE.addRelocation(GetRelocation(MO, PPC::reloc_pcrel_bcx));
196   return 0;
197 }
198
199 unsigned PPCCodeEmitter::getAbsDirectBrEncoding(const MachineInstr &MI,
200                                                 unsigned OpNo) const {
201   const MachineOperand &MO = MI.getOperand(OpNo);
202   if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO);
203
204   llvm_unreachable("Absolute branch relocations unsupported on the old JIT.");
205 }
206
207 unsigned PPCCodeEmitter::getAbsCondBrEncoding(const MachineInstr &MI,
208                                               unsigned OpNo) const {
209   llvm_unreachable("Absolute branch relocations unsupported on the old JIT.");
210 }
211
212 unsigned PPCCodeEmitter::getS16ImmEncoding(const MachineInstr &MI,
213                                            unsigned OpNo) const {
214   const MachineOperand &MO = MI.getOperand(OpNo);
215   if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO);
216
217   unsigned RelocID;
218   switch (MO.getTargetFlags() & PPCII::MO_ACCESS_MASK) {
219     default: llvm_unreachable("Unsupported target operand flags!");
220     case PPCII::MO_LO: RelocID = PPC::reloc_absolute_low; break;
221     case PPCII::MO_HA: RelocID = PPC::reloc_absolute_high; break;
222   }
223
224   MCE.addRelocation(GetRelocation(MO, RelocID));
225   return 0;
226 }
227
228 unsigned PPCCodeEmitter::getMemRIEncoding(const MachineInstr &MI,
229                                           unsigned OpNo) const {
230   // Encode (imm, reg) as a memri, which has the low 16-bits as the
231   // displacement and the next 5 bits as the register #.
232   assert(MI.getOperand(OpNo+1).isReg());
233   unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1)) << 16;
234   
235   const MachineOperand &MO = MI.getOperand(OpNo);
236   if (MO.isImm())
237     return (getMachineOpValue(MI, MO) & 0xFFFF) | RegBits;
238   
239   // Add a fixup for the displacement field.
240   MCE.addRelocation(GetRelocation(MO, PPC::reloc_absolute_low));
241   return RegBits;
242 }
243
244 unsigned PPCCodeEmitter::getMemRIXEncoding(const MachineInstr &MI,
245                                            unsigned OpNo) const {
246   // Encode (imm, reg) as a memrix, which has the low 14-bits as the
247   // displacement and the next 5 bits as the register #.
248   assert(MI.getOperand(OpNo+1).isReg());
249   unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1)) << 14;
250   
251   const MachineOperand &MO = MI.getOperand(OpNo);
252   if (MO.isImm())
253     return ((getMachineOpValue(MI, MO) >> 2) & 0x3FFF) | RegBits;
254   
255   MCE.addRelocation(GetRelocation(MO, PPC::reloc_absolute_low_ix));
256   return RegBits;
257 }
258
259
260 unsigned PPCCodeEmitter::getTLSRegEncoding(const MachineInstr &MI,
261                                            unsigned OpNo) const {
262   llvm_unreachable("TLS not supported on the old JIT.");
263   return 0;
264 }
265
266
267 unsigned PPCCodeEmitter::getMachineOpValue(const MachineInstr &MI,
268                                            const MachineOperand &MO) const {
269
270   if (MO.isReg()) {
271     // MTCRF/MFOCRF should go through get_crbitm_encoding for the CR operand.
272     // The GPR operand should come through here though.
273     assert((MI.getOpcode() != PPC::MTCRF && MI.getOpcode() != PPC::MTCRF8 &&
274              MI.getOpcode() != PPC::MFOCRF) ||
275            MO.getReg() < PPC::CR0 || MO.getReg() > PPC::CR7);
276     return TM.getRegisterInfo()->getEncodingValue(MO.getReg());
277   }
278   
279   assert(MO.isImm() &&
280          "Relocation required in an instruction that we cannot encode!");
281   return MO.getImm();
282 }
283
284 #include "PPCGenCodeEmitter.inc"