1 //===-- Emitter.cpp - Write machine code to executable memory -------------===//
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.
6 //===----------------------------------------------------------------------===//
9 #include "Config/sys/mman.h"
10 #include "llvm/CodeGen/MachineCodeEmitter.h"
11 #include "llvm/CodeGen/MachineFunction.h"
12 #include "llvm/CodeGen/MachineConstantPool.h"
13 #include "llvm/Target/TargetData.h"
14 #include "llvm/Module.h"
15 #include "Support/Debug.h"
16 #include "Support/Statistic.h"
20 Statistic<> NumBytes("jello", "Number of bytes of machine code compiled");
23 class Emitter : public MachineCodeEmitter {
24 // CurBlock - The start of the current block of memory. CurByte - The
25 // current byte being emitted to.
26 unsigned char *CurBlock, *CurByte;
28 // When outputting a function stub in the context of some other function, we
29 // save CurBlock and CurByte here.
30 unsigned char *SavedCurBlock, *SavedCurByte;
32 // ConstantPoolAddresses - Contains the location for each entry in the
34 std::vector<void*> ConstantPoolAddresses;
36 Emitter(VM &vm) { TheVM = &vm; }
38 virtual void startFunction(MachineFunction &F);
39 virtual void finishFunction(MachineFunction &F);
40 virtual void emitConstantPool(MachineConstantPool *MCP);
41 virtual void startFunctionStub(const Function &F, unsigned StubSize);
42 virtual void* finishFunctionStub(const Function &F);
43 virtual void emitByte(unsigned char B);
44 virtual void emitWord(unsigned W);
46 virtual uint64_t getGlobalValueAddress(GlobalValue *V);
47 virtual uint64_t getGlobalValueAddress(const std::string &Name);
48 virtual uint64_t getConstantPoolEntryAddress(unsigned Entry);
49 virtual uint64_t getCurrentPCValue();
51 // forceCompilationOf - Force the compilation of the specified function, and
52 // return its address, because we REALLY need the address now.
54 // FIXME: This is JIT specific!
56 virtual uint64_t forceCompilationOf(Function *F);
60 MachineCodeEmitter *VM::createEmitter(VM &V) {
61 return new Emitter(V);
65 #define _POSIX_MAPPED_FILES
69 // FIXME: This should be rewritten to support a real memory manager for
70 // executable memory pages!
71 static void *getMemory(unsigned NumPages) {
73 if (NumPages == 0) return 0;
74 static const long pageSize = sysconf(_SC_PAGESIZE);
76 #if defined(i386) || defined(__i386__) || defined(__x86__)
77 /* Linux and *BSD tend to have these flags named differently. */
78 #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
79 # define MAP_ANONYMOUS MAP_ANON
80 #endif /* defined(MAP_ANON) && !defined(MAP_ANONYMOUS) */
82 #elif defined(sparc) || defined(__sparc__) || defined(__sparcv9)
85 std::cerr << "This architecture is not supported by the JIT!\n";
88 pa = mmap(0, pageSize*NumPages, PROT_READ|PROT_WRITE|PROT_EXEC,
89 MAP_PRIVATE|MAP_ANONYMOUS, fd, 0);
90 if (pa == MAP_FAILED) {
98 void Emitter::startFunction(MachineFunction &F) {
99 CurBlock = (unsigned char *)getMemory(16);
100 CurByte = CurBlock; // Start writing at the beginning of the fn.
101 TheVM->addGlobalMapping(F.getFunction(), CurBlock);
104 void Emitter::finishFunction(MachineFunction &F) {
105 ConstantPoolAddresses.clear();
106 NumBytes += CurByte-CurBlock;
108 DEBUG(std::cerr << "Finished CodeGen of [" << (void*)CurBlock
109 << "] Function: " << F.getFunction()->getName()
110 << ": " << CurByte-CurBlock << " bytes of text\n");
113 void Emitter::emitConstantPool(MachineConstantPool *MCP) {
114 const std::vector<Constant*> &Constants = MCP->getConstants();
115 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
116 // For now we just allocate some memory on the heap, this can be
117 // dramatically improved.
118 const Type *Ty = ((Value*)Constants[i])->getType();
119 void *Addr = malloc(TheVM->getTargetData().getTypeSize(Ty));
120 TheVM->InitializeMemory(Constants[i], Addr);
121 ConstantPoolAddresses.push_back(Addr);
125 void Emitter::startFunctionStub(const Function &F, unsigned StubSize) {
126 static const long pageSize = sysconf(_SC_PAGESIZE);
127 SavedCurBlock = CurBlock; SavedCurByte = CurByte;
128 // FIXME: this is a huge waste of memory.
129 CurBlock = (unsigned char *)getMemory((StubSize+pageSize-1)/pageSize);
130 CurByte = CurBlock; // Start writing at the beginning of the fn.
133 void *Emitter::finishFunctionStub(const Function &F) {
134 NumBytes += CurByte-CurBlock;
135 DEBUG(std::cerr << "Finished CodeGen of [0x" << std::hex
136 << (unsigned)(intptr_t)CurBlock
137 << std::dec << "] Function stub for: " << F.getName()
138 << ": " << CurByte-CurBlock << " bytes of text\n");
139 std::swap(CurBlock, SavedCurBlock);
140 CurByte = SavedCurByte;
141 return SavedCurBlock;
144 void Emitter::emitByte(unsigned char B) {
145 *CurByte++ = B; // Write the byte to memory
148 void Emitter::emitWord(unsigned W) {
149 // FIXME: This won't work if the endianness of the host and target don't
150 // agree! (For a JIT this can't happen though. :)
151 *(unsigned*)CurByte = W;
152 CurByte += sizeof(unsigned);
156 uint64_t Emitter::getGlobalValueAddress(GlobalValue *V) {
157 // Try looking up the function to see if it is already compiled, if not return
159 return (intptr_t)TheVM->getPointerToGlobalIfAvailable(V);
161 uint64_t Emitter::getGlobalValueAddress(const std::string &Name) {
162 return (intptr_t)TheVM->getPointerToNamedFunction(Name);
165 // getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry
166 // in the constant pool that was last emitted with the 'emitConstantPool'
169 uint64_t Emitter::getConstantPoolEntryAddress(unsigned ConstantNum) {
170 assert(ConstantNum < ConstantPoolAddresses.size() &&
171 "Invalid ConstantPoolIndex!");
172 return (intptr_t)ConstantPoolAddresses[ConstantNum];
175 // getCurrentPCValue - This returns the address that the next emitted byte
176 // will be output to.
178 uint64_t Emitter::getCurrentPCValue() {
179 return (intptr_t)CurByte;
182 uint64_t Emitter::forceCompilationOf(Function *F) {
183 return (intptr_t)TheVM->getPointerToFunction(F);
186 // getPointerToNamedFunction - This function is used as a global wrapper to
187 // VM::getPointerToNamedFunction for the purpose of resolving symbols when
188 // bugpoint is debugging the JIT. In that scenario, we are loading an .so and
189 // need to resolve function(s) that are being mis-codegenerated, so we need to
190 // resolve their addresses at runtime, and this is the way to do it.
192 void *getPointerToNamedFunction(const char *Name) {
193 Module &M = TheVM->getModule();
194 if (Function *F = M.getNamedFunction(Name))
195 return TheVM->getPointerToFunction(F);
196 return TheVM->getPointerToNamedFunction(Name);