blob: 3882b192582b5e96d3eea776b118e475e3fc9ba9 [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) {
80#if 0
81 void *pa = mmap(0, 4096*NumPages, PROT_READ|PROT_WRITE|PROT_EXEC,
82 MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
83 if (pa == MAP_FAILED) {
84 perror("mmap");
85 abort();
86 }
87#endif
88 void *pa = malloc(4096*NumPages);
89 if (!pa) {
90 perror("malloc");
91 abort();
92 }
93 funcMemory.push_back(pa);
94 return pa;
Misha Brukmanabb027c2003-05-27 21:40:39 +000095}
96
97
98void SparcEmitter::startFunction(MachineFunction &F) {
99 CurBlock = (unsigned char *)getMemory(8);
Misha Brukman8e5bf702003-05-28 18:44:38 +0000100 std::cerr << "Starting function " << F.getFunction()->getName() << "\n";
Misha Brukmanabb027c2003-05-27 21:40:39 +0000101 CurByte = CurBlock; // Start writing at the beginning of the fn.
102 TheVM.addGlobalMapping(F.getFunction(), CurBlock);
103}
104
105void SparcEmitter::finishFunction(MachineFunction &F) {
106 ConstantPoolAddresses.clear();
Misha Brukman8e5bf702003-05-28 18:44:38 +0000107 // Re-write branches to BasicBlocks for the entire function
Misha Brukmanabb027c2003-05-27 21:40:39 +0000108 for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
Misha Brukmanabb027c2003-05-27 21:40:39 +0000109 unsigned Location = BBLocations[BBRefs[i].first];
110 unsigned *Ref = BBRefs[i].second.first;
111 MachineInstr *MI = BBRefs[i].second.second;
112 for (unsigned i=0, e = MI->getNumOperands(); i != e; ++i) {
113 MachineOperand &op = MI->getOperand(i);
114 if (op.isImmediate()) {
115 MI->SetMachineOperandConst(i, op.getType(), Location);
116 break;
117 }
118 }
119 unsigned fixedInstr = SparcV9CodeEmitter::getBinaryCodeForInstr(*MI);
120 *Ref = fixedInstr;
121 }
122 BBRefs.clear();
123 BBLocations.clear();
124
125 NumBytes += CurByte-CurBlock;
126
127 DEBUG(std::cerr << "Finished CodeGen of [0x" << std::hex
128 << (unsigned)(intptr_t)CurBlock
129 << std::dec << "] Function: " << F.getFunction()->getName()
130 << ": " << CurByte-CurBlock << " bytes of text\n");
131}
132
133void SparcEmitter::emitConstantPool(MachineConstantPool *MCP) {
134 const std::vector<Constant*> &Constants = MCP->getConstants();
135 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
136 // For now we just allocate some memory on the heap, this can be
137 // dramatically improved.
138 const Type *Ty = ((Value*)Constants[i])->getType();
139 void *Addr = malloc(TheVM.getTargetData().getTypeSize(Ty));
140 TheVM.InitializeMemory(Constants[i], Addr);
141 ConstantPoolAddresses.push_back(Addr);
142 }
143}
144
145
146void SparcEmitter::startBasicBlock(MachineBasicBlock &BB) {
147 BBLocations[BB.getBasicBlock()] = (unsigned)(intptr_t)CurByte;
148}
149
150
151void SparcEmitter::startFunctionStub(const Function &F, unsigned StubSize) {
152 SavedCurBlock = CurBlock; SavedCurByte = CurByte;
153 // FIXME: this is a huge waste of memory.
154 CurBlock = (unsigned char *)getMemory((StubSize+4095)/4096);
155 CurByte = CurBlock; // Start writing at the beginning of the fn.
156}
157
158void *SparcEmitter::finishFunctionStub(const Function &F) {
159 NumBytes += CurByte-CurBlock;
160 DEBUG(std::cerr << "Finished CodeGen of [0x" << std::hex
161 << (unsigned)(intptr_t)CurBlock
162 << std::dec << "] Function stub for: " << F.getName()
163 << ": " << CurByte-CurBlock << " bytes of text\n");
164 std::swap(CurBlock, SavedCurBlock);
165 CurByte = SavedCurByte;
166 return SavedCurBlock;
167}
168
169void SparcEmitter::emitByte(unsigned char B) {
170 *CurByte++ = B; // Write the byte to memory
171}
172
173// BasicBlock -> pair<memloc, MachineInstr>
174// when the BB is emitted, machineinstr is modified with then-currbyte,
175// processed with MCE, and written out at memloc.
Misha Brukman8e5bf702003-05-28 18:44:38 +0000176// Should be called by the emitter if its outputting a PCRelative disp
Misha Brukmanabb027c2003-05-27 21:40:39 +0000177void SparcEmitter::saveBBreference(BasicBlock *BB, MachineInstr &MI) {
178 BBRefs.push_back(std::make_pair(BB, std::make_pair((unsigned*)CurByte, &MI)));
179}
180
181
182// emitPCRelativeDisp - For functions, just output a displacement that will
183// cause a reference to the zero page, which will cause a seg-fault, causing
184// things to get resolved on demand. Keep track of these markers.
185//
186// For basic block references, keep track of where the references are so they
187// may be patched up when the basic block is defined.
188//
189// BasicBlock -> pair<memloc, MachineInstr>
190// when the BB is emitted, machineinstr is modified with then-currbyte,
191// processed with MCE, and written out at memloc.
192
193void SparcEmitter::emitPCRelativeDisp(Value *V) {
194#if 0
195 BasicBlock *BB = cast<BasicBlock>(V); // Keep track of reference...
196 BBRefs.push_back(std::make_pair(BB, (unsigned*)CurByte));
197 CurByte += 4;
198#endif
199}
200
201// emitAddress - Emit an address in either direct or PCRelative form...
202//
203void SparcEmitter::emitAddress(void *Addr, bool isPCRelative) {
204#if 0
205 if (isPCRelative) {
206 *(intptr_t*)CurByte = (intptr_t)Addr - (intptr_t)CurByte-4;
207 } else {
208 *(void**)CurByte = Addr;
209 }
210 CurByte += 4;
211#endif
212}
213
214void SparcEmitter::emitGlobalAddress(GlobalValue *V, bool isPCRelative) {
215 if (isPCRelative) { // must be a call, this is a major hack!
216 // Try looking up the function to see if it is already compiled!
217 if (void *Addr = TheVM.getPointerToGlobalIfAvailable(V)) {
218 emitAddress(Addr, isPCRelative);
219 } else { // Function has not yet been code generated!
220 TheVM.addFunctionRef(CurByte, cast<Function>(V));
221
222 // Delayed resolution...
223 emitAddress((void*)VM::CompilationCallback, isPCRelative);
224 }
225 } else {
226 emitAddress(TheVM.getPointerToGlobal(V), isPCRelative);
227 }
228}
229
230void SparcEmitter::emitGlobalAddress(const std::string &Name, bool isPCRelative)
231{
232#if 0
233 emitAddress(TheVM.getPointerToNamedFunction(Name), isPCRelative);
234#endif
235}
236
237void SparcEmitter::emitFunctionConstantValueAddress(unsigned ConstantNum,
238 int Offset) {
239 assert(ConstantNum < ConstantPoolAddresses.size() &&
240 "Invalid ConstantPoolIndex!");
241 *(void**)CurByte = (char*)ConstantPoolAddresses[ConstantNum]+Offset;
242 CurByte += 4;
243}