blob: 695e04474ca4de0b1bc92aed27b992adc1644770 [file] [log] [blame]
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001//===-- Emitter.cpp - Write machine code to executable memory -------------===//
John Criswellb576c942003-10-20 19:43:21 +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//===----------------------------------------------------------------------===//
Chris Lattnerbd199fb2002-12-24 00:01:05 +00009//
Chris Lattner5be478f2004-11-20 03:46:14 +000010// This file defines a MachineCodeEmitter object that is used by the JIT to
11// write machine code to memory and remember where relocatable values are.
Chris Lattnerbd199fb2002-12-24 00:01:05 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattner3785fad2003-08-05 17:00:32 +000015#define DEBUG_TYPE "jit"
Chris Lattner4d326fa2003-12-20 01:46:27 +000016#include "JIT.h"
Chris Lattner2c0a6a12003-11-30 04:23:21 +000017#include "llvm/Constant.h"
18#include "llvm/Module.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000019#include "llvm/CodeGen/MachineCodeEmitter.h"
20#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner1cc08382003-01-13 01:00:12 +000021#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner5be478f2004-11-20 03:46:14 +000022#include "llvm/CodeGen/MachineRelocation.h"
Chris Lattner1cc08382003-01-13 01:00:12 +000023#include "llvm/Target/TargetData.h"
Chris Lattner5be478f2004-11-20 03:46:14 +000024#include "llvm/Target/TargetJITInfo.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000025#include "llvm/Support/Debug.h"
26#include "llvm/ADT/Statistic.h"
Reid Spencer52b0ba62004-09-11 04:31:03 +000027#include "llvm/System/Memory.h"
28
Chris Lattnerc19aade2003-12-08 08:06:28 +000029using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000030
Chris Lattnerbd199fb2002-12-24 00:01:05 +000031namespace {
Chris Lattnere7386562003-10-20 05:45:49 +000032 Statistic<> NumBytes("jit", "Number of bytes of machine code compiled");
Chris Lattner4d326fa2003-12-20 01:46:27 +000033 JIT *TheJIT = 0;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000034
Chris Lattner688506d2003-08-14 18:35:27 +000035 /// JITMemoryManager - Manage memory for the JIT code generation in a logical,
36 /// sane way. This splits a large block of MAP_NORESERVE'd memory into two
37 /// sections, one for function stubs, one for the functions themselves. We
38 /// have to do this because we may need to emit a function stub while in the
39 /// middle of emitting a function, and we don't know how large the function we
40 /// are emitting is. This never bothers to release the memory, because when
41 /// we are ready to destroy the JIT, the program exits.
42 class JITMemoryManager {
Reid Spencer33189e72004-09-13 22:38:11 +000043 sys::MemoryBlock MemBlock; // Virtual memory block allocated RWX
Chris Lattner688506d2003-08-14 18:35:27 +000044 unsigned char *MemBase; // Base of block of memory, start of stub mem
45 unsigned char *FunctionBase; // Start of the function body area
46 unsigned char *CurStubPtr, *CurFunctionPtr;
47 public:
48 JITMemoryManager();
49
50 inline unsigned char *allocateStub(unsigned StubSize);
51 inline unsigned char *startFunctionBody();
52 inline void endFunctionBody(unsigned char *FunctionEnd);
53 };
54}
55
Chris Lattner688506d2003-08-14 18:35:27 +000056JITMemoryManager::JITMemoryManager() {
57 // Allocate a 16M block of memory...
Reid Spencer33189e72004-09-13 22:38:11 +000058 MemBlock = sys::Memory::AllocateRWX((16 << 20));
Reid Spencer52b0ba62004-09-11 04:31:03 +000059 MemBase = reinterpret_cast<unsigned char*>(MemBlock.base());
Chris Lattner688506d2003-08-14 18:35:27 +000060 FunctionBase = MemBase + 512*1024; // Use 512k for stubs
61
62 // Allocate stubs backwards from the function base, allocate functions forward
63 // from the function base.
64 CurStubPtr = CurFunctionPtr = FunctionBase;
65}
66
67unsigned char *JITMemoryManager::allocateStub(unsigned StubSize) {
68 CurStubPtr -= StubSize;
69 if (CurStubPtr < MemBase) {
70 std::cerr << "JIT ran out of memory for function stubs!\n";
71 abort();
72 }
73 return CurStubPtr;
74}
75
76unsigned char *JITMemoryManager::startFunctionBody() {
Chris Lattner5be478f2004-11-20 03:46:14 +000077 // Round up to an even multiple of 8 bytes, this should eventually be target
Chris Lattner688506d2003-08-14 18:35:27 +000078 // specific.
Chris Lattner5be478f2004-11-20 03:46:14 +000079 return (unsigned char*)(((intptr_t)CurFunctionPtr + 7) & ~7);
Chris Lattner688506d2003-08-14 18:35:27 +000080}
81
82void JITMemoryManager::endFunctionBody(unsigned char *FunctionEnd) {
83 assert(FunctionEnd > CurFunctionPtr);
84 CurFunctionPtr = FunctionEnd;
85}
86
87
88
89namespace {
Brian Gaeke6020ddd2003-10-16 23:33:38 +000090 /// Emitter - The JIT implementation of the MachineCodeEmitter, which is used
Chris Lattner688506d2003-08-14 18:35:27 +000091 /// to output functions to memory for execution.
Chris Lattnerbd199fb2002-12-24 00:01:05 +000092 class Emitter : public MachineCodeEmitter {
Chris Lattner688506d2003-08-14 18:35:27 +000093 JITMemoryManager MemMgr;
94
Chris Lattnerbba1b6d2003-06-01 23:24:36 +000095 // CurBlock - The start of the current block of memory. CurByte - The
96 // current byte being emitted to.
Chris Lattner6125fdd2003-05-09 03:30:07 +000097 unsigned char *CurBlock, *CurByte;
98
99 // When outputting a function stub in the context of some other function, we
100 // save CurBlock and CurByte here.
101 unsigned char *SavedCurBlock, *SavedCurByte;
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000102
103 // ConstantPoolAddresses - Contains the location for each entry in the
104 // constant pool.
Chris Lattner1cc08382003-01-13 01:00:12 +0000105 std::vector<void*> ConstantPoolAddresses;
Chris Lattner5be478f2004-11-20 03:46:14 +0000106
107 /// Relocations - These are the relocations that the function needs, as
108 /// emitted.
109 std::vector<MachineRelocation> Relocations;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000110 public:
Chris Lattner4d326fa2003-12-20 01:46:27 +0000111 Emitter(JIT &jit) { TheJIT = &jit; }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000112
113 virtual void startFunction(MachineFunction &F);
114 virtual void finishFunction(MachineFunction &F);
Chris Lattner1cc08382003-01-13 01:00:12 +0000115 virtual void emitConstantPool(MachineConstantPool *MCP);
Chris Lattner6125fdd2003-05-09 03:30:07 +0000116 virtual void startFunctionStub(const Function &F, unsigned StubSize);
117 virtual void* finishFunctionStub(const Function &F);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000118 virtual void emitByte(unsigned char B);
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000119 virtual void emitWord(unsigned W);
Brian Gaekeaea1b582004-04-23 17:11:14 +0000120 virtual void emitWordAt(unsigned W, unsigned *Ptr);
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000121
Chris Lattner5be478f2004-11-20 03:46:14 +0000122 virtual void addRelocation(const MachineRelocation &MR) {
123 Relocations.push_back(MR);
124 }
125
126 virtual uint64_t getCurrentPCValue();
127 virtual uint64_t getCurrentPCOffset();
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000128 virtual uint64_t getGlobalValueAddress(GlobalValue *V);
Chris Lattner45730d72004-11-19 20:56:46 +0000129 virtual uint64_t getGlobalValueAddress(const char *Name);
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000130 virtual uint64_t getConstantPoolEntryAddress(unsigned Entry);
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000131
132 // forceCompilationOf - Force the compilation of the specified function, and
133 // return its address, because we REALLY need the address now.
134 //
135 // FIXME: This is JIT specific!
136 //
137 virtual uint64_t forceCompilationOf(Function *F);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000138 };
139}
140
Chris Lattner4d326fa2003-12-20 01:46:27 +0000141MachineCodeEmitter *JIT::createEmitter(JIT &jit) {
142 return new Emitter(jit);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000143}
144
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000145void Emitter::startFunction(MachineFunction &F) {
Chris Lattner688506d2003-08-14 18:35:27 +0000146 CurByte = CurBlock = MemMgr.startFunctionBody();
Chris Lattner4d326fa2003-12-20 01:46:27 +0000147 TheJIT->addGlobalMapping(F.getFunction(), CurBlock);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000148}
149
150void Emitter::finishFunction(MachineFunction &F) {
Chris Lattner688506d2003-08-14 18:35:27 +0000151 MemMgr.endFunctionBody(CurByte);
Chris Lattner1cc08382003-01-13 01:00:12 +0000152 ConstantPoolAddresses.clear();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000153 NumBytes += CurByte-CurBlock;
154
Chris Lattner5be478f2004-11-20 03:46:14 +0000155 if (!Relocations.empty()) {
156 // Resolve the relocations to concrete pointers.
157 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
158 MachineRelocation &MR = Relocations[i];
159 void *ResultPtr;
160 if (MR.isGlobalValue()) {
161 assert(0 && "Unimplemented!\n");
162 } else {
163 ResultPtr = TheJIT->getPointerToNamedFunction(MR.getString());
164 }
165 MR.setResultPointer(ResultPtr);
166 }
167
168 TheJIT->getJITInfo().relocate(CurBlock, &Relocations[0],
169 Relocations.size());
170 }
171
Brian Gaeke02c26b62003-06-30 18:06:20 +0000172 DEBUG(std::cerr << "Finished CodeGen of [" << (void*)CurBlock
Misha Brukman1d440852003-06-06 06:52:35 +0000173 << "] Function: " << F.getFunction()->getName()
Chris Lattner5be478f2004-11-20 03:46:14 +0000174 << ": " << CurByte-CurBlock << " bytes of text, "
175 << Relocations.size() << " relocations\n");
176 Relocations.clear();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000177}
178
Chris Lattner1cc08382003-01-13 01:00:12 +0000179void Emitter::emitConstantPool(MachineConstantPool *MCP) {
180 const std::vector<Constant*> &Constants = MCP->getConstants();
Chris Lattner2c0a6a12003-11-30 04:23:21 +0000181 if (Constants.empty()) return;
182
183 std::vector<unsigned> ConstantOffset;
184 ConstantOffset.reserve(Constants.size());
185
186 // Calculate how much space we will need for all the constants, and the offset
187 // each one will live in.
188 unsigned TotalSize = 0;
Chris Lattner1cc08382003-01-13 01:00:12 +0000189 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
Chris Lattner2c0a6a12003-11-30 04:23:21 +0000190 const Type *Ty = Constants[i]->getType();
Chris Lattner4d326fa2003-12-20 01:46:27 +0000191 unsigned Size = TheJIT->getTargetData().getTypeSize(Ty);
192 unsigned Alignment = TheJIT->getTargetData().getTypeAlignment(Ty);
Chris Lattner2c0a6a12003-11-30 04:23:21 +0000193 // Make sure to take into account the alignment requirements of the type.
194 TotalSize = (TotalSize + Alignment-1) & ~(Alignment-1);
195
196 // Remember the offset this element lives at.
197 ConstantOffset.push_back(TotalSize);
198 TotalSize += Size; // Reserve space for the constant.
199 }
200
201 // Now that we know how much memory to allocate, do so.
202 char *Pool = new char[TotalSize];
203
204 // Actually output all of the constants, and remember their addresses.
205 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
206 void *Addr = Pool + ConstantOffset[i];
Chris Lattner4d326fa2003-12-20 01:46:27 +0000207 TheJIT->InitializeMemory(Constants[i], Addr);
Misha Brukman91de3522003-11-30 00:50:53 +0000208 ConstantPoolAddresses.push_back(Addr);
Chris Lattner1cc08382003-01-13 01:00:12 +0000209 }
210}
211
Chris Lattner6125fdd2003-05-09 03:30:07 +0000212void Emitter::startFunctionStub(const Function &F, unsigned StubSize) {
213 SavedCurBlock = CurBlock; SavedCurByte = CurByte;
Chris Lattner688506d2003-08-14 18:35:27 +0000214 CurByte = CurBlock = MemMgr.allocateStub(StubSize);
Chris Lattner6125fdd2003-05-09 03:30:07 +0000215}
216
217void *Emitter::finishFunctionStub(const Function &F) {
218 NumBytes += CurByte-CurBlock;
219 DEBUG(std::cerr << "Finished CodeGen of [0x" << std::hex
Brian Gaeke88560c32004-10-29 18:22:45 +0000220 << (uintptr_t)CurBlock
Chris Lattner6125fdd2003-05-09 03:30:07 +0000221 << std::dec << "] Function stub for: " << F.getName()
222 << ": " << CurByte-CurBlock << " bytes of text\n");
223 std::swap(CurBlock, SavedCurBlock);
224 CurByte = SavedCurByte;
225 return SavedCurBlock;
226}
227
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000228void Emitter::emitByte(unsigned char B) {
229 *CurByte++ = B; // Write the byte to memory
230}
231
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000232void Emitter::emitWord(unsigned W) {
Chris Lattner688506d2003-08-14 18:35:27 +0000233 // This won't work if the endianness of the host and target don't agree! (For
234 // a JIT this can't happen though. :)
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000235 *(unsigned*)CurByte = W;
236 CurByte += sizeof(unsigned);
237}
238
Brian Gaekeaea1b582004-04-23 17:11:14 +0000239void Emitter::emitWordAt(unsigned W, unsigned *Ptr) {
240 *Ptr = W;
241}
242
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000243uint64_t Emitter::getGlobalValueAddress(GlobalValue *V) {
244 // Try looking up the function to see if it is already compiled, if not return
245 // 0.
Chris Lattner02376e32004-11-15 23:20:04 +0000246 if (Function *F = dyn_cast<Function>(V)) {
247 void *Addr = TheJIT->getPointerToGlobalIfAvailable(F);
248 if (Addr == 0 && F->hasExternalLinkage()) {
249 // Do not output stubs for external functions.
250 Addr = TheJIT->getPointerToFunction(F);
251 }
252 return (intptr_t)Addr;
253 } else {
Chris Lattnerc07ed132003-12-20 03:36:47 +0000254 return (intptr_t)TheJIT->getOrEmitGlobalVariable(cast<GlobalVariable>(V));
255 }
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000256}
Chris Lattner45730d72004-11-19 20:56:46 +0000257uint64_t Emitter::getGlobalValueAddress(const char *Name) {
Chris Lattner4d326fa2003-12-20 01:46:27 +0000258 return (intptr_t)TheJIT->getPointerToNamedFunction(Name);
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000259}
260
261// getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry
262// in the constant pool that was last emitted with the 'emitConstantPool'
263// method.
264//
265uint64_t Emitter::getConstantPoolEntryAddress(unsigned ConstantNum) {
266 assert(ConstantNum < ConstantPoolAddresses.size() &&
267 "Invalid ConstantPoolIndex!");
268 return (intptr_t)ConstantPoolAddresses[ConstantNum];
269}
270
271// getCurrentPCValue - This returns the address that the next emitted byte
272// will be output to.
273//
274uint64_t Emitter::getCurrentPCValue() {
275 return (intptr_t)CurByte;
276}
277
Chris Lattner5be478f2004-11-20 03:46:14 +0000278uint64_t Emitter::getCurrentPCOffset() {
279 return (intptr_t)CurByte-(intptr_t)CurBlock;
280}
281
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000282uint64_t Emitter::forceCompilationOf(Function *F) {
Chris Lattner4d326fa2003-12-20 01:46:27 +0000283 return (intptr_t)TheJIT->getPointerToFunction(F);
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000284}
285
Misha Brukmand69c1e62003-07-28 19:09:06 +0000286// getPointerToNamedFunction - This function is used as a global wrapper to
Chris Lattner4d326fa2003-12-20 01:46:27 +0000287// JIT::getPointerToNamedFunction for the purpose of resolving symbols when
Misha Brukmand69c1e62003-07-28 19:09:06 +0000288// bugpoint is debugging the JIT. In that scenario, we are loading an .so and
289// need to resolve function(s) that are being mis-codegenerated, so we need to
290// resolve their addresses at runtime, and this is the way to do it.
291extern "C" {
292 void *getPointerToNamedFunction(const char *Name) {
Chris Lattner4d326fa2003-12-20 01:46:27 +0000293 Module &M = TheJIT->getModule();
Misha Brukmand69c1e62003-07-28 19:09:06 +0000294 if (Function *F = M.getNamedFunction(Name))
Chris Lattner4d326fa2003-12-20 01:46:27 +0000295 return TheJIT->getPointerToFunction(F);
296 return TheJIT->getPointerToNamedFunction(Name);
Misha Brukmand69c1e62003-07-28 19:09:06 +0000297 }
298}