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/Function.h"
12 #include "Support/Statistic.h"
15 Statistic<> NumBytes("jello", "Number of bytes of machine code compiled");
17 class Emitter : public MachineCodeEmitter {
20 unsigned char *CurBlock;
21 unsigned char *CurByte;
23 std::vector<std::pair<BasicBlock*, unsigned *> > BBRefs;
24 std::map<BasicBlock*, unsigned> BBLocations;
26 Emitter(VM &vm) : TheVM(vm) {}
28 virtual void startFunction(MachineFunction &F);
29 virtual void finishFunction(MachineFunction &F);
30 virtual void startBasicBlock(MachineBasicBlock &BB);
31 virtual void emitByte(unsigned char B);
32 virtual void emitPCRelativeDisp(Value *V);
33 virtual void emitGlobalAddress(GlobalValue *V);
37 MachineCodeEmitter *VM::createEmitter(VM &V) {
38 return new Emitter(V);
42 #define _POSIX_MAPPED_FILES
46 static void *getMemory() {
47 return mmap(0, 4096*2, PROT_READ|PROT_WRITE|PROT_EXEC,
48 MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
52 void Emitter::startFunction(MachineFunction &F) {
53 CurBlock = (unsigned char *)getMemory();
54 CurByte = CurBlock; // Start writing at the beginning of the fn.
55 TheVM.addGlobalMapping(F.getFunction(), CurBlock);
58 void Emitter::finishFunction(MachineFunction &F) {
59 for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
60 unsigned Location = BBLocations[BBRefs[i].first];
61 unsigned *Ref = BBRefs[i].second;
62 *Ref = Location-(unsigned)Ref-4;
67 NumBytes += CurByte-CurBlock;
69 DEBUG(std::cerr << "Finished CodeGen of [" << std::hex << (unsigned)CurBlock
70 << std::dec << "] Function: " << F.getFunction()->getName()
71 << ": " << CurByte-CurBlock << " bytes of text\n");
74 void Emitter::startBasicBlock(MachineBasicBlock &BB) {
75 BBLocations[BB.getBasicBlock()] = (unsigned)CurByte;
79 void Emitter::emitByte(unsigned char B) {
80 *CurByte++ = B; // Write the byte to memory
84 // emitPCRelativeDisp - For functions, just output a displacement that will
85 // cause a reference to the zero page, which will cause a seg-fault, causing
86 // things to get resolved on demand. Keep track of these markers.
88 // For basic block references, keep track of where the references are so they
89 // may be patched up when the basic block is defined.
91 void Emitter::emitPCRelativeDisp(Value *V) {
92 if (Function *F = dyn_cast<Function>(V)) {
93 TheVM.addFunctionRef(CurByte, F);
94 unsigned ZeroAddr = -(unsigned)CurByte-4; // Calculate displacement to null
95 *(unsigned*)CurByte = ZeroAddr; // 4 byte offset
98 BasicBlock *BB = cast<BasicBlock>(V); // Keep track of reference...
99 BBRefs.push_back(std::make_pair(BB, (unsigned*)CurByte));
104 void Emitter::emitGlobalAddress(GlobalValue *V) {
105 *(void**)CurByte = TheVM.getPointerToGlobal(V);