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"
17 Statistic<> NumBytes("jello", "Number of bytes of machine code compiled");
19 class Emitter : public MachineCodeEmitter {
22 unsigned char *CurBlock;
23 unsigned char *CurByte;
25 std::vector<std::pair<BasicBlock*, unsigned *> > BBRefs;
26 std::map<BasicBlock*, unsigned> BBLocations;
27 std::vector<void*> ConstantPoolAddresses;
29 Emitter(VM &vm) : TheVM(vm) {}
31 virtual void startFunction(MachineFunction &F);
32 virtual void finishFunction(MachineFunction &F);
33 virtual void emitConstantPool(MachineConstantPool *MCP);
34 virtual void startBasicBlock(MachineBasicBlock &BB);
35 virtual void emitByte(unsigned char B);
36 virtual void emitPCRelativeDisp(Value *V);
37 virtual void emitGlobalAddress(GlobalValue *V, bool isPCRelative);
38 virtual void emitGlobalAddress(const std::string &Name, bool isPCRelative);
39 virtual void emitFunctionConstantValueAddress(unsigned ConstantNum,
42 void emitAddress(void *Addr, bool isPCRelative);
46 MachineCodeEmitter *VM::createEmitter(VM &V) {
47 return new Emitter(V);
51 #define _POSIX_MAPPED_FILES
55 static void *getMemory() {
56 return mmap(0, 4096*8, PROT_READ|PROT_WRITE|PROT_EXEC,
57 MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
61 void Emitter::startFunction(MachineFunction &F) {
62 CurBlock = (unsigned char *)getMemory();
63 CurByte = CurBlock; // Start writing at the beginning of the fn.
64 TheVM.addGlobalMapping(F.getFunction(), CurBlock);
67 void Emitter::finishFunction(MachineFunction &F) {
68 ConstantPoolAddresses.clear();
69 for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
70 unsigned Location = BBLocations[BBRefs[i].first];
71 unsigned *Ref = BBRefs[i].second;
72 *Ref = Location-(unsigned)(intptr_t)Ref-4;
77 NumBytes += CurByte-CurBlock;
79 DEBUG(std::cerr << "Finished CodeGen of [0x" << std::hex
80 << (unsigned)(intptr_t)CurBlock
81 << std::dec << "] Function: " << F.getFunction()->getName()
82 << ": " << CurByte-CurBlock << " bytes of text\n");
85 void Emitter::emitConstantPool(MachineConstantPool *MCP) {
86 const std::vector<Constant*> &Constants = MCP->getConstants();
87 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
88 // For now we just allocate some memory on the heap, this can be
89 // dramatically improved.
90 const Type *Ty = ((Value*)Constants[i])->getType();
91 void *Addr = malloc(TheVM.getTargetData().getTypeSize(Ty));
92 TheVM.InitializeMemory(Constants[i], Addr);
93 ConstantPoolAddresses.push_back(Addr);
98 void Emitter::startBasicBlock(MachineBasicBlock &BB) {
99 BBLocations[BB.getBasicBlock()] = (unsigned)(intptr_t)CurByte;
103 void Emitter::emitByte(unsigned char B) {
104 *CurByte++ = B; // Write the byte to memory
108 // emitPCRelativeDisp - For functions, just output a displacement that will
109 // cause a reference to the zero page, which will cause a seg-fault, causing
110 // things to get resolved on demand. Keep track of these markers.
112 // For basic block references, keep track of where the references are so they
113 // may be patched up when the basic block is defined.
115 void Emitter::emitPCRelativeDisp(Value *V) {
116 BasicBlock *BB = cast<BasicBlock>(V); // Keep track of reference...
117 BBRefs.push_back(std::make_pair(BB, (unsigned*)CurByte));
121 // emitAddress - Emit an address in either direct or PCRelative form...
123 void Emitter::emitAddress(void *Addr, bool isPCRelative) {
125 *(intptr_t*)CurByte = (intptr_t)Addr - (intptr_t)CurByte-4;
127 *(void**)CurByte = Addr;
132 void Emitter::emitGlobalAddress(GlobalValue *V, bool isPCRelative) {
133 if (isPCRelative) { // must be a call, this is a major hack!
134 // FIXME: Try looking up the function to see if it is already compiled!
135 TheVM.addFunctionRef(CurByte, cast<Function>(V));
137 // Delayed resolution...
138 emitAddress((void*)VM::CompilationCallback, isPCRelative);
140 emitAddress(TheVM.getPointerToGlobal(V), isPCRelative);
144 void Emitter::emitGlobalAddress(const std::string &Name, bool isPCRelative) {
145 emitAddress(TheVM.getPointerToNamedFunction(Name), isPCRelative);
148 void Emitter::emitFunctionConstantValueAddress(unsigned ConstantNum,
150 assert(ConstantNum < ConstantPoolAddresses.size() &&
151 "Invalid ConstantPoolIndex!");
152 *(void**)CurByte = (char*)ConstantPoolAddresses[ConstantNum]+Offset;