blob: d25aaa3e4a3d0e52444178cb2d3f8e5022134b8b [file] [log] [blame]
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001//===-- Emitter.cpp - Write machine code to executable memory -------------===//
2//
3// This file defines a MachineCodeEmitter object that is used by Jello to write
4// machine code to memory and remember where relocatable values lie.
5//
6//===----------------------------------------------------------------------===//
7
Chris Lattner3785fad2003-08-05 17:00:32 +00008#define DEBUG_TYPE "jit"
Misha Brukman36d10ef2003-09-10 20:52:05 +00009#ifndef _POSIX_MAPPED_FILES
10#define _POSIX_MAPPED_FILES
11#endif
Chris Lattnerbd199fb2002-12-24 00:01:05 +000012#include "VM.h"
13#include "llvm/CodeGen/MachineCodeEmitter.h"
14#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner1cc08382003-01-13 01:00:12 +000015#include "llvm/CodeGen/MachineConstantPool.h"
16#include "llvm/Target/TargetData.h"
Misha Brukmand69c1e62003-07-28 19:09:06 +000017#include "llvm/Module.h"
Chris Lattnerc648dab2003-08-01 22:13:59 +000018#include "Support/Debug.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000019#include "Support/Statistic.h"
Misha Brukman36d10ef2003-09-10 20:52:05 +000020#include "Config/unistd.h"
21#include "Config/sys/mman.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000022
23namespace {
24 Statistic<> NumBytes("jello", "Number of bytes of machine code compiled");
Misha Brukmand69c1e62003-07-28 19:09:06 +000025 VM *TheVM = 0;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000026
Chris Lattner688506d2003-08-14 18:35:27 +000027 /// JITMemoryManager - Manage memory for the JIT code generation in a logical,
28 /// sane way. This splits a large block of MAP_NORESERVE'd memory into two
29 /// sections, one for function stubs, one for the functions themselves. We
30 /// have to do this because we may need to emit a function stub while in the
31 /// middle of emitting a function, and we don't know how large the function we
32 /// are emitting is. This never bothers to release the memory, because when
33 /// we are ready to destroy the JIT, the program exits.
34 class JITMemoryManager {
35 unsigned char *MemBase; // Base of block of memory, start of stub mem
36 unsigned char *FunctionBase; // Start of the function body area
37 unsigned char *CurStubPtr, *CurFunctionPtr;
38 public:
39 JITMemoryManager();
40
41 inline unsigned char *allocateStub(unsigned StubSize);
42 inline unsigned char *startFunctionBody();
43 inline void endFunctionBody(unsigned char *FunctionEnd);
44 };
45}
46
Chris Lattner688506d2003-08-14 18:35:27 +000047// getMemory - Return a pointer to the specified number of bytes, which is
48// mapped as executable readable and writable.
49static void *getMemory(unsigned NumBytes) {
50 if (NumBytes == 0) return 0;
51 static const long pageSize = sysconf(_SC_PAGESIZE);
52 unsigned NumPages = (NumBytes+pageSize-1)/pageSize;
53
54#if defined(i386) || defined(__i386__) || defined(__x86__)
55 /* Linux and *BSD tend to have these flags named differently. */
56#if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
57# define MAP_ANONYMOUS MAP_ANON
58#endif /* defined(MAP_ANON) && !defined(MAP_ANONYMOUS) */
Chris Lattner688506d2003-08-14 18:35:27 +000059#elif defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Brian Gaeke364f86d2003-10-11 03:51:18 +000060/* nothing */
Chris Lattner688506d2003-08-14 18:35:27 +000061#else
62 std::cerr << "This architecture is not supported by the JIT!\n";
63 abort();
64#endif
Brian Gaeke364f86d2003-10-11 03:51:18 +000065
66#if defined(__linux__)
67#define fd 0
68#else
69#define fd -1
70#endif
Chris Lattner688506d2003-08-14 18:35:27 +000071
72 unsigned mmapFlags = MAP_PRIVATE|MAP_ANONYMOUS;
73#ifdef MAP_NORESERVE
74 mmapFlags |= MAP_NORESERVE;
75#endif
76
77 void *pa = mmap(0, pageSize*NumPages, PROT_READ|PROT_WRITE|PROT_EXEC,
Chris Lattner011efae2003-10-06 19:07:41 +000078 mmapFlags, fd, 0);
Chris Lattner688506d2003-08-14 18:35:27 +000079 if (pa == MAP_FAILED) {
80 perror("mmap");
81 abort();
82 }
83 return pa;
84}
85
86JITMemoryManager::JITMemoryManager() {
87 // Allocate a 16M block of memory...
88 MemBase = (unsigned char*)getMemory(16 << 20);
89 FunctionBase = MemBase + 512*1024; // Use 512k for stubs
90
91 // Allocate stubs backwards from the function base, allocate functions forward
92 // from the function base.
93 CurStubPtr = CurFunctionPtr = FunctionBase;
94}
95
96unsigned char *JITMemoryManager::allocateStub(unsigned StubSize) {
97 CurStubPtr -= StubSize;
98 if (CurStubPtr < MemBase) {
99 std::cerr << "JIT ran out of memory for function stubs!\n";
100 abort();
101 }
102 return CurStubPtr;
103}
104
105unsigned char *JITMemoryManager::startFunctionBody() {
106 // Round up to an even multiple of 4 bytes, this should eventually be target
107 // specific.
108 return (unsigned char*)(((intptr_t)CurFunctionPtr + 3) & ~3);
109}
110
111void JITMemoryManager::endFunctionBody(unsigned char *FunctionEnd) {
112 assert(FunctionEnd > CurFunctionPtr);
113 CurFunctionPtr = FunctionEnd;
114}
115
116
117
118namespace {
119 /// Emitter - The JIT implementation of the MachineCodeEmiter, which is used
120 /// to output functions to memory for execution.
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000121 class Emitter : public MachineCodeEmitter {
Chris Lattner688506d2003-08-14 18:35:27 +0000122 JITMemoryManager MemMgr;
123
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000124 // CurBlock - The start of the current block of memory. CurByte - The
125 // current byte being emitted to.
Chris Lattner6125fdd2003-05-09 03:30:07 +0000126 unsigned char *CurBlock, *CurByte;
127
128 // When outputting a function stub in the context of some other function, we
129 // save CurBlock and CurByte here.
130 unsigned char *SavedCurBlock, *SavedCurByte;
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000131
132 // ConstantPoolAddresses - Contains the location for each entry in the
133 // constant pool.
Chris Lattner1cc08382003-01-13 01:00:12 +0000134 std::vector<void*> ConstantPoolAddresses;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000135 public:
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000136 Emitter(VM &vm) { TheVM = &vm; }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000137
138 virtual void startFunction(MachineFunction &F);
139 virtual void finishFunction(MachineFunction &F);
Chris Lattner1cc08382003-01-13 01:00:12 +0000140 virtual void emitConstantPool(MachineConstantPool *MCP);
Chris Lattner6125fdd2003-05-09 03:30:07 +0000141 virtual void startFunctionStub(const Function &F, unsigned StubSize);
142 virtual void* finishFunctionStub(const Function &F);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000143 virtual void emitByte(unsigned char B);
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000144 virtual void emitWord(unsigned W);
145
146 virtual uint64_t getGlobalValueAddress(GlobalValue *V);
147 virtual uint64_t getGlobalValueAddress(const std::string &Name);
148 virtual uint64_t getConstantPoolEntryAddress(unsigned Entry);
149 virtual uint64_t getCurrentPCValue();
150
151 // forceCompilationOf - Force the compilation of the specified function, and
152 // return its address, because we REALLY need the address now.
153 //
154 // FIXME: This is JIT specific!
155 //
156 virtual uint64_t forceCompilationOf(Function *F);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000157 };
158}
159
Misha Brukman906f5fa2003-06-02 03:23:16 +0000160MachineCodeEmitter *VM::createEmitter(VM &V) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000161 return new Emitter(V);
162}
163
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000164void Emitter::startFunction(MachineFunction &F) {
Chris Lattner688506d2003-08-14 18:35:27 +0000165 CurByte = CurBlock = MemMgr.startFunctionBody();
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000166 TheVM->addGlobalMapping(F.getFunction(), CurBlock);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000167}
168
169void Emitter::finishFunction(MachineFunction &F) {
Chris Lattner688506d2003-08-14 18:35:27 +0000170 MemMgr.endFunctionBody(CurByte);
Chris Lattner1cc08382003-01-13 01:00:12 +0000171 ConstantPoolAddresses.clear();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000172 NumBytes += CurByte-CurBlock;
173
Brian Gaeke02c26b62003-06-30 18:06:20 +0000174 DEBUG(std::cerr << "Finished CodeGen of [" << (void*)CurBlock
Misha Brukman1d440852003-06-06 06:52:35 +0000175 << "] Function: " << F.getFunction()->getName()
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000176 << ": " << CurByte-CurBlock << " bytes of text\n");
177}
178
Chris Lattner1cc08382003-01-13 01:00:12 +0000179void Emitter::emitConstantPool(MachineConstantPool *MCP) {
180 const std::vector<Constant*> &Constants = MCP->getConstants();
181 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
182 // For now we just allocate some memory on the heap, this can be
183 // dramatically improved.
184 const Type *Ty = ((Value*)Constants[i])->getType();
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000185 void *Addr = malloc(TheVM->getTargetData().getTypeSize(Ty));
186 TheVM->InitializeMemory(Constants[i], Addr);
Chris Lattner1cc08382003-01-13 01:00:12 +0000187 ConstantPoolAddresses.push_back(Addr);
188 }
189}
190
Chris Lattner6125fdd2003-05-09 03:30:07 +0000191void Emitter::startFunctionStub(const Function &F, unsigned StubSize) {
192 SavedCurBlock = CurBlock; SavedCurByte = CurByte;
Chris Lattner688506d2003-08-14 18:35:27 +0000193 CurByte = CurBlock = MemMgr.allocateStub(StubSize);
Chris Lattner6125fdd2003-05-09 03:30:07 +0000194}
195
196void *Emitter::finishFunctionStub(const Function &F) {
197 NumBytes += CurByte-CurBlock;
198 DEBUG(std::cerr << "Finished CodeGen of [0x" << std::hex
199 << (unsigned)(intptr_t)CurBlock
200 << std::dec << "] Function stub for: " << F.getName()
201 << ": " << CurByte-CurBlock << " bytes of text\n");
202 std::swap(CurBlock, SavedCurBlock);
203 CurByte = SavedCurByte;
204 return SavedCurBlock;
205}
206
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000207void Emitter::emitByte(unsigned char B) {
208 *CurByte++ = B; // Write the byte to memory
209}
210
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000211void Emitter::emitWord(unsigned W) {
Chris Lattner688506d2003-08-14 18:35:27 +0000212 // This won't work if the endianness of the host and target don't agree! (For
213 // a JIT this can't happen though. :)
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000214 *(unsigned*)CurByte = W;
215 CurByte += sizeof(unsigned);
216}
217
218
219uint64_t Emitter::getGlobalValueAddress(GlobalValue *V) {
220 // Try looking up the function to see if it is already compiled, if not return
221 // 0.
222 return (intptr_t)TheVM->getPointerToGlobalIfAvailable(V);
223}
224uint64_t Emitter::getGlobalValueAddress(const std::string &Name) {
225 return (intptr_t)TheVM->getPointerToNamedFunction(Name);
226}
227
228// getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry
229// in the constant pool that was last emitted with the 'emitConstantPool'
230// method.
231//
232uint64_t Emitter::getConstantPoolEntryAddress(unsigned ConstantNum) {
233 assert(ConstantNum < ConstantPoolAddresses.size() &&
234 "Invalid ConstantPoolIndex!");
235 return (intptr_t)ConstantPoolAddresses[ConstantNum];
236}
237
238// getCurrentPCValue - This returns the address that the next emitted byte
239// will be output to.
240//
241uint64_t Emitter::getCurrentPCValue() {
242 return (intptr_t)CurByte;
243}
244
245uint64_t Emitter::forceCompilationOf(Function *F) {
246 return (intptr_t)TheVM->getPointerToFunction(F);
247}
248
Misha Brukmand69c1e62003-07-28 19:09:06 +0000249// getPointerToNamedFunction - This function is used as a global wrapper to
250// VM::getPointerToNamedFunction for the purpose of resolving symbols when
251// bugpoint is debugging the JIT. In that scenario, we are loading an .so and
252// need to resolve function(s) that are being mis-codegenerated, so we need to
253// resolve their addresses at runtime, and this is the way to do it.
254extern "C" {
255 void *getPointerToNamedFunction(const char *Name) {
256 Module &M = TheVM->getModule();
257 if (Function *F = M.getNamedFunction(Name))
258 return TheVM->getPointerToFunction(F);
259 return TheVM->getPointerToNamedFunction(Name);
260 }
261}