blob: 69e2453090da80c44db725d869e4de65d2ed4b89 [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"
Chris Lattner1cc08382003-01-13 01:00:12 +000011#include "llvm/CodeGen/MachineConstantPool.h"
12#include "llvm/Target/TargetData.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000013#include "llvm/Function.h"
14#include "Support/Statistic.h"
15
16namespace {
17 Statistic<> NumBytes("jello", "Number of bytes of machine code compiled");
18
19 class Emitter : public MachineCodeEmitter {
20 VM &TheVM;
21
22 unsigned char *CurBlock;
23 unsigned char *CurByte;
24
25 std::vector<std::pair<BasicBlock*, unsigned *> > BBRefs;
26 std::map<BasicBlock*, unsigned> BBLocations;
Chris Lattner1cc08382003-01-13 01:00:12 +000027 std::vector<void*> ConstantPoolAddresses;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000028 public:
29 Emitter(VM &vm) : TheVM(vm) {}
30
31 virtual void startFunction(MachineFunction &F);
32 virtual void finishFunction(MachineFunction &F);
Chris Lattner1cc08382003-01-13 01:00:12 +000033 virtual void emitConstantPool(MachineConstantPool *MCP);
Chris Lattnerbd199fb2002-12-24 00:01:05 +000034 virtual void startBasicBlock(MachineBasicBlock &BB);
35 virtual void emitByte(unsigned char B);
36 virtual void emitPCRelativeDisp(Value *V);
Chris Lattner1cc08382003-01-13 01:00:12 +000037 virtual void emitGlobalAddress(GlobalValue *V, bool isPCRelative);
38 virtual void emitGlobalAddress(const std::string &Name, bool isPCRelative);
39 virtual void emitFunctionConstantValueAddress(unsigned ConstantNum,
40 int Offset);
41 private:
42 void emitAddress(void *Addr, bool isPCRelative);
Chris Lattnerbd199fb2002-12-24 00:01:05 +000043 };
44}
45
46MachineCodeEmitter *VM::createEmitter(VM &V) {
47 return new Emitter(V);
48}
49
50
51#define _POSIX_MAPPED_FILES
52#include <unistd.h>
53#include <sys/mman.h>
54
55static void *getMemory() {
Chris Lattner1cc08382003-01-13 01:00:12 +000056 return mmap(0, 4096*8, PROT_READ|PROT_WRITE|PROT_EXEC,
Chris Lattnerbd199fb2002-12-24 00:01:05 +000057 MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
58}
59
60
61void 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);
65}
66
67void Emitter::finishFunction(MachineFunction &F) {
Chris Lattner1cc08382003-01-13 01:00:12 +000068 ConstantPoolAddresses.clear();
Chris Lattnerbd199fb2002-12-24 00:01:05 +000069 for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
70 unsigned Location = BBLocations[BBRefs[i].first];
71 unsigned *Ref = BBRefs[i].second;
Chris Lattner910687e2003-01-29 18:02:02 +000072 *Ref = Location-(unsigned)(intptr_t)Ref-4;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000073 }
74 BBRefs.clear();
75 BBLocations.clear();
76
77 NumBytes += CurByte-CurBlock;
78
Chris Lattner910687e2003-01-29 18:02:02 +000079 DEBUG(std::cerr << "Finished CodeGen of [0x" << std::hex
80 << (unsigned)(intptr_t)CurBlock
Chris Lattnerbd199fb2002-12-24 00:01:05 +000081 << std::dec << "] Function: " << F.getFunction()->getName()
82 << ": " << CurByte-CurBlock << " bytes of text\n");
83}
84
Chris Lattner1cc08382003-01-13 01:00:12 +000085void 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);
94 }
95}
96
97
Chris Lattnerbd199fb2002-12-24 00:01:05 +000098void Emitter::startBasicBlock(MachineBasicBlock &BB) {
Chris Lattner910687e2003-01-29 18:02:02 +000099 BBLocations[BB.getBasicBlock()] = (unsigned)(intptr_t)CurByte;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000100}
101
102
103void Emitter::emitByte(unsigned char B) {
104 *CurByte++ = B; // Write the byte to memory
105}
106
107
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.
111//
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.
114//
115void Emitter::emitPCRelativeDisp(Value *V) {
Chris Lattner1cc08382003-01-13 01:00:12 +0000116 BasicBlock *BB = cast<BasicBlock>(V); // Keep track of reference...
117 BBRefs.push_back(std::make_pair(BB, (unsigned*)CurByte));
118 CurByte += 4;
119}
120
121// emitAddress - Emit an address in either direct or PCRelative form...
122//
123void Emitter::emitAddress(void *Addr, bool isPCRelative) {
124 if (isPCRelative) {
Chris Lattner910687e2003-01-29 18:02:02 +0000125 *(intptr_t*)CurByte = (intptr_t)Addr - (intptr_t)CurByte-4;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000126 } else {
Chris Lattner1cc08382003-01-13 01:00:12 +0000127 *(void**)CurByte = Addr;
128 }
129 CurByte += 4;
130}
131
132void Emitter::emitGlobalAddress(GlobalValue *V, bool isPCRelative) {
133 if (isPCRelative) { // must be a call, this is a major hack!
Chris Lattnereb5a93b2003-05-08 21:44:21 +0000134 // Try looking up the function to see if it is already compiled!
135 if (void *Addr = TheVM.getPointerToGlobalIfAvailable(V)) {
136 emitAddress(Addr, isPCRelative);
137 } else { // Function has not yet been code generated!
138 TheVM.addFunctionRef(CurByte, cast<Function>(V));
Chris Lattnerc309a762003-05-08 21:34:11 +0000139
Chris Lattnereb5a93b2003-05-08 21:44:21 +0000140 // Delayed resolution...
141 emitAddress((void*)VM::CompilationCallback, isPCRelative);
142 }
Chris Lattner1cc08382003-01-13 01:00:12 +0000143 } else {
144 emitAddress(TheVM.getPointerToGlobal(V), isPCRelative);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000145 }
146}
147
Chris Lattner1cc08382003-01-13 01:00:12 +0000148void Emitter::emitGlobalAddress(const std::string &Name, bool isPCRelative) {
149 emitAddress(TheVM.getPointerToNamedFunction(Name), isPCRelative);
150}
151
152void Emitter::emitFunctionConstantValueAddress(unsigned ConstantNum,
153 int Offset) {
154 assert(ConstantNum < ConstantPoolAddresses.size() &&
155 "Invalid ConstantPoolIndex!");
156 *(void**)CurByte = (char*)ConstantPoolAddresses[ConstantNum]+Offset;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000157 CurByte += 4;
158}