Change from using MachineRelocation ctors to using static methods
[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/Target/TargetOptions.h"
26 #include <iostream>
27 using namespace llvm;
28
29 namespace {
30   class PPCCodeEmitter : public MachineFunctionPass {
31     TargetMachine &TM;
32     MachineCodeEmitter &MCE;
33
34     // Tracks which instruction references which BasicBlock
35     std::vector<std::pair<MachineBasicBlock*, unsigned*> > BBRefs;
36     
37     /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
38     ///
39     int getMachineOpValue(MachineInstr &MI, MachineOperand &MO);
40
41   public:
42     PPCCodeEmitter(TargetMachine &T, MachineCodeEmitter &M)
43       : TM(T), MCE(M) {}
44
45     const char *getPassName() const { return "PowerPC Machine Code Emitter"; }
46
47     /// runOnMachineFunction - emits the given MachineFunction to memory
48     ///
49     bool runOnMachineFunction(MachineFunction &MF);
50
51     /// emitBasicBlock - emits the given MachineBasicBlock to memory
52     ///
53     void emitBasicBlock(MachineBasicBlock &MBB);
54
55     /// getValueBit - return the particular bit of Val
56     ///
57     unsigned getValueBit(int64_t Val, unsigned bit) { return (Val >> bit) & 1; }
58
59     /// getBinaryCodeForInstr - This function, generated by the
60     /// CodeEmitterGenerator using TableGen, produces the binary encoding for
61     /// machine instructions.
62     ///
63     unsigned getBinaryCodeForInstr(MachineInstr &MI);
64   };
65 }
66
67 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to get
68 /// machine code emitted.  This uses a MachineCodeEmitter object to handle
69 /// actually outputting the machine code and resolving things like the address
70 /// of functions.  This method should returns true if machine code emission is
71 /// not supported.
72 ///
73 bool PPCTargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
74                                                   MachineCodeEmitter &MCE) {
75   // Machine code emitter pass for PowerPC
76   PM.add(new PPCCodeEmitter(*this, MCE));
77   // Delete machine code for this function after emitting it
78   PM.add(createMachineCodeDeleter());
79   return false;
80 }
81
82 bool PPCCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
83   assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
84           MF.getTarget().getRelocationModel() != Reloc::Static) &&
85          "JIT relocation model must be set to static or default!");
86   do {
87     BBRefs.clear();
88
89     MCE.startFunction(MF);
90     for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
91       emitBasicBlock(*BB);
92   } while (MCE.finishFunction(MF));
93
94   // Resolve branches to BasicBlocks for the entire function
95   for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
96     intptr_t Location = MCE.getMachineBasicBlockAddress(BBRefs[i].first);
97     unsigned *Ref = BBRefs[i].second;
98     DEBUG(std::cerr << "Fixup @ " << (void*)Ref << " to " << (void*)Location
99                     << "\n");
100     unsigned Instr = *Ref;
101     intptr_t BranchTargetDisp = (Location - (intptr_t)Ref) >> 2;
102
103     switch (Instr >> 26) {
104     default: assert(0 && "Unknown branch user!");
105     case 18:  // This is B or BL
106       *Ref |= (BranchTargetDisp & ((1 << 24)-1)) << 2;
107       break;
108     case 16:  // This is BLT,BLE,BEQ,BGE,BGT,BNE, or other bcx instruction
109       *Ref |= (BranchTargetDisp & ((1 << 14)-1)) << 2;
110       break;
111     }
112   }
113   BBRefs.clear();
114
115   return false;
116 }
117
118 void PPCCodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
119   MCE.StartMachineBasicBlock(&MBB);
120   
121   for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
122     MachineInstr &MI = *I;
123     unsigned Opcode = MI.getOpcode();
124     switch (MI.getOpcode()) {
125     default:
126       MCE.emitWordBE(getBinaryCodeForInstr(*I));
127       break;
128     case PPC::IMPLICIT_DEF_GPR:
129     case PPC::IMPLICIT_DEF_F8:
130     case PPC::IMPLICIT_DEF_F4:
131     case PPC::IMPLICIT_DEF_VRRC:
132       break; // pseudo opcode, no side effects
133     case PPC::MovePCtoLR:
134       assert(0 && "CodeEmitter does not support MovePCtoLR instruction");
135       break;
136     }
137   }
138 }
139
140 int PPCCodeEmitter::getMachineOpValue(MachineInstr &MI, MachineOperand &MO) {
141
142   intptr_t rv = 0; // Return value; defaults to 0 for unhandled cases
143                    // or things that get fixed up later by the JIT.
144   if (MO.isRegister()) {
145     rv = PPCRegisterInfo::getRegisterNumbering(MO.getReg());
146
147     // Special encoding for MTCRF and MFOCRF, which uses a bit mask for the
148     // register, not the register number directly.
149     if ((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MFOCRF) &&
150         (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7)) {
151       rv = 0x80 >> rv;
152     }
153   } else if (MO.isImmediate()) {
154     rv = MO.getImmedValue();
155   } else if (MO.isGlobalAddress() || MO.isExternalSymbol()) {
156     unsigned Reloc = 0;
157     if (MI.getOpcode() == PPC::BL)
158       Reloc = PPC::reloc_pcrel_bx;
159     else {
160       switch (MI.getOpcode()) {
161       default: MI.dump(); assert(0 && "Unknown instruction for relocation!");
162       case PPC::LIS:
163         Reloc = PPC::reloc_absolute_high;       // Pointer to symbol
164         break;
165       case PPC::LI:
166       case PPC::LA:
167       case PPC::LBZ:
168       case PPC::LHA:
169       case PPC::LHZ:
170       case PPC::LWZ:
171       case PPC::LFS:
172       case PPC::LFD:
173       case PPC::STB:
174       case PPC::STH:
175       case PPC::STW:
176       case PPC::STFS:
177       case PPC::STFD:
178         Reloc = PPC::reloc_absolute_low;
179         break;
180       }
181     }
182     if (MO.isGlobalAddress())
183       MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(),
184                                           Reloc, MO.getGlobal(), 0));
185     else
186       MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
187                                           Reloc, MO.getSymbolName(), 0));
188   } else if (MO.isMachineBasicBlock()) {
189     unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
190     BBRefs.push_back(std::make_pair(MO.getMachineBasicBlock(), CurrPC));
191   } else if (MO.isConstantPoolIndex() || MO.isJumpTableIndex()) {
192     if (MO.isConstantPoolIndex())
193       rv = MCE.getConstantPoolEntryAddress(MO.getConstantPoolIndex());
194     else
195       rv = MCE.getJumpTableEntryAddress(MO.getJumpTableIndex());
196
197     unsigned Opcode = MI.getOpcode();
198     if (Opcode == PPC::LIS || Opcode == PPC::ADDIS) {
199       // lis wants hi16(addr)
200       if ((short)rv < 0) rv += 1 << 16;
201       rv >>= 16;
202     } else if (Opcode == PPC::LWZ || Opcode == PPC::LA ||
203                Opcode == PPC::LI ||
204                Opcode == PPC::LFS || Opcode == PPC::LFD) {
205       // These load opcodes want lo16(addr)
206       rv &= 0xffff;
207     } else {
208       assert(0 && "Unknown constant pool or jump table using instruction!");
209     }
210   } else {
211     std::cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
212     abort();
213   }
214
215   return rv;
216 }
217
218 #include "PPCGenCodeEmitter.inc"
219