Don't include Config/stdio.h or <stdio.h>.
[oota-llvm.git] / lib / ExecutionEngine / JIT / JITEmitter.cpp
1 //===-- 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
8 #define DEBUG_TYPE "jit"
9 #ifndef _POSIX_MAPPED_FILES
10 #define _POSIX_MAPPED_FILES
11 #endif
12 #include "VM.h"
13 #include "llvm/CodeGen/MachineCodeEmitter.h"
14 #include "llvm/CodeGen/MachineFunction.h"
15 #include "llvm/CodeGen/MachineConstantPool.h"
16 #include "llvm/Target/TargetData.h"
17 #include "llvm/Module.h"
18 #include "Support/Debug.h"
19 #include "Support/Statistic.h"
20 #include "Config/unistd.h"
21 #include "Config/sys/mman.h"
22
23 namespace {
24   Statistic<> NumBytes("jello", "Number of bytes of machine code compiled");
25   VM *TheVM = 0;
26
27   /// 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
47 // getMemory - Return a pointer to the specified number of bytes, which is
48 // mapped as executable readable and writable.
49 static 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) */
59 #define fd  0
60 #elif defined(sparc) || defined(__sparc__) || defined(__sparcv9)
61 #define fd -1
62 #else
63   std::cerr << "This architecture is not supported by the JIT!\n";
64   abort();
65 #endif
66   
67   unsigned mmapFlags = MAP_PRIVATE|MAP_ANONYMOUS;
68 #ifdef MAP_NORESERVE
69   mmapFlags |= MAP_NORESERVE;
70 #endif
71
72   void *pa = mmap(0, pageSize*NumPages, PROT_READ|PROT_WRITE|PROT_EXEC,
73                   mmapFlags, fd, 0);
74   if (pa == MAP_FAILED) {
75     perror("mmap");
76     abort();
77   }
78   return pa;
79 }
80
81 JITMemoryManager::JITMemoryManager() {
82   // Allocate a 16M block of memory...
83   MemBase = (unsigned char*)getMemory(16 << 20);
84   FunctionBase = MemBase + 512*1024; // Use 512k for stubs
85
86   // Allocate stubs backwards from the function base, allocate functions forward
87   // from the function base.
88   CurStubPtr = CurFunctionPtr = FunctionBase;
89 }
90
91 unsigned char *JITMemoryManager::allocateStub(unsigned StubSize) {
92   CurStubPtr -= StubSize;
93   if (CurStubPtr < MemBase) {
94     std::cerr << "JIT ran out of memory for function stubs!\n";
95     abort();
96   }
97   return CurStubPtr;
98 }
99
100 unsigned char *JITMemoryManager::startFunctionBody() {
101   // Round up to an even multiple of 4 bytes, this should eventually be target
102   // specific.
103   return (unsigned char*)(((intptr_t)CurFunctionPtr + 3) & ~3);
104 }
105
106 void JITMemoryManager::endFunctionBody(unsigned char *FunctionEnd) {
107   assert(FunctionEnd > CurFunctionPtr);
108   CurFunctionPtr = FunctionEnd;
109 }
110
111
112
113 namespace {
114   /// Emitter - The JIT implementation of the MachineCodeEmiter, which is used
115   /// to output functions to memory for execution.
116   class Emitter : public MachineCodeEmitter {
117     JITMemoryManager MemMgr;
118
119     // CurBlock - The start of the current block of memory.  CurByte - The
120     // current byte being emitted to.
121     unsigned char *CurBlock, *CurByte;
122
123     // When outputting a function stub in the context of some other function, we
124     // save CurBlock and CurByte here.
125     unsigned char *SavedCurBlock, *SavedCurByte;
126
127     // ConstantPoolAddresses - Contains the location for each entry in the
128     // constant pool.
129     std::vector<void*> ConstantPoolAddresses;
130   public:
131     Emitter(VM &vm) { TheVM = &vm; }
132
133     virtual void startFunction(MachineFunction &F);
134     virtual void finishFunction(MachineFunction &F);
135     virtual void emitConstantPool(MachineConstantPool *MCP);
136     virtual void startFunctionStub(const Function &F, unsigned StubSize);
137     virtual void* finishFunctionStub(const Function &F);
138     virtual void emitByte(unsigned char B);
139     virtual void emitWord(unsigned W);
140
141     virtual uint64_t getGlobalValueAddress(GlobalValue *V);
142     virtual uint64_t getGlobalValueAddress(const std::string &Name);
143     virtual uint64_t getConstantPoolEntryAddress(unsigned Entry);
144     virtual uint64_t getCurrentPCValue();
145
146     // forceCompilationOf - Force the compilation of the specified function, and
147     // return its address, because we REALLY need the address now.
148     //
149     // FIXME: This is JIT specific!
150     //
151     virtual uint64_t forceCompilationOf(Function *F);
152   };
153 }
154
155 MachineCodeEmitter *VM::createEmitter(VM &V) {
156   return new Emitter(V);
157 }
158
159 void Emitter::startFunction(MachineFunction &F) {
160   CurByte = CurBlock = MemMgr.startFunctionBody();
161   TheVM->addGlobalMapping(F.getFunction(), CurBlock);
162 }
163
164 void Emitter::finishFunction(MachineFunction &F) {
165   MemMgr.endFunctionBody(CurByte);
166   ConstantPoolAddresses.clear();
167   NumBytes += CurByte-CurBlock;
168
169   DEBUG(std::cerr << "Finished CodeGen of [" << (void*)CurBlock
170                   << "] Function: " << F.getFunction()->getName()
171                   << ": " << CurByte-CurBlock << " bytes of text\n");
172 }
173
174 void Emitter::emitConstantPool(MachineConstantPool *MCP) {
175   const std::vector<Constant*> &Constants = MCP->getConstants();
176   for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
177     // For now we just allocate some memory on the heap, this can be
178     // dramatically improved.
179     const Type *Ty = ((Value*)Constants[i])->getType();
180     void *Addr = malloc(TheVM->getTargetData().getTypeSize(Ty));
181     TheVM->InitializeMemory(Constants[i], Addr);
182     ConstantPoolAddresses.push_back(Addr);
183   }
184 }
185
186 void Emitter::startFunctionStub(const Function &F, unsigned StubSize) {
187   SavedCurBlock = CurBlock;  SavedCurByte = CurByte;
188   CurByte = CurBlock = MemMgr.allocateStub(StubSize);
189 }
190
191 void *Emitter::finishFunctionStub(const Function &F) {
192   NumBytes += CurByte-CurBlock;
193   DEBUG(std::cerr << "Finished CodeGen of [0x" << std::hex
194                   << (unsigned)(intptr_t)CurBlock
195                   << std::dec << "] Function stub for: " << F.getName()
196                   << ": " << CurByte-CurBlock << " bytes of text\n");
197   std::swap(CurBlock, SavedCurBlock);
198   CurByte = SavedCurByte;
199   return SavedCurBlock;
200 }
201
202 void Emitter::emitByte(unsigned char B) {
203   *CurByte++ = B;   // Write the byte to memory
204 }
205
206 void Emitter::emitWord(unsigned W) {
207   // This won't work if the endianness of the host and target don't agree!  (For
208   // a JIT this can't happen though.  :)
209   *(unsigned*)CurByte = W;
210   CurByte += sizeof(unsigned);
211 }
212
213
214 uint64_t Emitter::getGlobalValueAddress(GlobalValue *V) {
215   // Try looking up the function to see if it is already compiled, if not return
216   // 0.
217   return (intptr_t)TheVM->getPointerToGlobalIfAvailable(V);
218 }
219 uint64_t Emitter::getGlobalValueAddress(const std::string &Name) {
220   return (intptr_t)TheVM->getPointerToNamedFunction(Name);
221 }
222
223 // getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry
224 // in the constant pool that was last emitted with the 'emitConstantPool'
225 // method.
226 //
227 uint64_t Emitter::getConstantPoolEntryAddress(unsigned ConstantNum) {
228   assert(ConstantNum < ConstantPoolAddresses.size() &&
229          "Invalid ConstantPoolIndex!");
230   return (intptr_t)ConstantPoolAddresses[ConstantNum];
231 }
232
233 // getCurrentPCValue - This returns the address that the next emitted byte
234 // will be output to.
235 //
236 uint64_t Emitter::getCurrentPCValue() {
237   return (intptr_t)CurByte;
238 }
239
240 uint64_t Emitter::forceCompilationOf(Function *F) {
241   return (intptr_t)TheVM->getPointerToFunction(F);
242 }
243
244 // getPointerToNamedFunction - This function is used as a global wrapper to
245 // VM::getPointerToNamedFunction for the purpose of resolving symbols when
246 // bugpoint is debugging the JIT. In that scenario, we are loading an .so and
247 // need to resolve function(s) that are being mis-codegenerated, so we need to
248 // resolve their addresses at runtime, and this is the way to do it.
249 extern "C" {
250   void *getPointerToNamedFunction(const char *Name) {
251     Module &M = TheVM->getModule();
252     if (Function *F = M.getNamedFunction(Name))
253       return TheVM->getPointerToFunction(F);
254     return TheVM->getPointerToNamedFunction(Name);
255   }
256 }