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 "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"
19 Statistic<> NumBytes("jello", "Number of bytes of machine code compiled");
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;
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;
30 // ConstantPoolAddresses - Contains the location for each entry in the
32 std::vector<void*> ConstantPoolAddresses;
34 Emitter(VM &vm) { TheVM = &vm; }
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);
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();
49 // forceCompilationOf - Force the compilation of the specified function, and
50 // return its address, because we REALLY need the address now.
52 // FIXME: This is JIT specific!
54 virtual uint64_t forceCompilationOf(Function *F);
58 MachineCodeEmitter *VM::createEmitter(VM &V) {
59 return new Emitter(V);
63 #define _POSIX_MAPPED_FILES
67 // FIXME: This should be rewritten to support a real memory manager for
68 // executable memory pages!
69 static void *getMemory(unsigned NumPages) {
70 #if defined(i386) || defined(__i386__) || defined(__x86__)
71 static const int fd = 0;
72 #elif defined(sparc) || defined(__sparc__) || defined(__sparcv9)
73 static const int fd = -1;
75 // This is an unsupported architecture.
76 static const int fd = 0;
80 if (NumPages == 0) return 0;
81 static const long pageSize = sysconf (_SC_PAGESIZE);
82 pa = mmap(0, pageSize*NumPages, PROT_READ|PROT_WRITE|PROT_EXEC,
83 MAP_PRIVATE|MAP_ANONYMOUS, fd, 0);
84 if (pa == MAP_FAILED) {
92 void Emitter::startFunction(MachineFunction &F) {
93 CurBlock = (unsigned char *)getMemory(16);
94 CurByte = CurBlock; // Start writing at the beginning of the fn.
95 TheVM->addGlobalMapping(F.getFunction(), CurBlock);
98 void Emitter::finishFunction(MachineFunction &F) {
99 ConstantPoolAddresses.clear();
100 NumBytes += CurByte-CurBlock;
102 DEBUG(std::cerr << "Finished CodeGen of [0x" << std::hex
103 << (unsigned)(intptr_t)CurBlock
104 << std::dec << "] Function: " << F.getFunction()->getName()
105 << ": " << CurByte-CurBlock << " bytes of text\n");
108 void Emitter::emitConstantPool(MachineConstantPool *MCP) {
109 const std::vector<Constant*> &Constants = MCP->getConstants();
110 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
111 // For now we just allocate some memory on the heap, this can be
112 // dramatically improved.
113 const Type *Ty = ((Value*)Constants[i])->getType();
114 void *Addr = malloc(TheVM->getTargetData().getTypeSize(Ty));
115 TheVM->InitializeMemory(Constants[i], Addr);
116 ConstantPoolAddresses.push_back(Addr);
120 void Emitter::startFunctionStub(const Function &F, unsigned StubSize) {
121 SavedCurBlock = CurBlock; SavedCurByte = CurByte;
122 // FIXME: this is a huge waste of memory.
123 CurBlock = (unsigned char *)getMemory((StubSize+4095)/4096);
124 CurByte = CurBlock; // Start writing at the beginning of the fn.
127 void *Emitter::finishFunctionStub(const Function &F) {
128 NumBytes += CurByte-CurBlock;
129 DEBUG(std::cerr << "Finished CodeGen of [0x" << std::hex
130 << (unsigned)(intptr_t)CurBlock
131 << std::dec << "] Function stub for: " << F.getName()
132 << ": " << CurByte-CurBlock << " bytes of text\n");
133 std::swap(CurBlock, SavedCurBlock);
134 CurByte = SavedCurByte;
135 return SavedCurBlock;
138 void Emitter::emitByte(unsigned char B) {
139 *CurByte++ = B; // Write the byte to memory
142 void Emitter::emitWord(unsigned W) {
143 // FIXME: This won't work if the endianness of the host and target don't
144 // agree! (For a JIT this can't happen though. :)
145 *(unsigned*)CurByte = W;
146 CurByte += sizeof(unsigned);
150 uint64_t Emitter::getGlobalValueAddress(GlobalValue *V) {
151 // Try looking up the function to see if it is already compiled, if not return
153 return (intptr_t)TheVM->getPointerToGlobalIfAvailable(V);
155 uint64_t Emitter::getGlobalValueAddress(const std::string &Name) {
156 return (intptr_t)TheVM->getPointerToNamedFunction(Name);
159 // getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry
160 // in the constant pool that was last emitted with the 'emitConstantPool'
163 uint64_t Emitter::getConstantPoolEntryAddress(unsigned ConstantNum) {
164 assert(ConstantNum < ConstantPoolAddresses.size() &&
165 "Invalid ConstantPoolIndex!");
166 return (intptr_t)ConstantPoolAddresses[ConstantNum];
169 // getCurrentPCValue - This returns the address that the next emitted byte
170 // will be output to.
172 uint64_t Emitter::getCurrentPCValue() {
173 return (intptr_t)CurByte;
176 uint64_t Emitter::forceCompilationOf(Function *F) {
177 return (intptr_t)TheVM->getPointerToFunction(F);