blob: 601be92857db3b4641ee78c6b7ea955b0156f71a [file] [log] [blame]
Misha Brukmanabb027c2003-05-27 21:40:39 +00001//===-- SparcEmitter.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/CodeGen/MachineConstantPool.h"
12#include "llvm/CodeGen/MachineInstr.h"
13#include "llvm/Target/TargetData.h"
14#include "llvm/Function.h"
15#include "Support/Statistic.h"
16// FIXME
17#include "../../../lib/Target/Sparc/SparcV9CodeEmitter.h"
18
19namespace {
20 Statistic<> NumBytes("jello", "Number of bytes of machine code compiled");
21
22 class SparcEmitter : public MachineCodeEmitter {
23 VM &TheVM;
24
25 unsigned char *CurBlock, *CurByte;
26
27 // When outputting a function stub in the context of some other function, we
28 // save CurBlock and CurByte here.
29 unsigned char *SavedCurBlock, *SavedCurByte;
30
31 std::vector<std::pair<BasicBlock*,
32 std::pair<unsigned*,MachineInstr*> > > BBRefs;
33 std::map<BasicBlock*, unsigned> BBLocations;
34 std::vector<void*> ConstantPoolAddresses;
Misha Brukman8e5bf702003-05-28 18:44:38 +000035 std::vector<void*> funcMemory;
Misha Brukmanabb027c2003-05-27 21:40:39 +000036 public:
37 SparcEmitter(VM &vm) : TheVM(vm) {}
Misha Brukman8e5bf702003-05-28 18:44:38 +000038 ~SparcEmitter() {
39 while (! funcMemory.empty()) {
40 void* addr = funcMemory.back();
41 free(addr);
42 funcMemory.pop_back();
43 }
44 }
Misha Brukmanabb027c2003-05-27 21:40:39 +000045
46 virtual void startFunction(MachineFunction &F);
47 virtual void finishFunction(MachineFunction &F);
48 virtual void emitConstantPool(MachineConstantPool *MCP);
49 virtual void startBasicBlock(MachineBasicBlock &BB);
50 virtual void startFunctionStub(const Function &F, unsigned StubSize);
51 virtual void* finishFunctionStub(const Function &F);
52 virtual void emitByte(unsigned char B);
53 virtual void emitPCRelativeDisp(Value *V);
54 virtual void emitGlobalAddress(GlobalValue *V, bool isPCRelative);
55 virtual void emitGlobalAddress(const std::string &Name, bool isPCRelative);
56 virtual void emitFunctionConstantValueAddress(unsigned ConstantNum,
57 int Offset);
58
59 virtual void saveBBreference(BasicBlock *BB, MachineInstr &MI);
Misha Brukman8e5bf702003-05-28 18:44:38 +000060
Misha Brukmanabb027c2003-05-27 21:40:39 +000061
62 private:
63 void emitAddress(void *Addr, bool isPCRelative);
Misha Brukman8e5bf702003-05-28 18:44:38 +000064 void* getMemory(unsigned NumPages);
Misha Brukmanabb027c2003-05-27 21:40:39 +000065 };
66}
67
68MachineCodeEmitter *VM::createSparcEmitter(VM &V) {
69 return new SparcEmitter(V);
70}
71
72
73#define _POSIX_MAPPED_FILES
74#include <unistd.h>
75#include <sys/mman.h>
76
77// FIXME: This should be rewritten to support a real memory manager for
78// executable memory pages!
Misha Brukman8e5bf702003-05-28 18:44:38 +000079void * SparcEmitter::getMemory(unsigned NumPages) {
Brian Gaeke6607fbe2003-05-30 03:37:13 +000080 void *pa;
81 if (NumPages == 0) return 0;
82 static const long pageSize = sysconf (_SC_PAGESIZE);
83 pa = mmap(0, pageSize*NumPages, PROT_READ|PROT_WRITE|PROT_EXEC,
84 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
Misha Brukman8e5bf702003-05-28 18:44:38 +000085 if (pa == MAP_FAILED) {
86 perror("mmap");
87 abort();
88 }
Misha Brukman8e5bf702003-05-28 18:44:38 +000089 return pa;
Misha Brukmanabb027c2003-05-27 21:40:39 +000090}
91
92
93void SparcEmitter::startFunction(MachineFunction &F) {
94 CurBlock = (unsigned char *)getMemory(8);
Misha Brukman8e5bf702003-05-28 18:44:38 +000095 std::cerr << "Starting function " << F.getFunction()->getName() << "\n";
Misha Brukmanabb027c2003-05-27 21:40:39 +000096 CurByte = CurBlock; // Start writing at the beginning of the fn.
97 TheVM.addGlobalMapping(F.getFunction(), CurBlock);
98}
99
100void SparcEmitter::finishFunction(MachineFunction &F) {
101 ConstantPoolAddresses.clear();
Misha Brukman8e5bf702003-05-28 18:44:38 +0000102 // Re-write branches to BasicBlocks for the entire function
Misha Brukmanabb027c2003-05-27 21:40:39 +0000103 for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
Misha Brukmanabb027c2003-05-27 21:40:39 +0000104 unsigned Location = BBLocations[BBRefs[i].first];
105 unsigned *Ref = BBRefs[i].second.first;
106 MachineInstr *MI = BBRefs[i].second.second;
107 for (unsigned i=0, e = MI->getNumOperands(); i != e; ++i) {
108 MachineOperand &op = MI->getOperand(i);
109 if (op.isImmediate()) {
110 MI->SetMachineOperandConst(i, op.getType(), Location);
111 break;
112 }
113 }
114 unsigned fixedInstr = SparcV9CodeEmitter::getBinaryCodeForInstr(*MI);
115 *Ref = fixedInstr;
116 }
117 BBRefs.clear();
118 BBLocations.clear();
119
120 NumBytes += CurByte-CurBlock;
121
122 DEBUG(std::cerr << "Finished CodeGen of [0x" << std::hex
123 << (unsigned)(intptr_t)CurBlock
124 << std::dec << "] Function: " << F.getFunction()->getName()
125 << ": " << CurByte-CurBlock << " bytes of text\n");
126}
127
128void SparcEmitter::emitConstantPool(MachineConstantPool *MCP) {
129 const std::vector<Constant*> &Constants = MCP->getConstants();
130 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
131 // For now we just allocate some memory on the heap, this can be
132 // dramatically improved.
133 const Type *Ty = ((Value*)Constants[i])->getType();
134 void *Addr = malloc(TheVM.getTargetData().getTypeSize(Ty));
135 TheVM.InitializeMemory(Constants[i], Addr);
136 ConstantPoolAddresses.push_back(Addr);
137 }
138}
139
140
141void SparcEmitter::startBasicBlock(MachineBasicBlock &BB) {
142 BBLocations[BB.getBasicBlock()] = (unsigned)(intptr_t)CurByte;
143}
144
145
146void SparcEmitter::startFunctionStub(const Function &F, unsigned StubSize) {
147 SavedCurBlock = CurBlock; SavedCurByte = CurByte;
148 // FIXME: this is a huge waste of memory.
149 CurBlock = (unsigned char *)getMemory((StubSize+4095)/4096);
150 CurByte = CurBlock; // Start writing at the beginning of the fn.
151}
152
153void *SparcEmitter::finishFunctionStub(const Function &F) {
154 NumBytes += CurByte-CurBlock;
155 DEBUG(std::cerr << "Finished CodeGen of [0x" << std::hex
156 << (unsigned)(intptr_t)CurBlock
157 << std::dec << "] Function stub for: " << F.getName()
158 << ": " << CurByte-CurBlock << " bytes of text\n");
159 std::swap(CurBlock, SavedCurBlock);
160 CurByte = SavedCurByte;
161 return SavedCurBlock;
162}
163
164void SparcEmitter::emitByte(unsigned char B) {
165 *CurByte++ = B; // Write the byte to memory
166}
167
168// BasicBlock -> pair<memloc, MachineInstr>
169// when the BB is emitted, machineinstr is modified with then-currbyte,
170// processed with MCE, and written out at memloc.
Misha Brukman8e5bf702003-05-28 18:44:38 +0000171// Should be called by the emitter if its outputting a PCRelative disp
Misha Brukmanabb027c2003-05-27 21:40:39 +0000172void SparcEmitter::saveBBreference(BasicBlock *BB, MachineInstr &MI) {
173 BBRefs.push_back(std::make_pair(BB, std::make_pair((unsigned*)CurByte, &MI)));
174}
175
176
177// emitPCRelativeDisp - For functions, just output a displacement that will
178// cause a reference to the zero page, which will cause a seg-fault, causing
179// things to get resolved on demand. Keep track of these markers.
180//
181// For basic block references, keep track of where the references are so they
182// may be patched up when the basic block is defined.
183//
184// BasicBlock -> pair<memloc, MachineInstr>
185// when the BB is emitted, machineinstr is modified with then-currbyte,
186// processed with MCE, and written out at memloc.
187
188void SparcEmitter::emitPCRelativeDisp(Value *V) {
189#if 0
190 BasicBlock *BB = cast<BasicBlock>(V); // Keep track of reference...
191 BBRefs.push_back(std::make_pair(BB, (unsigned*)CurByte));
192 CurByte += 4;
193#endif
194}
195
196// emitAddress - Emit an address in either direct or PCRelative form...
197//
198void SparcEmitter::emitAddress(void *Addr, bool isPCRelative) {
199#if 0
200 if (isPCRelative) {
201 *(intptr_t*)CurByte = (intptr_t)Addr - (intptr_t)CurByte-4;
202 } else {
203 *(void**)CurByte = Addr;
204 }
205 CurByte += 4;
206#endif
207}
208
209void SparcEmitter::emitGlobalAddress(GlobalValue *V, bool isPCRelative) {
210 if (isPCRelative) { // must be a call, this is a major hack!
211 // Try looking up the function to see if it is already compiled!
212 if (void *Addr = TheVM.getPointerToGlobalIfAvailable(V)) {
213 emitAddress(Addr, isPCRelative);
214 } else { // Function has not yet been code generated!
215 TheVM.addFunctionRef(CurByte, cast<Function>(V));
216
217 // Delayed resolution...
218 emitAddress((void*)VM::CompilationCallback, isPCRelative);
219 }
220 } else {
221 emitAddress(TheVM.getPointerToGlobal(V), isPCRelative);
222 }
223}
224
225void SparcEmitter::emitGlobalAddress(const std::string &Name, bool isPCRelative)
226{
227#if 0
228 emitAddress(TheVM.getPointerToNamedFunction(Name), isPCRelative);
229#endif
230}
231
232void SparcEmitter::emitFunctionConstantValueAddress(unsigned ConstantNum,
233 int Offset) {
234 assert(ConstantNum < ConstantPoolAddresses.size() &&
235 "Invalid ConstantPoolIndex!");
236 *(void**)CurByte = (char*)ConstantPoolAddresses[ConstantNum]+Offset;
237 CurByte += 4;
238}