blob: 253a229a3818e09f07a73250f5fc9f3ec9c94ceb [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"
11#include "llvm/Function.h"
12#include "Support/Statistic.h"
13
14namespace {
15 Statistic<> NumBytes("jello", "Number of bytes of machine code compiled");
16
17 class Emitter : public MachineCodeEmitter {
18 VM &TheVM;
19
20 unsigned char *CurBlock;
21 unsigned char *CurByte;
22
23 std::vector<std::pair<BasicBlock*, unsigned *> > BBRefs;
24 std::map<BasicBlock*, unsigned> BBLocations;
25 public:
26 Emitter(VM &vm) : TheVM(vm) {}
27
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);
34 };
35}
36
37MachineCodeEmitter *VM::createEmitter(VM &V) {
38 return new Emitter(V);
39}
40
41
42#define _POSIX_MAPPED_FILES
43#include <unistd.h>
44#include <sys/mman.h>
45
46static void *getMemory() {
47 return mmap(0, 4096*2, PROT_READ|PROT_WRITE|PROT_EXEC,
48 MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
49}
50
51
52void 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);
56}
57
58void 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;
63 }
64 BBRefs.clear();
65 BBLocations.clear();
66
67 NumBytes += CurByte-CurBlock;
68
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");
72}
73
74void Emitter::startBasicBlock(MachineBasicBlock &BB) {
75 BBLocations[BB.getBasicBlock()] = (unsigned)CurByte;
76}
77
78
79void Emitter::emitByte(unsigned char B) {
80 *CurByte++ = B; // Write the byte to memory
81}
82
83
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.
87//
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.
90//
91void 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
96 CurByte += 4;
97 } else {
98 BasicBlock *BB = cast<BasicBlock>(V); // Keep track of reference...
99 BBRefs.push_back(std::make_pair(BB, (unsigned*)CurByte));
100 CurByte += 4;
101 }
102}
103
104void Emitter::emitGlobalAddress(GlobalValue *V) {
105 *(void**)CurByte = TheVM.getPointerToGlobal(V);
106 CurByte += 4;
107}