Split IMPLICIT_DEF into IMPLICIT_DEF_GPR and IMPLICIT_DEF_FP, so that the
[oota-llvm.git] / lib / Target / PowerPC / PPCCodeEmitter.cpp
1 //===-- PPC32CodeEmitter.cpp - JIT Code Emitter for PowerPC32 -----*- C++ -*-=//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the PowerPC 32-bit CodeEmitter and associated machinery to
11 // JIT-compile bytecode to native PowerPC.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "PPC32TargetMachine.h"
16 #include "PPC32Relocations.h"
17 #include "PowerPC.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"
24 using namespace llvm;
25
26 namespace {
27   class PPC32CodeEmitter : public MachineFunctionPass {
28     TargetMachine &TM;
29     MachineCodeEmitter &MCE;
30
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;
35
36     /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
37     ///
38     int getMachineOpValue(MachineInstr &MI, MachineOperand &MO);
39
40   public:
41     PPC32CodeEmitter(TargetMachine &T, MachineCodeEmitter &M)
42       : TM(T), MCE(M) {}
43
44     const char *getPassName() const { return "PowerPC Machine Code Emitter"; }
45
46     /// runOnMachineFunction - emits the given MachineFunction to memory
47     ///
48     bool runOnMachineFunction(MachineFunction &MF);
49
50     /// emitBasicBlock - emits the given MachineBasicBlock to memory
51     ///
52     void emitBasicBlock(MachineBasicBlock &MBB);
53
54     /// emitWord - write a 32-bit word to memory at the current PC
55     ///
56     void emitWord(unsigned w) { MCE.emitWord(w); }
57
58     /// getValueBit - return the particular bit of Val
59     ///
60     unsigned getValueBit(int64_t Val, unsigned bit) { return (Val >> bit) & 1; }
61
62     /// getBinaryCodeForInstr - This function, generated by the
63     /// CodeEmitterGenerator using TableGen, produces the binary encoding for
64     /// machine instructions.
65     ///
66     unsigned getBinaryCodeForInstr(MachineInstr &MI);
67   };
68 }
69
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
74 /// not supported.
75 ///
76 bool PPC32TargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
77                                                     MachineCodeEmitter &MCE) {
78   // Machine code emitter pass for PowerPC
79   PM.add(new PPC32CodeEmitter(*this, MCE));
80   // Delete machine code for this function after emitting it
81   PM.add(createMachineCodeDeleter());
82   return false;
83 }
84
85 bool PPC32CodeEmitter::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)
89     emitBasicBlock(*BB);
90   MCE.finishFunction(MF);
91
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
97                     << "\n");
98     unsigned Instr = *Ref;
99     intptr_t BranchTargetDisp = (Location - (intptr_t)Ref) >> 2;
100
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;
105       break;
106     case 16:  // This is BLT,BLE,BEQ,BGE,BGT,BNE, or other bcx instruction
107       *Ref |= (BranchTargetDisp & ((1 << 14)-1)) << 2;
108       break;
109     }
110   }
111   BBRefs.clear();
112   BBLocations.clear();
113
114   return false;
115 }
116
117 void PPC32CodeEmitter::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()) {
124     default:
125       emitWord(getBinaryCodeForInstr(*I));
126       break;
127     case PPC::IMPLICIT_DEF_GPR:
128     case PPC::IMPLICIT_DEF_FP:
129       break; // pseudo opcode, no side effects
130     case PPC::MovePCtoLR:
131       assert(0 && "CodeEmitter does not support MovePCtoLR instruction");
132       break;
133     }
134   }
135 }
136
137 static unsigned enumRegToMachineReg(unsigned enumReg) {
138   switch (enumReg) {
139   case PPC::R0 :  case PPC::F0 :  case PPC::CR0:  return  0;
140   case PPC::R1 :  case PPC::F1 :  case PPC::CR1:  return  1;
141   case PPC::R2 :  case PPC::F2 :  case PPC::CR2:  return  2;
142   case PPC::R3 :  case PPC::F3 :  case PPC::CR3:  return  3;
143   case PPC::R4 :  case PPC::F4 :  case PPC::CR4:  return  4;
144   case PPC::R5 :  case PPC::F5 :  case PPC::CR5:  return  5;
145   case PPC::R6 :  case PPC::F6 :  case PPC::CR6:  return  6;
146   case PPC::R7 :  case PPC::F7 :  case PPC::CR7:  return  7;
147   case PPC::R8 :  case PPC::F8 :  return  8;
148   case PPC::R9 :  case PPC::F9 :  return  9;
149   case PPC::R10:  case PPC::F10:  return 10;
150   case PPC::R11:  case PPC::F11:  return 11;
151   case PPC::R12:  case PPC::F12:  return 12;
152   case PPC::R13:  case PPC::F13:  return 13;
153   case PPC::R14:  case PPC::F14:  return 14;
154   case PPC::R15:  case PPC::F15:  return 15;
155   case PPC::R16:  case PPC::F16:  return 16;
156   case PPC::R17:  case PPC::F17:  return 17;
157   case PPC::R18:  case PPC::F18:  return 18;
158   case PPC::R19:  case PPC::F19:  return 19;
159   case PPC::R20:  case PPC::F20:  return 20;
160   case PPC::R21:  case PPC::F21:  return 21;
161   case PPC::R22:  case PPC::F22:  return 22;
162   case PPC::R23:  case PPC::F23:  return 23;
163   case PPC::R24:  case PPC::F24:  return 24;
164   case PPC::R25:  case PPC::F25:  return 25;
165   case PPC::R26:  case PPC::F26:  return 26;
166   case PPC::R27:  case PPC::F27:  return 27;
167   case PPC::R28:  case PPC::F28:  return 28;
168   case PPC::R29:  case PPC::F29:  return 29;
169   case PPC::R30:  case PPC::F30:  return 30;
170   case PPC::R31:  case PPC::F31:  return 31;
171   default:
172     std::cerr << "Unhandled reg in enumRegToRealReg!\n";
173     abort();
174   }
175 }
176
177 int PPC32CodeEmitter::getMachineOpValue(MachineInstr &MI, MachineOperand &MO) {
178
179   int rv = 0; // Return value; defaults to 0 for unhandled cases
180                   // or things that get fixed up later by the JIT.
181   if (MO.isRegister()) {
182     rv = enumRegToMachineReg(MO.getReg());
183
184     // Special encoding for MTCRF and MFOCRF, which uses a bit mask for the
185     // register, not the register number directly.
186     if ((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MFOCRF) &&
187         (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7)) {
188       rv = 0x80 >> rv;
189     }
190   } else if (MO.isImmediate()) {
191     rv = MO.getImmedValue();
192   } else if (MO.isGlobalAddress() || MO.isExternalSymbol()) {
193     bool isExternal = MO.isExternalSymbol() ||
194                       MO.getGlobal()->hasWeakLinkage() ||
195                       MO.getGlobal()->isExternal();
196     unsigned Reloc = 0;
197     if (MI.getOpcode() == PPC::CALLpcrel)
198       Reloc = PPC::reloc_pcrel_bx;
199     else {
200       switch (MI.getOpcode()) {
201       default: MI.dump(); assert(0 && "Unknown instruction for relocation!");
202       case PPC::LIS:
203         if (isExternal)
204           Reloc = PPC::reloc_absolute_ptr_high;   // Pointer to stub
205         else
206           Reloc = PPC::reloc_absolute_high;       // Pointer to symbol
207         break;
208       case PPC::LA:
209         assert(!isExternal && "Something in the ISEL changed\n");
210         Reloc = PPC::reloc_absolute_low;
211         break;
212       case PPC::LBZ:
213       case PPC::LHA:
214       case PPC::LHZ:
215       case PPC::LWZ:
216       case PPC::LFS:
217       case PPC::LFD:
218       case PPC::STB:
219       case PPC::STH:
220       case PPC::STW:
221       case PPC::STFS:
222       case PPC::STFD:
223         if (isExternal)
224           Reloc = PPC::reloc_absolute_ptr_low;
225         else
226           Reloc = PPC::reloc_absolute_low;
227         break;
228       }
229     }
230     if (MO.isGlobalAddress())
231       MCE.addRelocation(MachineRelocation(MCE.getCurrentPCOffset(),
232                                           Reloc, MO.getGlobal(), 0));
233     else
234       MCE.addRelocation(MachineRelocation(MCE.getCurrentPCOffset(),
235                                           Reloc, MO.getSymbolName(), 0));
236   } else if (MO.isMachineBasicBlock()) {
237     unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
238     BBRefs.push_back(std::make_pair(MO.getMachineBasicBlock(), CurrPC));
239   } else if (MO.isConstantPoolIndex()) {
240     unsigned index = MO.getConstantPoolIndex();
241     unsigned Opcode = MI.getOpcode();
242     rv = MCE.getConstantPoolEntryAddress(index);
243     if (Opcode == PPC::LIS) {
244       // lis wants hi16(addr)
245       if ((short)rv < 0) rv += 1 << 16;
246       rv >>= 16;
247     } else if (Opcode == PPC::LWZ || Opcode == PPC::LA ||
248                Opcode == PPC::LFS || Opcode == PPC::LFD) {
249       // These load opcodes want lo16(addr)
250       rv &= 0xffff;
251     } else {
252       assert(0 && "Unknown constant pool using instruction!");
253     }
254   } else {
255     std::cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
256     abort();
257   }
258
259   return rv;
260 }
261
262 #include "PPC32GenCodeEmitter.inc"
263