blob: 70bf77410725138ec5fbf9c214b1077d059ee47c [file] [log] [blame]
Misha Brukman3d9a6c22004-08-11 00:09:42 +00001//===-- PPC32CodeEmitter.cpp - JIT Code Emitter for PowerPC32 -----*- C++ -*-=//
Misha Brukman5dfe3a92004-06-21 16:55:25 +00002//
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//
Misha Brukmand37faba2004-10-14 06:07:25 +000010// This file defines the PowerPC 32-bit CodeEmitter and associated machinery to
11// JIT-compile bytecode to native PowerPC.
Misha Brukman5dfe3a92004-06-21 16:55:25 +000012//
13//===----------------------------------------------------------------------===//
14
Misha Brukman3d9a6c22004-08-11 00:09:42 +000015#include "PPC32JITInfo.h"
16#include "PPC32TargetMachine.h"
Misha Brukman3070e2f2004-10-21 01:42:02 +000017#include "llvm/Module.h"
Misha Brukmanb05daff2004-08-09 23:03:59 +000018#include "llvm/CodeGen/MachineCodeEmitter.h"
19#include "llvm/CodeGen/MachineFunctionPass.h"
20#include "llvm/CodeGen/Passes.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/Support/Debug.h"
Misha Brukman5dfe3a92004-06-21 16:55:25 +000022
23namespace llvm {
24
Misha Brukmanb05daff2004-08-09 23:03:59 +000025namespace {
Misha Brukman3070e2f2004-10-21 01:42:02 +000026 class JITResolver {
27 MachineCodeEmitter &MCE;
28
29 // LazyCodeGenMap - Keep track of call sites for functions that are to be
30 // lazily resolved.
31 std::map<unsigned, Function*> LazyCodeGenMap;
32
33 // LazyResolverMap - Keep track of the lazy resolver created for a
34 // particular function so that we can reuse them if necessary.
35 std::map<Function*, unsigned> LazyResolverMap;
36
37 public:
38 JITResolver(MachineCodeEmitter &mce) : MCE(mce) {}
39 unsigned getLazyResolver(Function *F);
40 unsigned addFunctionReference(unsigned Address, Function *F);
41
42 private:
43 unsigned emitStubForFunction(Function *F);
44 static void CompilationCallback();
45 unsigned resolveFunctionReference(unsigned RetAddr);
46 };
47
48 static JITResolver &getResolver(MachineCodeEmitter &MCE) {
49 static JITResolver *TheJITResolver = 0;
50 if (TheJITResolver == 0)
51 TheJITResolver = new JITResolver(MCE);
52 return *TheJITResolver;
53 }
54}
55
56unsigned JITResolver::getLazyResolver(Function *F) {
57 std::map<Function*, unsigned>::iterator I = LazyResolverMap.lower_bound(F);
58 if (I != LazyResolverMap.end() && I->first == F) return I->second;
59
60 unsigned Stub = emitStubForFunction(F);
61 LazyResolverMap.insert(I, std::make_pair(F, Stub));
62 return Stub;
63}
64
65/// addFunctionReference - This method is called when we need to emit the
66/// address of a function that has not yet been emitted, so we don't know the
67/// address. Instead, we emit a call to the CompilationCallback method, and
68/// keep track of where we are.
69///
70unsigned JITResolver::addFunctionReference(unsigned Address, Function *F) {
71 LazyCodeGenMap[Address] = F;
72 return (intptr_t)&JITResolver::CompilationCallback;
73}
74
75unsigned JITResolver::resolveFunctionReference(unsigned RetAddr) {
76 std::map<unsigned, Function*>::iterator I = LazyCodeGenMap.find(RetAddr);
77 assert(I != LazyCodeGenMap.end() && "Not in map!");
78 Function *F = I->second;
79 LazyCodeGenMap.erase(I);
80 return MCE.forceCompilationOf(F);
81}
82
83/// emitStubForFunction - This method is used by the JIT when it needs to emit
84/// the address of a function for a function whose code has not yet been
85/// generated. In order to do this, it generates a stub which jumps to the lazy
86/// function compiler, which will eventually get fixed to call the function
87/// directly.
88///
89unsigned JITResolver::emitStubForFunction(Function *F) {
90 std::cerr << "PPC32CodeEmitter::emitStubForFunction() unimplemented!\n";
91 abort();
92 return 0;
93}
94
95void JITResolver::CompilationCallback() {
96 std::cerr << "PPC32CodeEmitter: CompilationCallback() unimplemented!";
97 abort();
98}
99
100namespace {
Misha Brukman3d9a6c22004-08-11 00:09:42 +0000101 class PPC32CodeEmitter : public MachineFunctionPass {
Misha Brukmanb05daff2004-08-09 23:03:59 +0000102 TargetMachine &TM;
103 MachineCodeEmitter &MCE;
104
Misha Brukman3070e2f2004-10-21 01:42:02 +0000105 // Tracks which instruction references which BasicBlock
106 std::vector<std::pair<const BasicBlock*,
107 std::pair<unsigned*,MachineInstr*> > > BBRefs;
108 // Tracks where each BasicBlock starts
109 std::map<const BasicBlock*, long> BBLocations;
110
111 /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
112 ///
Misha Brukmand37faba2004-10-14 06:07:25 +0000113 int64_t getMachineOpValue(MachineInstr &MI, MachineOperand &MO);
114
Misha Brukman3070e2f2004-10-21 01:42:02 +0000115 unsigned getAddressOfExternalFunction(Function *F);
116
Misha Brukmanb05daff2004-08-09 23:03:59 +0000117 public:
Misha Brukman3d9a6c22004-08-11 00:09:42 +0000118 PPC32CodeEmitter(TargetMachine &T, MachineCodeEmitter &M)
Misha Brukmanb05daff2004-08-09 23:03:59 +0000119 : TM(T), MCE(M) {}
120
121 const char *getPassName() const { return "PowerPC Machine Code Emitter"; }
122
123 /// runOnMachineFunction - emits the given MachineFunction to memory
124 ///
125 bool runOnMachineFunction(MachineFunction &MF);
126
127 /// emitBasicBlock - emits the given MachineBasicBlock to memory
128 ///
129 void emitBasicBlock(MachineBasicBlock &MBB);
130
131 /// emitWord - write a 32-bit word to memory at the current PC
132 ///
133 void emitWord(unsigned w) { MCE.emitWord(w); }
Misha Brukmand37faba2004-10-14 06:07:25 +0000134
135 /// getValueBit - return the particular bit of Val
136 ///
137 unsigned getValueBit(int64_t Val, unsigned bit) { return (Val >> bit) & 1; }
Misha Brukmanb05daff2004-08-09 23:03:59 +0000138
Misha Brukman3070e2f2004-10-21 01:42:02 +0000139 /// getBinaryCodeForInstr - This function, generated by the
140 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
141 /// machine instructions.
Misha Brukmanb05daff2004-08-09 23:03:59 +0000142 ///
Misha Brukmand37faba2004-10-14 06:07:25 +0000143 unsigned getBinaryCodeForInstr(MachineInstr &MI);
Misha Brukmanb05daff2004-08-09 23:03:59 +0000144 };
145}
146
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000147/// addPassesToEmitMachineCode - Add passes to the specified pass manager to get
148/// machine code emitted. This uses a MachineCodeEmitter object to handle
149/// actually outputting the machine code and resolving things like the address
150/// of functions. This method should returns true if machine code emission is
151/// not supported.
152///
Misha Brukman3d9a6c22004-08-11 00:09:42 +0000153bool PPC32TargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
154 MachineCodeEmitter &MCE) {
Misha Brukman3070e2f2004-10-21 01:42:02 +0000155 // Keep as `true' until this is a functional JIT to allow llvm-gcc to build
156 return true;
157
Misha Brukmanb05daff2004-08-09 23:03:59 +0000158 // Machine code emitter pass for PowerPC
Misha Brukman3070e2f2004-10-21 01:42:02 +0000159 MachineCodeEmitter *M = &MCE;
160 DEBUG(M = MachineCodeEmitter::createDebugEmitter(MCE));
161 PM.add(new PPC32CodeEmitter(*this, *M));
Misha Brukmand37faba2004-10-14 06:07:25 +0000162 // Delete machine code for this function after emitting it
Misha Brukmanb05daff2004-08-09 23:03:59 +0000163 PM.add(createMachineCodeDeleter());
Misha Brukmanc982cfa2004-10-14 06:39:56 +0000164 return false;
Misha Brukmanb05daff2004-08-09 23:03:59 +0000165}
166
Misha Brukman3d9a6c22004-08-11 00:09:42 +0000167bool PPC32CodeEmitter::runOnMachineFunction(MachineFunction &MF) {
Misha Brukmanb05daff2004-08-09 23:03:59 +0000168 MCE.startFunction(MF);
169 MCE.emitConstantPool(MF.getConstantPool());
Misha Brukman3070e2f2004-10-21 01:42:02 +0000170 for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
171 emitBasicBlock(*BB);
Misha Brukmanb05daff2004-08-09 23:03:59 +0000172 MCE.finishFunction(MF);
Misha Brukman3070e2f2004-10-21 01:42:02 +0000173
174 // Resolve branches to BasicBlocks for the entire function
175 for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
176 long Location = BBLocations[BBRefs[i].first];
177 unsigned *Ref = BBRefs[i].second.first;
178 MachineInstr *MI = BBRefs[i].second.second;
179 DEBUG(std::cerr << "Fixup @ " << std::hex << Ref << " to 0x" << Location
180 << " in instr: " << std::dec << *MI);
181 for (unsigned ii = 0, ee = MI->getNumOperands(); ii != ee; ++ii) {
182 MachineOperand &op = MI->getOperand(ii);
183 if (op.isPCRelativeDisp()) {
184 // the instruction's branch target is made such that it branches to
185 // PC + (branchTarget * 4), so undo that arithmetic here:
186 // Location is the target of the branch
187 // Ref is the location of the instruction, and hence the PC
188 int64_t branchTarget = (Location - (long)Ref) >> 2;
189 MI->SetMachineOperandConst(ii, MachineOperand::MO_SignExtendedImmed,
190 branchTarget);
191 unsigned fixedInstr = PPC32CodeEmitter::getBinaryCodeForInstr(*MI);
192 MCE.emitWordAt(fixedInstr, Ref);
193 break;
194 }
195 }
196 }
197 BBRefs.clear();
198 BBLocations.clear();
199
Misha Brukmanb05daff2004-08-09 23:03:59 +0000200 return false;
201}
202
Misha Brukman3d9a6c22004-08-11 00:09:42 +0000203void PPC32CodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
Misha Brukmanb05daff2004-08-09 23:03:59 +0000204 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I)
205 emitWord(getBinaryCodeForInstr(*I));
206}
207
Misha Brukman3070e2f2004-10-21 01:42:02 +0000208unsigned PPC32CodeEmitter::getAddressOfExternalFunction(Function *F) {
209 static std::map<Function*, unsigned> ExternalFn2Addr;
210 std::map<Function*, unsigned>::iterator Addr = ExternalFn2Addr.find(F);
211
212 if (Addr == ExternalFn2Addr.end())
213 ExternalFn2Addr[F] = MCE.forceCompilationOf(F);
214 return ExternalFn2Addr[F];
215}
216
Misha Brukmand37faba2004-10-14 06:07:25 +0000217int64_t PPC32CodeEmitter::getMachineOpValue(MachineInstr &MI,
218 MachineOperand &MO) {
Misha Brukmanc982cfa2004-10-14 06:39:56 +0000219 int64_t rv = 0; // Return value; defaults to 0 for unhandled cases
220 // or things that get fixed up later by the JIT.
Misha Brukman3070e2f2004-10-21 01:42:02 +0000221 if (MO.isRegister()) {
Misha Brukmanc982cfa2004-10-14 06:39:56 +0000222 rv = MO.getReg();
223 } else if (MO.isImmediate()) {
224 rv = MO.getImmedValue();
Misha Brukmanc982cfa2004-10-14 06:39:56 +0000225 } else if (MO.isGlobalAddress()) {
Misha Brukman3070e2f2004-10-21 01:42:02 +0000226 GlobalValue *GV = MO.getGlobal();
227 intptr_t Addr = (intptr_t)MCE.getGlobalValueAddress(GV);
228 if (Addr == 0) {
229 if (Function *F = dyn_cast<Function>(GV)) {
230 if (F->isExternal())
231 rv = getAddressOfExternalFunction(F);
232 else {
233 // Function has not yet been code generated!
234 getResolver(MCE).addFunctionReference(MCE.getCurrentPCValue(), F);
235 // Delayed resolution...
236 return (intptr_t)getResolver(MCE).getLazyResolver(F);
237 }
238 } else if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
239 if (GVar->isExternal())
240 rv = MCE.getGlobalValueAddress(MO.getSymbolName());
241 else {
242 std::cerr << "PPC32CodeEmitter: External global addr not found: "
243 << *GVar;
244 abort();
245 }
246 }
247 }
248 if (MO.isPCRelative()) { // Global variable reference
249 rv = (Addr - MCE.getCurrentPCValue()) >> 2;
250 }
Misha Brukmanc982cfa2004-10-14 06:39:56 +0000251 } else if (MO.isMachineBasicBlock()) {
Misha Brukman3070e2f2004-10-21 01:42:02 +0000252 const BasicBlock *BB = MO.getMachineBasicBlock()->getBasicBlock();
253 unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
254 BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI)));
Misha Brukmanc982cfa2004-10-14 06:39:56 +0000255 } else if (MO.isConstantPoolIndex()) {
Misha Brukman3070e2f2004-10-21 01:42:02 +0000256 unsigned index = MO.getConstantPoolIndex();
257 rv = MCE.getConstantPoolEntryAddress(index);
258 } else if (MO.isFrameIndex()) {
259 std::cerr << "PPC32CodeEmitter: error: Frame index unhandled!\n";
260 abort();
Misha Brukmanc982cfa2004-10-14 06:39:56 +0000261 } else {
262 std::cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
263 abort();
264 }
265
266 return rv;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000267}
268
Misha Brukmand37faba2004-10-14 06:07:25 +0000269
Misha Brukman3070e2f2004-10-21 01:42:02 +0000270void *PPC32JITInfo::getJITStubForFunction(Function *F, MachineCodeEmitter &MCE){
271 return (void*)((unsigned long)getResolver(MCE).getLazyResolver(F));
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000272}
273
Misha Brukman3d9a6c22004-08-11 00:09:42 +0000274void PPC32JITInfo::replaceMachineCodeForFunction (void *Old, void *New) {
Misha Brukmand37faba2004-10-14 06:07:25 +0000275 std::cerr << "PPC32JITInfo::replaceMachineCodeForFunction not implemented\n";
276 abort();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000277}
278
Misha Brukmand37faba2004-10-14 06:07:25 +0000279#include "PPC32GenCodeEmitter.inc"
Misha Brukmanb05daff2004-08-09 23:03:59 +0000280
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000281} // end llvm namespace
282