Removed debug print statement.
[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 #include "VM.h"
9 #include "llvm/CodeGen/MachineCodeEmitter.h"
10 #include "llvm/CodeGen/MachineFunction.h"
11 #include "llvm/CodeGen/MachineConstantPool.h"
12 #include "llvm/Target/TargetData.h"
13 #include "llvm/Function.h"
14 #include "Support/Statistic.h"
15
16 static VM *TheVM = 0;
17
18 namespace {
19   Statistic<> NumBytes("jello", "Number of bytes of machine code compiled");
20
21   class Emitter : public MachineCodeEmitter {
22     // CurBlock - The start of the current block of memory.  CurByte - The
23     // current byte being emitted to.
24     unsigned char *CurBlock, *CurByte;
25
26     // When outputting a function stub in the context of some other function, we
27     // save CurBlock and CurByte here.
28     unsigned char *SavedCurBlock, *SavedCurByte;
29
30     // ConstantPoolAddresses - Contains the location for each entry in the
31     // constant pool.
32     std::vector<void*> ConstantPoolAddresses;
33   public:
34     Emitter(VM &vm) { TheVM = &vm; }
35
36     virtual void startFunction(MachineFunction &F);
37     virtual void finishFunction(MachineFunction &F);
38     virtual void emitConstantPool(MachineConstantPool *MCP);
39     virtual void startFunctionStub(const Function &F, unsigned StubSize);
40     virtual void* finishFunctionStub(const Function &F);
41     virtual void emitByte(unsigned char B);
42     virtual void emitWord(unsigned W);
43
44     virtual uint64_t getGlobalValueAddress(GlobalValue *V);
45     virtual uint64_t getGlobalValueAddress(const std::string &Name);
46     virtual uint64_t getConstantPoolEntryAddress(unsigned Entry);
47     virtual uint64_t getCurrentPCValue();
48
49     // forceCompilationOf - Force the compilation of the specified function, and
50     // return its address, because we REALLY need the address now.
51     //
52     // FIXME: This is JIT specific!
53     //
54     virtual uint64_t forceCompilationOf(Function *F);
55   };
56 }
57
58 MachineCodeEmitter *VM::createEmitter(VM &V) {
59   return new Emitter(V);
60 }
61
62
63 #define _POSIX_MAPPED_FILES
64 #include <unistd.h>
65 #include <sys/mman.h>
66
67 // FIXME: This should be rewritten to support a real memory manager for
68 // executable memory pages!
69 static void *getMemory(unsigned NumPages) {
70   void *pa;
71   if (NumPages == 0) return 0;
72   static const long pageSize = sysconf(_SC_PAGESIZE);
73
74 #if defined(i386) || defined(__i386__) || defined(__x86__)
75   pa = mmap(0, pageSize*NumPages, PROT_READ|PROT_WRITE|PROT_EXEC,
76             MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);  /* fd = 0  */
77 #elif defined(sparc) || defined(__sparc__) || defined(__sparcv9)
78   static unsigned long Counter = 0;
79   pa = mmap((void*)(0x140000000UL+Counter), pageSize*NumPages,
80             PROT_READ|PROT_WRITE|PROT_EXEC,
81             MAP_PRIVATE|MAP_ANON|MAP_FIXED, -1, 0); /* fd = -1 */
82   Counter += pageSize*NumPages;
83 #else
84   std::cerr << "This architecture is not supported by the JIT\n";
85   abort();
86 #endif
87
88   if (pa == MAP_FAILED) {
89     perror("mmap");
90     abort();
91   }
92   return pa;
93 }
94
95
96 void Emitter::startFunction(MachineFunction &F) {
97   CurBlock = (unsigned char *)getMemory(16);
98   CurByte = CurBlock;  // Start writing at the beginning of the fn.
99   TheVM->addGlobalMapping(F.getFunction(), CurBlock);
100 }
101
102 void Emitter::finishFunction(MachineFunction &F) {
103   ConstantPoolAddresses.clear();
104   NumBytes += CurByte-CurBlock;
105
106   DEBUG(std::cerr << "Finished CodeGen of [0x" << std::hex
107                   << (unsigned)(intptr_t)CurBlock
108                   << std::dec << "] Function: " << F.getFunction()->getName()
109                   << ": " << CurByte-CurBlock << " bytes of text\n");
110 }
111
112 void Emitter::emitConstantPool(MachineConstantPool *MCP) {
113   const std::vector<Constant*> &Constants = MCP->getConstants();
114   for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
115     // For now we just allocate some memory on the heap, this can be
116     // dramatically improved.
117     const Type *Ty = ((Value*)Constants[i])->getType();
118     void *Addr = malloc(TheVM->getTargetData().getTypeSize(Ty));
119     TheVM->InitializeMemory(Constants[i], Addr);
120     ConstantPoolAddresses.push_back(Addr);
121   }
122 }
123
124 void Emitter::startFunctionStub(const Function &F, unsigned StubSize) {
125   static const long pageSize = sysconf(_SC_PAGESIZE);
126   SavedCurBlock = CurBlock;  SavedCurByte = CurByte;
127   // FIXME: this is a huge waste of memory.
128   CurBlock = (unsigned char *)getMemory((StubSize+pageSize-1)/pageSize);
129   CurByte = CurBlock;  // Start writing at the beginning of the fn.
130 }
131
132 void *Emitter::finishFunctionStub(const Function &F) {
133   NumBytes += CurByte-CurBlock;
134   DEBUG(std::cerr << "Finished CodeGen of [0x" << std::hex
135                   << (unsigned)(intptr_t)CurBlock
136                   << std::dec << "] Function stub for: " << F.getName()
137                   << ": " << CurByte-CurBlock << " bytes of text\n");
138   std::swap(CurBlock, SavedCurBlock);
139   CurByte = SavedCurByte;
140   return SavedCurBlock;
141 }
142
143 void Emitter::emitByte(unsigned char B) {
144   *CurByte++ = B;   // Write the byte to memory
145 }
146
147 void Emitter::emitWord(unsigned W) {
148   // FIXME: This won't work if the endianness of the host and target don't
149   // agree!  (For a JIT this can't happen though.  :)
150   *(unsigned*)CurByte = W;
151   CurByte += sizeof(unsigned);
152 }
153
154
155 uint64_t Emitter::getGlobalValueAddress(GlobalValue *V) {
156   // Try looking up the function to see if it is already compiled, if not return
157   // 0.
158   return (intptr_t)TheVM->getPointerToGlobalIfAvailable(V);
159 }
160 uint64_t Emitter::getGlobalValueAddress(const std::string &Name) {
161   return (intptr_t)TheVM->getPointerToNamedFunction(Name);
162 }
163
164 // getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry
165 // in the constant pool that was last emitted with the 'emitConstantPool'
166 // method.
167 //
168 uint64_t Emitter::getConstantPoolEntryAddress(unsigned ConstantNum) {
169   assert(ConstantNum < ConstantPoolAddresses.size() &&
170          "Invalid ConstantPoolIndex!");
171   return (intptr_t)ConstantPoolAddresses[ConstantNum];
172 }
173
174 // getCurrentPCValue - This returns the address that the next emitted byte
175 // will be output to.
176 //
177 uint64_t Emitter::getCurrentPCValue() {
178   return (intptr_t)CurByte;
179 }
180
181 uint64_t Emitter::forceCompilationOf(Function *F) {
182   return (intptr_t)TheVM->getPointerToFunction(F);
183 }
184