blob: 242576604a9e7fa13f44faeb7e45e945701d1b82 [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
8#include "VM.h"
9#include "llvm/CodeGen/MachineCodeEmitter.h"
10#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner1cc08382003-01-13 01:00:12 +000011#include "llvm/CodeGen/MachineConstantPool.h"
12#include "llvm/Target/TargetData.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000013#include "llvm/Function.h"
14#include "Support/Statistic.h"
15
Chris Lattnerbba1b6d2003-06-01 23:24:36 +000016static VM *TheVM = 0;
17
Chris Lattnerbd199fb2002-12-24 00:01:05 +000018namespace {
19 Statistic<> NumBytes("jello", "Number of bytes of machine code compiled");
20
21 class Emitter : public MachineCodeEmitter {
Chris Lattnerbba1b6d2003-06-01 23:24:36 +000022 // CurBlock - The start of the current block of memory. CurByte - The
23 // current byte being emitted to.
Chris Lattner6125fdd2003-05-09 03:30:07 +000024 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;
Chris Lattnerbba1b6d2003-06-01 23:24:36 +000029
30 // ConstantPoolAddresses - Contains the location for each entry in the
31 // constant pool.
Chris Lattner1cc08382003-01-13 01:00:12 +000032 std::vector<void*> ConstantPoolAddresses;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000033 public:
Chris Lattnerbba1b6d2003-06-01 23:24:36 +000034 Emitter(VM &vm) { TheVM = &vm; }
Chris Lattnerbd199fb2002-12-24 00:01:05 +000035
36 virtual void startFunction(MachineFunction &F);
37 virtual void finishFunction(MachineFunction &F);
Chris Lattner1cc08382003-01-13 01:00:12 +000038 virtual void emitConstantPool(MachineConstantPool *MCP);
Chris Lattner6125fdd2003-05-09 03:30:07 +000039 virtual void startFunctionStub(const Function &F, unsigned StubSize);
40 virtual void* finishFunctionStub(const Function &F);
Chris Lattnerbd199fb2002-12-24 00:01:05 +000041 virtual void emitByte(unsigned char B);
Chris Lattnerbba1b6d2003-06-01 23:24:36 +000042 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);
Chris Lattnerbd199fb2002-12-24 00:01:05 +000055 };
56}
57
Misha Brukman906f5fa2003-06-02 03:23:16 +000058MachineCodeEmitter *VM::createEmitter(VM &V) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +000059 return new Emitter(V);
60}
61
62
63#define _POSIX_MAPPED_FILES
64#include <unistd.h>
65#include <sys/mman.h>
66
Chris Lattner6125fdd2003-05-09 03:30:07 +000067// FIXME: This should be rewritten to support a real memory manager for
68// executable memory pages!
69static void *getMemory(unsigned NumPages) {
Misha Brukman906f5fa2003-06-02 03:23:16 +000070 void *pa;
71 if (NumPages == 0) return 0;
Misha Brukman3a55e8a2003-06-04 19:45:25 +000072 static const long pageSize = sysconf(_SC_PAGESIZE);
73
74#if defined(i386) || defined(__i386__) || defined(__x86__)
Misha Brukman906f5fa2003-06-02 03:23:16 +000075 pa = mmap(0, pageSize*NumPages, PROT_READ|PROT_WRITE|PROT_EXEC,
Misha Brukman3a55e8a2003-06-04 19:45:25 +000076 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;
Misha Brukman3a55e8a2003-06-04 19:45:25 +000083#else
84 std::cerr << "This architecture is not supported by the JIT\n";
85 abort();
86#endif
87
Misha Brukman906f5fa2003-06-02 03:23:16 +000088 if (pa == MAP_FAILED) {
89 perror("mmap");
90 abort();
91 }
92 return pa;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000093}
94
95
96void Emitter::startFunction(MachineFunction &F) {
Chris Lattnerbba1b6d2003-06-01 23:24:36 +000097 CurBlock = (unsigned char *)getMemory(16);
Chris Lattnerbd199fb2002-12-24 00:01:05 +000098 CurByte = CurBlock; // Start writing at the beginning of the fn.
Chris Lattnerbba1b6d2003-06-01 23:24:36 +000099 TheVM->addGlobalMapping(F.getFunction(), CurBlock);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000100}
101
102void Emitter::finishFunction(MachineFunction &F) {
Chris Lattner1cc08382003-01-13 01:00:12 +0000103 ConstantPoolAddresses.clear();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000104 NumBytes += CurByte-CurBlock;
105
Misha Brukman1d440852003-06-06 06:52:35 +0000106 DEBUG(std::cerr << "Finished CodeGen of [0x" << (void*)CurBlock
107 << "] Function: " << F.getFunction()->getName()
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000108 << ": " << CurByte-CurBlock << " bytes of text\n");
109}
110
Chris Lattner1cc08382003-01-13 01:00:12 +0000111void Emitter::emitConstantPool(MachineConstantPool *MCP) {
112 const std::vector<Constant*> &Constants = MCP->getConstants();
113 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
114 // For now we just allocate some memory on the heap, this can be
115 // dramatically improved.
116 const Type *Ty = ((Value*)Constants[i])->getType();
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000117 void *Addr = malloc(TheVM->getTargetData().getTypeSize(Ty));
118 TheVM->InitializeMemory(Constants[i], Addr);
Chris Lattner1cc08382003-01-13 01:00:12 +0000119 ConstantPoolAddresses.push_back(Addr);
120 }
121}
122
Chris Lattner6125fdd2003-05-09 03:30:07 +0000123void Emitter::startFunctionStub(const Function &F, unsigned StubSize) {
Misha Brukman3a55e8a2003-06-04 19:45:25 +0000124 static const long pageSize = sysconf(_SC_PAGESIZE);
Chris Lattner6125fdd2003-05-09 03:30:07 +0000125 SavedCurBlock = CurBlock; SavedCurByte = CurByte;
126 // FIXME: this is a huge waste of memory.
Misha Brukman3a55e8a2003-06-04 19:45:25 +0000127 CurBlock = (unsigned char *)getMemory((StubSize+pageSize-1)/pageSize);
Chris Lattner6125fdd2003-05-09 03:30:07 +0000128 CurByte = CurBlock; // Start writing at the beginning of the fn.
129}
130
131void *Emitter::finishFunctionStub(const Function &F) {
132 NumBytes += CurByte-CurBlock;
133 DEBUG(std::cerr << "Finished CodeGen of [0x" << std::hex
134 << (unsigned)(intptr_t)CurBlock
135 << std::dec << "] Function stub for: " << F.getName()
136 << ": " << CurByte-CurBlock << " bytes of text\n");
137 std::swap(CurBlock, SavedCurBlock);
138 CurByte = SavedCurByte;
139 return SavedCurBlock;
140}
141
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000142void Emitter::emitByte(unsigned char B) {
143 *CurByte++ = B; // Write the byte to memory
144}
145
Chris Lattnerbba1b6d2003-06-01 23:24:36 +0000146void Emitter::emitWord(unsigned W) {
147 // FIXME: This won't work if the endianness of the host and target don't
148 // agree! (For a JIT this can't happen though. :)
149 *(unsigned*)CurByte = W;
150 CurByte += sizeof(unsigned);
151}
152
153
154uint64_t Emitter::getGlobalValueAddress(GlobalValue *V) {
155 // Try looking up the function to see if it is already compiled, if not return
156 // 0.
157 return (intptr_t)TheVM->getPointerToGlobalIfAvailable(V);
158}
159uint64_t Emitter::getGlobalValueAddress(const std::string &Name) {
160 return (intptr_t)TheVM->getPointerToNamedFunction(Name);
161}
162
163// getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry
164// in the constant pool that was last emitted with the 'emitConstantPool'
165// method.
166//
167uint64_t Emitter::getConstantPoolEntryAddress(unsigned ConstantNum) {
168 assert(ConstantNum < ConstantPoolAddresses.size() &&
169 "Invalid ConstantPoolIndex!");
170 return (intptr_t)ConstantPoolAddresses[ConstantNum];
171}
172
173// getCurrentPCValue - This returns the address that the next emitted byte
174// will be output to.
175//
176uint64_t Emitter::getCurrentPCValue() {
177 return (intptr_t)CurByte;
178}
179
180uint64_t Emitter::forceCompilationOf(Function *F) {
181 return (intptr_t)TheVM->getPointerToFunction(F);
182}
183