implement support for the linux/ppc function call ABI. Patch by
[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 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 "PPCTargetMachine.h"
16 #include "PPCRelocations.h"
17 #include "PPC.h"
18 #include "llvm/Module.h"
19 #include "llvm/PassManager.h"
20 #include "llvm/CodeGen/MachineCodeEmitter.h"
21 #include "llvm/CodeGen/MachineFunctionPass.h"
22 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 #include "llvm/CodeGen/Passes.h"
24 #include "llvm/Support/Debug.h"                   
25 #include "llvm/Support/Compiler.h"
26 #include "llvm/Target/TargetOptions.h"
27 using namespace llvm;
28
29 namespace {
30   class VISIBILITY_HIDDEN PPCCodeEmitter : public MachineFunctionPass {
31     TargetMachine &TM;
32     MachineCodeEmitter &MCE;
33
34     /// MovePCtoLROffset - When/if we see a MovePCtoLR instruction, we record
35     /// its address in the function into this pointer.
36     void *MovePCtoLROffset;
37     
38     /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
39     ///
40     int getMachineOpValue(MachineInstr &MI, MachineOperand &MO);
41
42   public:
43     PPCCodeEmitter(TargetMachine &T, MachineCodeEmitter &M)
44       : TM(T), MCE(M) {}
45
46     const char *getPassName() const { return "PowerPC Machine Code Emitter"; }
47
48     /// runOnMachineFunction - emits the given MachineFunction to memory
49     ///
50     bool runOnMachineFunction(MachineFunction &MF);
51
52     /// emitBasicBlock - emits the given MachineBasicBlock to memory
53     ///
54     void emitBasicBlock(MachineBasicBlock &MBB);
55
56     /// getValueBit - return the particular bit of Val
57     ///
58     unsigned getValueBit(int64_t Val, unsigned bit) { return (Val >> bit) & 1; }
59
60     /// getBinaryCodeForInstr - This function, generated by the
61     /// CodeEmitterGenerator using TableGen, produces the binary encoding for
62     /// machine instructions.
63     ///
64     unsigned getBinaryCodeForInstr(MachineInstr &MI);
65   };
66 }
67
68 /// createPPCCodeEmitterPass - Return a pass that emits the collected PPC code
69 /// to the specified MCE object.
70 FunctionPass *llvm::createPPCCodeEmitterPass(PPCTargetMachine &TM,
71                                              MachineCodeEmitter &MCE) {
72   return new PPCCodeEmitter(TM, MCE);
73 }
74
75 #ifdef __APPLE__ 
76 extern "C" void sys_icache_invalidate(const void *Addr, size_t len);
77 #endif
78
79 bool PPCCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
80   assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
81           MF.getTarget().getRelocationModel() != Reloc::Static) &&
82          "JIT relocation model must be set to static or default!");
83   do {
84     MovePCtoLROffset = 0;
85     MCE.startFunction(MF);
86     for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
87       emitBasicBlock(*BB);
88   } while (MCE.finishFunction(MF));
89
90   return false;
91 }
92
93 void PPCCodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
94   MCE.StartMachineBasicBlock(&MBB);
95   
96   for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
97     MachineInstr &MI = *I;
98     switch (MI.getOpcode()) {
99     default:
100       MCE.emitWordBE(getBinaryCodeForInstr(*I));
101       break;
102     case PPC::IMPLICIT_DEF_GPRC:
103     case PPC::IMPLICIT_DEF_G8RC:
104     case PPC::IMPLICIT_DEF_F8:
105     case PPC::IMPLICIT_DEF_F4:
106     case PPC::IMPLICIT_DEF_VRRC:
107       break; // pseudo opcode, no side effects
108     case PPC::MovePCtoLR:
109     case PPC::MovePCtoLR8:
110       assert(TM.getRelocationModel() == Reloc::PIC_);
111       MovePCtoLROffset = (void*)MCE.getCurrentPCValue();
112       MCE.emitWordBE(0x48000005);   // bl 1
113       break;
114     }
115   }
116 }
117
118 int PPCCodeEmitter::getMachineOpValue(MachineInstr &MI, MachineOperand &MO) {
119
120   intptr_t rv = 0; // Return value; defaults to 0 for unhandled cases
121                    // or things that get fixed up later by the JIT.
122   if (MO.isRegister()) {
123     rv = PPCRegisterInfo::getRegisterNumbering(MO.getReg());
124
125     // Special encoding for MTCRF and MFOCRF, which uses a bit mask for the
126     // register, not the register number directly.
127     if ((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MFOCRF) &&
128         (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7)) {
129       rv = 0x80 >> rv;
130     }
131   } else if (MO.isImmediate()) {
132     rv = MO.getImmedValue();
133   } else if (MO.isGlobalAddress() || MO.isExternalSymbol() ||
134              MO.isConstantPoolIndex() || MO.isJumpTableIndex()) {
135     unsigned Reloc = 0;
136     if (MI.getOpcode() == PPC::BL_Macho || MI.getOpcode() == PPC::BL8_Macho ||
137         MI.getOpcode() == PPC::BL_ELF || MI.getOpcode() == PPC::BL8_ELF)
138       Reloc = PPC::reloc_pcrel_bx;
139     else {
140       if (TM.getRelocationModel() == Reloc::PIC_) {
141         assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
142       }
143       switch (MI.getOpcode()) {
144       default: MI.dump(); assert(0 && "Unknown instruction for relocation!");
145       case PPC::LIS:
146       case PPC::LIS8:
147       case PPC::ADDIS:
148       case PPC::ADDIS8:
149         Reloc = PPC::reloc_absolute_high;       // Pointer to symbol
150         break;
151       case PPC::LI:
152       case PPC::LI8:
153       case PPC::LA:
154       // Loads.
155       case PPC::LBZ:
156       case PPC::LBZ8:
157       case PPC::LHA:
158       case PPC::LHA8:
159       case PPC::LHZ:
160       case PPC::LHZ8:
161       case PPC::LWZ:
162       case PPC::LWZ8:
163       case PPC::LFS:
164       case PPC::LFD:
165       
166       // Stores.
167       case PPC::STB:
168       case PPC::STB8:
169       case PPC::STH:
170       case PPC::STH8:
171       case PPC::STW:
172       case PPC::STW8:
173       case PPC::STFS:
174       case PPC::STFD:
175         Reloc = PPC::reloc_absolute_low;
176         break;
177
178       case PPC::LWA:
179       case PPC::LD:
180       case PPC::STD:
181       case PPC::STD_32:
182         Reloc = PPC::reloc_absolute_low_ix;
183         break;
184       }
185     }
186     
187     MachineRelocation R;
188     if (MO.isGlobalAddress()) {
189       R = MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
190                                    MO.getGlobal(), 0);
191     } else if (MO.isExternalSymbol()) {
192       R = MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
193                                        Reloc, MO.getSymbolName(), 0);
194     } else if (MO.isConstantPoolIndex()) {
195       R = MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
196                                           Reloc, MO.getConstantPoolIndex(), 0);
197     } else {
198       assert(MO.isJumpTableIndex());
199       R = MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
200                                           Reloc, MO.getJumpTableIndex(), 0);
201     }
202     
203     // If in PIC mode, we need to encode the negated address of the
204     // 'movepctolr' into the unrelocated field.  After relocation, we'll have
205     // &gv-&movepctolr-4 in the imm field.  Once &movepctolr is added to the imm
206     // field, we get &gv.  This doesn't happen for branch relocations, which are
207     // always implicitly pc relative.
208     if (TM.getRelocationModel() == Reloc::PIC_ && Reloc != PPC::reloc_pcrel_bx){
209       assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
210       R.setConstantVal(-(intptr_t)MovePCtoLROffset - 4);
211     }
212     MCE.addRelocation(R);
213     
214   } else if (MO.isMachineBasicBlock()) {
215     unsigned Reloc = 0;
216     unsigned Opcode = MI.getOpcode();
217     if (Opcode == PPC::B || Opcode == PPC::BL_Macho ||
218         Opcode == PPC::BLA_Macho || Opcode == PPC::BL_ELF || 
219         Opcode == PPC::BLA_ELF)
220       Reloc = PPC::reloc_pcrel_bx;
221     else // BCC instruction
222       Reloc = PPC::reloc_pcrel_bcx;
223     MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
224                                                Reloc,
225                                                MO.getMachineBasicBlock()));
226   } else {
227     cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
228     abort();
229   }
230
231   return rv;
232 }
233
234 #include "PPCGenCodeEmitter.inc"
235