For PR786:
[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 #include <iostream>
28 using namespace llvm;
29
30 namespace {
31   class VISIBILITY_HIDDEN PPCCodeEmitter : public MachineFunctionPass {
32     TargetMachine &TM;
33     MachineCodeEmitter &MCE;
34
35     /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
36     ///
37     int getMachineOpValue(MachineInstr &MI, MachineOperand &MO);
38
39   public:
40     PPCCodeEmitter(TargetMachine &T, MachineCodeEmitter &M)
41       : TM(T), MCE(M) {}
42
43     const char *getPassName() const { return "PowerPC Machine Code Emitter"; }
44
45     /// runOnMachineFunction - emits the given MachineFunction to memory
46     ///
47     bool runOnMachineFunction(MachineFunction &MF);
48
49     /// emitBasicBlock - emits the given MachineBasicBlock to memory
50     ///
51     void emitBasicBlock(MachineBasicBlock &MBB);
52
53     /// getValueBit - return the particular bit of Val
54     ///
55     unsigned getValueBit(int64_t Val, unsigned bit) { return (Val >> bit) & 1; }
56
57     /// getBinaryCodeForInstr - This function, generated by the
58     /// CodeEmitterGenerator using TableGen, produces the binary encoding for
59     /// machine instructions.
60     ///
61     unsigned getBinaryCodeForInstr(MachineInstr &MI);
62   };
63 }
64
65 /// createPPCCodeEmitterPass - Return a pass that emits the collected PPC code
66 /// to the specified MCE object.
67 FunctionPass *llvm::createPPCCodeEmitterPass(PPCTargetMachine &TM,
68                                              MachineCodeEmitter &MCE) {
69   return new PPCCodeEmitter(TM, MCE);
70 }
71
72 #ifdef __APPLE__ 
73 extern "C" void sys_icache_invalidate(const void *Addr, size_t len);
74 #endif
75
76 bool PPCCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
77   assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
78           MF.getTarget().getRelocationModel() != Reloc::Static) &&
79          "JIT relocation model must be set to static or default!");
80   do {
81     MCE.startFunction(MF);
82     for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
83       emitBasicBlock(*BB);
84   } while (MCE.finishFunction(MF));
85
86   return false;
87 }
88
89 void PPCCodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
90   MCE.StartMachineBasicBlock(&MBB);
91   
92   for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
93     MachineInstr &MI = *I;
94     switch (MI.getOpcode()) {
95     default:
96       MCE.emitWordBE(getBinaryCodeForInstr(*I));
97       break;
98     case PPC::IMPLICIT_DEF_GPRC:
99     case PPC::IMPLICIT_DEF_G8RC:
100     case PPC::IMPLICIT_DEF_F8:
101     case PPC::IMPLICIT_DEF_F4:
102     case PPC::IMPLICIT_DEF_VRRC:
103       break; // pseudo opcode, no side effects
104     case PPC::MovePCtoLR:
105       assert(0 && "CodeEmitter does not support MovePCtoLR instruction");
106       break;
107     }
108   }
109 }
110
111 int PPCCodeEmitter::getMachineOpValue(MachineInstr &MI, MachineOperand &MO) {
112
113   intptr_t rv = 0; // Return value; defaults to 0 for unhandled cases
114                    // or things that get fixed up later by the JIT.
115   if (MO.isRegister()) {
116     rv = PPCRegisterInfo::getRegisterNumbering(MO.getReg());
117
118     // Special encoding for MTCRF and MFOCRF, which uses a bit mask for the
119     // register, not the register number directly.
120     if ((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MFOCRF) &&
121         (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7)) {
122       rv = 0x80 >> rv;
123     }
124   } else if (MO.isImmediate()) {
125     rv = MO.getImmedValue();
126   } else if (MO.isGlobalAddress() || MO.isExternalSymbol() ||
127              MO.isConstantPoolIndex() || MO.isJumpTableIndex()) {
128     unsigned Reloc = 0;
129     if (MI.getOpcode() == PPC::BL)
130       Reloc = PPC::reloc_pcrel_bx;
131     else {
132       switch (MI.getOpcode()) {
133       default: DEBUG(MI.dump()); assert(0 && "Unknown instruction for relocation!");
134       case PPC::LIS:
135       case PPC::LIS8:
136       case PPC::ADDIS:
137       case PPC::ADDIS8:
138         Reloc = PPC::reloc_absolute_high;       // Pointer to symbol
139         break;
140       case PPC::LI:
141       case PPC::LI8:
142       case PPC::LA:
143       // Loads.
144       case PPC::LBZ:
145       case PPC::LHA:
146       case PPC::LHZ:
147       case PPC::LWZ:
148       case PPC::LFS:
149       case PPC::LFD:
150       case PPC::LWZ8:
151       
152       // Stores.
153       case PPC::STB:
154       case PPC::STH:
155       case PPC::STW:
156       case PPC::STFS:
157       case PPC::STFD:
158         Reloc = PPC::reloc_absolute_low;
159         break;
160
161       case PPC::LWA:
162       case PPC::LD:
163       case PPC::STD:
164       case PPC::STD_32:
165         Reloc = PPC::reloc_absolute_low_ix;
166         break;
167       }
168     }
169     if (MO.isGlobalAddress())
170       MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(),
171                                           Reloc, MO.getGlobal(), 0));
172     else if (MO.isExternalSymbol())
173       MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
174                                           Reloc, MO.getSymbolName(), 0));
175     else if (MO.isConstantPoolIndex())
176       MCE.addRelocation(MachineRelocation::getConstPool(
177                                           MCE.getCurrentPCOffset(),
178                                           Reloc, MO.getConstantPoolIndex(), 0));
179     else // isJumpTableIndex
180       MCE.addRelocation(MachineRelocation::getJumpTable(
181                                           MCE.getCurrentPCOffset(),
182                                           Reloc, MO.getJumpTableIndex(), 0));
183   } else if (MO.isMachineBasicBlock()) {
184     unsigned Reloc = 0;
185     unsigned Opcode = MI.getOpcode();
186     if (Opcode == PPC::B || Opcode == PPC::BL || Opcode == PPC::BLA)
187       Reloc = PPC::reloc_pcrel_bx;
188     else
189       // BLT,BLE,BEQ,BGE,BGT,BNE, or other bcx instruction
190       Reloc = PPC::reloc_pcrel_bcx;
191     MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
192                                                Reloc,
193                                                MO.getMachineBasicBlock()));
194   } else {
195     std::cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
196     abort();
197   }
198
199   return rv;
200 }
201
202 #include "PPCGenCodeEmitter.inc"
203