blob: 0e54322ef9b40c6fc90aaf37c1bd3aa1ac39bd00 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
//===-- Emitter.cpp - Write machine code to executable memory -------------===//
//
// This file defines a MachineCodeEmitter object that is used by Jello to write
// machine code to memory and remember where relocatable values lie.
//
//===----------------------------------------------------------------------===//
#include "VM.h"
#include "llvm/CodeGen/MachineCodeEmitter.h"
#include "llvm/CodeGen/MachineFunction.h"
namespace {
class Emitter : public MachineCodeEmitter {
VM &TheVM;
unsigned char *CurBlock;
unsigned char *CurByte;
public:
Emitter(VM &vm) : TheVM(vm) {}
virtual void startFunction(MachineFunction &F);
virtual void finishFunction(MachineFunction &F);
virtual void startBasicBlock(MachineBasicBlock &BB) {}
virtual void emitByte(unsigned char B);
virtual void emitPCRelativeDisp(Value *V);
};
}
MachineCodeEmitter *VM::createEmitter(VM &V) {
return new Emitter(V);
}
#define _POSIX_MAPPED_FILES
#include <unistd.h>
#include <sys/mman.h>
static void *getMemory() {
return mmap(0, 4096*2, PROT_READ|PROT_WRITE|PROT_EXEC,
MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
}
void Emitter::startFunction(MachineFunction &F) {
CurBlock = (unsigned char *)getMemory();
CurByte = CurBlock; // Start writing at the beginning of the fn.
}
#include <iostream>
#include "llvm/Function.h"
void Emitter::finishFunction(MachineFunction &F) {
std::cerr << "Finished Code Generation of Function: "
<< F.getFunction()->getName() << ": " << CurByte-CurBlock
<< " bytes of text\n";
TheVM.addGlobalMapping(F.getFunction(), CurBlock);
}
void Emitter::emitByte(unsigned char B) {
*CurByte++ = B; // Write the byte to memory
}
// emitPCRelativeDisp - Just output a displacement that will cause a reference
// to the zero page, which will cause a seg-fault, causing things to get
// resolved on demand. Keep track of these markers.
//
void Emitter::emitPCRelativeDisp(Value *V) {
TheVM.addFunctionRef(CurByte, cast<Function>(V));
unsigned ZeroAddr = -(unsigned)CurByte-4; // Calculate displacement to null
*(unsigned*)CurByte = ZeroAddr; // 4 byte offset
CurByte += 4;
}
|