blob: 59e7eba4be1bf71707fa7bf06dfbcae91c928b05 [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"
Chris Lattnerbd199fb2002-12-24 00:01:05 +00009#include "VM.h"
John Criswell7a73b802003-06-30 21:59:07 +000010#include "Config/sys/mman.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000011#include "llvm/CodeGen/MachineCodeEmitter.h"
12#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner1cc08382003-01-13 01:00:12 +000013#include "llvm/CodeGen/MachineConstantPool.h"
14#include "llvm/Target/TargetData.h"
Misha Brukmand69c1e62003-07-28 19:09:06 +000015#include "llvm/Module.h"
Chris Lattnerc648dab2003-08-01 22:13:59 +000016#include "Support/Debug.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000017#include "Support/Statistic.h"
Chris Lattner08d5e1f2003-06-08 06:43:57 +000018#include <stdio.h>
Chris Lattnerbd199fb2002-12-24 00:01:05 +000019
20namespace {
21 Statistic<> NumBytes("jello", "Number of bytes of machine code compiled");
Misha Brukmand69c1e62003-07-28 19:09:06 +000022 VM *TheVM = 0;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000023
Chris Lattner688506d2003-08-14 18:35:27 +000024 /// JITMemoryManager - Manage memory for the JIT code generation in a logical,
25 /// sane way. This splits a large block of MAP_NORESERVE'd memory into two
26 /// sections, one for function stubs, one for the functions themselves. We
27 /// have to do this because we may need to emit a function stub while in the
28 /// middle of emitting a function, and we don't know how large the function we
29 /// are emitting is. This never bothers to release the memory, because when
30 /// we are ready to destroy the JIT, the program exits.
31 class JITMemoryManager {
32 unsigned char *MemBase; // Base of block of memory, start of stub mem
33 unsigned char *FunctionBase; // Start of the function body area
34 unsigned char *CurStubPtr, *CurFunctionPtr;
35 public:
36 JITMemoryManager();
37
38 inline unsigned char *allocateStub(unsigned StubSize);
39 inline unsigned char *startFunctionBody();
40 inline void endFunctionBody(unsigned char *FunctionEnd);
41 };
42}
43
Misha Brukman88b48552003-09-10 15:09:45 +000044#ifndef _POSIX_MAPPED_FILES
Chris Lattner688506d2003-08-14 18:35:27 +000045#define _POSIX_MAPPED_FILES
Misha Brukman88b48552003-09-10 15:09:45 +000046#endif
Chris Lattner688506d2003-08-14 18:35:27 +000047#include <unistd.h>
48#include <sys/mman.h>
49
50// getMemory - Return a pointer to the specified number of bytes, which is
51// mapped as executable readable and writable.
52static void *getMemory(unsigned NumBytes) {
53 if (NumBytes == 0) return 0;
54 static const long pageSize = sysconf(_SC_PAGESIZE);
55 unsigned NumPages = (NumBytes+pageSize-1)/pageSize;
56
57#if defined(i386) || defined(__i386__) || defined(__x86__)
58 /* Linux and *BSD tend to have these flags named differently. */
59#if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
60# define MAP_ANONYMOUS MAP_ANON
61#endif /* defined(MAP_ANON) && !defined(MAP_ANONYMOUS) */
62#define fd 0
63#elif defined(sparc) || defined(__sparc__) || defined(__sparcv9)
64#define fd -1
65#else
66 std::cerr << "This architecture is not supported by the JIT!\n";
67 abort();
68#endif
69
70 unsigned mmapFlags = MAP_PRIVATE|MAP_ANONYMOUS;
71#ifdef MAP_NORESERVE
72 mmapFlags |= MAP_NORESERVE;
73#endif
74
75 void *pa = mmap(0, pageSize*NumPages, PROT_READ|PROT_WRITE|PROT_EXEC,
76 MAP_PRIVATE|MAP_ANONYMOUS, fd, 0);
77 if (pa == MAP_FAILED) {
78 perror("mmap");
79 abort();
80 }
81 return pa;
82}
83
84JITMemoryManager::JITMemoryManager() {
85 // Allocate a 16M block of memory...
86 MemBase = (unsigned char*)getMemory(16 << 20);
87 FunctionBase = MemBase + 512*1024; // Use 512k for stubs
88
89 // Allocate stubs backwards from the function base, allocate functions forward
90 // from the function base.
91 CurStubPtr = CurFunctionPtr = FunctionBase;
92}
93
94unsigned char *JITMemoryManager::allocateStub(unsigned StubSize) {
95 CurStubPtr -= StubSize;
96 if (CurStubPtr < MemBase) {
97 std::cerr << "JIT ran out of memory for function stubs!\n";
98 abort();
99 }
100 return CurStubPtr;
101}
102
103unsigned char *JITMemoryManager::startFunctionBody() {
104 // Round up to an even multiple of 4 bytes, this should eventually be target
105 // specific.
106 return (unsigned char*)(((intptr_t)CurFunctionPtr + 3) & ~3);
107}
108
109void JITMemoryManager::endFunctionBody(unsigned char *FunctionEnd) {
110 assert(FunctionEnd > CurFunctionPtr);
111 CurFunctionPtr = FunctionEnd;
112}
113
114
115
116namespace {
117 /// Emitter - The JIT implementation of the MachineCodeEmiter, which is used
118 /// to output functions to memory for execution.
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000119 class Emitter : public MachineCodeEmitter {
Chris Lattner688506d2003-08-14 18:35:27 +0000120 JITMemoryManager MemMgr;
121
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000122 // CurBlock - The start of the current block of memory. CurByte - The
123 // current byte being emitted to.
Chris Lattner6125fdd2003-05-09 03:30:07 +0000124 unsigned char *CurBlock, *CurByte;
125
126 // When outputting a function stub in the context of some other function, we
127 // save CurBlock and CurByte here.
128 unsigned char *SavedCurBlock, *SavedCurByte;
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000129
130 // ConstantPoolAddresses - Contains the location for each entry in the
131 // constant pool.
Chris Lattner1cc08382003-01-13 01:00:12 +0000132 std::vector<void*> ConstantPoolAddresses;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000133 public:
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000134 Emitter(VM &vm) { TheVM = &vm; }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000135
136 virtual void startFunction(MachineFunction &F);
137 virtual void finishFunction(MachineFunction &F);
Chris Lattner1cc08382003-01-13 01:00:12 +0000138 virtual void emitConstantPool(MachineConstantPool *MCP);
Chris Lattner6125fdd2003-05-09 03:30:07 +0000139 virtual void startFunctionStub(const Function &F, unsigned StubSize);
140 virtual void* finishFunctionStub(const Function &F);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000141 virtual void emitByte(unsigned char B);
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000142 virtual void emitWord(unsigned W);
143
144 virtual uint64_t getGlobalValueAddress(GlobalValue *V);
145 virtual uint64_t getGlobalValueAddress(const std::string &Name);
146 virtual uint64_t getConstantPoolEntryAddress(unsigned Entry);
147 virtual uint64_t getCurrentPCValue();
148
149 // forceCompilationOf - Force the compilation of the specified function, and
150 // return its address, because we REALLY need the address now.
151 //
152 // FIXME: This is JIT specific!
153 //
154 virtual uint64_t forceCompilationOf(Function *F);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000155 };
156}
157
Misha Brukman906f5fa2003-06-02 03:23:16 +0000158MachineCodeEmitter *VM::createEmitter(VM &V) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000159 return new Emitter(V);
160}
161
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000162void Emitter::startFunction(MachineFunction &F) {
Chris Lattner688506d2003-08-14 18:35:27 +0000163 CurByte = CurBlock = MemMgr.startFunctionBody();
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000164 TheVM->addGlobalMapping(F.getFunction(), CurBlock);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000165}
166
167void Emitter::finishFunction(MachineFunction &F) {
Chris Lattner688506d2003-08-14 18:35:27 +0000168 MemMgr.endFunctionBody(CurByte);
Chris Lattner1cc08382003-01-13 01:00:12 +0000169 ConstantPoolAddresses.clear();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000170 NumBytes += CurByte-CurBlock;
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 Lattnerbd199fb2002-12-24 00:01:05 +0000174 << ": " << CurByte-CurBlock << " bytes of text\n");
175}
176
Chris Lattner1cc08382003-01-13 01:00:12 +0000177void Emitter::emitConstantPool(MachineConstantPool *MCP) {
178 const std::vector<Constant*> &Constants = MCP->getConstants();
179 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
180 // For now we just allocate some memory on the heap, this can be
181 // dramatically improved.
182 const Type *Ty = ((Value*)Constants[i])->getType();
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000183 void *Addr = malloc(TheVM->getTargetData().getTypeSize(Ty));
184 TheVM->InitializeMemory(Constants[i], Addr);
Chris Lattner1cc08382003-01-13 01:00:12 +0000185 ConstantPoolAddresses.push_back(Addr);
186 }
187}
188
Chris Lattner6125fdd2003-05-09 03:30:07 +0000189void Emitter::startFunctionStub(const Function &F, unsigned StubSize) {
190 SavedCurBlock = CurBlock; SavedCurByte = CurByte;
Chris Lattner688506d2003-08-14 18:35:27 +0000191 CurByte = CurBlock = MemMgr.allocateStub(StubSize);
Chris Lattner6125fdd2003-05-09 03:30:07 +0000192}
193
194void *Emitter::finishFunctionStub(const Function &F) {
195 NumBytes += CurByte-CurBlock;
196 DEBUG(std::cerr << "Finished CodeGen of [0x" << std::hex
197 << (unsigned)(intptr_t)CurBlock
198 << std::dec << "] Function stub for: " << F.getName()
199 << ": " << CurByte-CurBlock << " bytes of text\n");
200 std::swap(CurBlock, SavedCurBlock);
201 CurByte = SavedCurByte;
202 return SavedCurBlock;
203}
204
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000205void Emitter::emitByte(unsigned char B) {
206 *CurByte++ = B; // Write the byte to memory
207}
208
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000209void Emitter::emitWord(unsigned W) {
Chris Lattner688506d2003-08-14 18:35:27 +0000210 // This won't work if the endianness of the host and target don't agree! (For
211 // a JIT this can't happen though. :)
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000212 *(unsigned*)CurByte = W;
213 CurByte += sizeof(unsigned);
214}
215
216
217uint64_t Emitter::getGlobalValueAddress(GlobalValue *V) {
218 // Try looking up the function to see if it is already compiled, if not return
219 // 0.
220 return (intptr_t)TheVM->getPointerToGlobalIfAvailable(V);
221}
222uint64_t Emitter::getGlobalValueAddress(const std::string &Name) {
223 return (intptr_t)TheVM->getPointerToNamedFunction(Name);
224}
225
226// getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry
227// in the constant pool that was last emitted with the 'emitConstantPool'
228// method.
229//
230uint64_t Emitter::getConstantPoolEntryAddress(unsigned ConstantNum) {
231 assert(ConstantNum < ConstantPoolAddresses.size() &&
232 "Invalid ConstantPoolIndex!");
233 return (intptr_t)ConstantPoolAddresses[ConstantNum];
234}
235
236// getCurrentPCValue - This returns the address that the next emitted byte
237// will be output to.
238//
239uint64_t Emitter::getCurrentPCValue() {
240 return (intptr_t)CurByte;
241}
242
243uint64_t Emitter::forceCompilationOf(Function *F) {
244 return (intptr_t)TheVM->getPointerToFunction(F);
245}
246
Misha Brukmand69c1e62003-07-28 19:09:06 +0000247// getPointerToNamedFunction - This function is used as a global wrapper to
248// VM::getPointerToNamedFunction for the purpose of resolving symbols when
249// bugpoint is debugging the JIT. In that scenario, we are loading an .so and
250// need to resolve function(s) that are being mis-codegenerated, so we need to
251// resolve their addresses at runtime, and this is the way to do it.
252extern "C" {
253 void *getPointerToNamedFunction(const char *Name) {
254 Module &M = TheVM->getModule();
255 if (Function *F = M.getNamedFunction(Name))
256 return TheVM->getPointerToFunction(F);
257 return TheVM->getPointerToNamedFunction(Name);
258 }
259}