blob: 77f48b2d882de5b8c3d974dfd692c0eb9e62a001 [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;
35 public:
36 SparcEmitter(VM &vm) : TheVM(vm) {}
37
38 virtual void startFunction(MachineFunction &F);
39 virtual void finishFunction(MachineFunction &F);
40 virtual void emitConstantPool(MachineConstantPool *MCP);
41 virtual void startBasicBlock(MachineBasicBlock &BB);
42 virtual void startFunctionStub(const Function &F, unsigned StubSize);
43 virtual void* finishFunctionStub(const Function &F);
44 virtual void emitByte(unsigned char B);
45 virtual void emitPCRelativeDisp(Value *V);
46 virtual void emitGlobalAddress(GlobalValue *V, bool isPCRelative);
47 virtual void emitGlobalAddress(const std::string &Name, bool isPCRelative);
48 virtual void emitFunctionConstantValueAddress(unsigned ConstantNum,
49 int Offset);
50
51 virtual void saveBBreference(BasicBlock *BB, MachineInstr &MI);
52
53 private:
54 void emitAddress(void *Addr, bool isPCRelative);
55 };
56}
57
58MachineCodeEmitter *VM::createSparcEmitter(VM &V) {
59 return new SparcEmitter(V);
60}
61
62
63#define _POSIX_MAPPED_FILES
64#include <unistd.h>
65#include <sys/mman.h>
66
67// FIXME: This should be rewritten to support a real memory manager for
68// executable memory pages!
69static void *getMemory(unsigned NumPages) {
70 return mmap(0, 4096*NumPages, PROT_READ|PROT_WRITE|PROT_EXEC,
71 MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
72}
73
74
75void SparcEmitter::startFunction(MachineFunction &F) {
76 CurBlock = (unsigned char *)getMemory(8);
77 CurByte = CurBlock; // Start writing at the beginning of the fn.
78 TheVM.addGlobalMapping(F.getFunction(), CurBlock);
79}
80
81void SparcEmitter::finishFunction(MachineFunction &F) {
82 ConstantPoolAddresses.clear();
83 for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
84 // Re-write branches to BasicBlocks for the entire function
85 unsigned Location = BBLocations[BBRefs[i].first];
86 unsigned *Ref = BBRefs[i].second.first;
87 MachineInstr *MI = BBRefs[i].second.second;
88 for (unsigned i=0, e = MI->getNumOperands(); i != e; ++i) {
89 MachineOperand &op = MI->getOperand(i);
90 if (op.isImmediate()) {
91 MI->SetMachineOperandConst(i, op.getType(), Location);
92 break;
93 }
94 }
95 unsigned fixedInstr = SparcV9CodeEmitter::getBinaryCodeForInstr(*MI);
96 *Ref = fixedInstr;
97 }
98 BBRefs.clear();
99 BBLocations.clear();
100
101 NumBytes += CurByte-CurBlock;
102
103 DEBUG(std::cerr << "Finished CodeGen of [0x" << std::hex
104 << (unsigned)(intptr_t)CurBlock
105 << std::dec << "] Function: " << F.getFunction()->getName()
106 << ": " << CurByte-CurBlock << " bytes of text\n");
107}
108
109void SparcEmitter::emitConstantPool(MachineConstantPool *MCP) {
110 const std::vector<Constant*> &Constants = MCP->getConstants();
111 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
112 // For now we just allocate some memory on the heap, this can be
113 // dramatically improved.
114 const Type *Ty = ((Value*)Constants[i])->getType();
115 void *Addr = malloc(TheVM.getTargetData().getTypeSize(Ty));
116 TheVM.InitializeMemory(Constants[i], Addr);
117 ConstantPoolAddresses.push_back(Addr);
118 }
119}
120
121
122void SparcEmitter::startBasicBlock(MachineBasicBlock &BB) {
123 BBLocations[BB.getBasicBlock()] = (unsigned)(intptr_t)CurByte;
124}
125
126
127void SparcEmitter::startFunctionStub(const Function &F, unsigned StubSize) {
128 SavedCurBlock = CurBlock; SavedCurByte = CurByte;
129 // FIXME: this is a huge waste of memory.
130 CurBlock = (unsigned char *)getMemory((StubSize+4095)/4096);
131 CurByte = CurBlock; // Start writing at the beginning of the fn.
132}
133
134void *SparcEmitter::finishFunctionStub(const Function &F) {
135 NumBytes += CurByte-CurBlock;
136 DEBUG(std::cerr << "Finished CodeGen of [0x" << std::hex
137 << (unsigned)(intptr_t)CurBlock
138 << std::dec << "] Function stub for: " << F.getName()
139 << ": " << CurByte-CurBlock << " bytes of text\n");
140 std::swap(CurBlock, SavedCurBlock);
141 CurByte = SavedCurByte;
142 return SavedCurBlock;
143}
144
145void SparcEmitter::emitByte(unsigned char B) {
146 *CurByte++ = B; // Write the byte to memory
147}
148
149// BasicBlock -> pair<memloc, MachineInstr>
150// when the BB is emitted, machineinstr is modified with then-currbyte,
151// processed with MCE, and written out at memloc.
152void SparcEmitter::saveBBreference(BasicBlock *BB, MachineInstr &MI) {
153 BBRefs.push_back(std::make_pair(BB, std::make_pair((unsigned*)CurByte, &MI)));
154}
155
156
157// emitPCRelativeDisp - For functions, just output a displacement that will
158// cause a reference to the zero page, which will cause a seg-fault, causing
159// things to get resolved on demand. Keep track of these markers.
160//
161// For basic block references, keep track of where the references are so they
162// may be patched up when the basic block is defined.
163//
164// BasicBlock -> pair<memloc, MachineInstr>
165// when the BB is emitted, machineinstr is modified with then-currbyte,
166// processed with MCE, and written out at memloc.
167
168void SparcEmitter::emitPCRelativeDisp(Value *V) {
169#if 0
170 BasicBlock *BB = cast<BasicBlock>(V); // Keep track of reference...
171 BBRefs.push_back(std::make_pair(BB, (unsigned*)CurByte));
172 CurByte += 4;
173#endif
174}
175
176// emitAddress - Emit an address in either direct or PCRelative form...
177//
178void SparcEmitter::emitAddress(void *Addr, bool isPCRelative) {
179#if 0
180 if (isPCRelative) {
181 *(intptr_t*)CurByte = (intptr_t)Addr - (intptr_t)CurByte-4;
182 } else {
183 *(void**)CurByte = Addr;
184 }
185 CurByte += 4;
186#endif
187}
188
189void SparcEmitter::emitGlobalAddress(GlobalValue *V, bool isPCRelative) {
190 if (isPCRelative) { // must be a call, this is a major hack!
191 // Try looking up the function to see if it is already compiled!
192 if (void *Addr = TheVM.getPointerToGlobalIfAvailable(V)) {
193 emitAddress(Addr, isPCRelative);
194 } else { // Function has not yet been code generated!
195 TheVM.addFunctionRef(CurByte, cast<Function>(V));
196
197 // Delayed resolution...
198 emitAddress((void*)VM::CompilationCallback, isPCRelative);
199 }
200 } else {
201 emitAddress(TheVM.getPointerToGlobal(V), isPCRelative);
202 }
203}
204
205void SparcEmitter::emitGlobalAddress(const std::string &Name, bool isPCRelative)
206{
207#if 0
208 emitAddress(TheVM.getPointerToNamedFunction(Name), isPCRelative);
209#endif
210}
211
212void SparcEmitter::emitFunctionConstantValueAddress(unsigned ConstantNum,
213 int Offset) {
214 assert(ConstantNum < ConstantPoolAddresses.size() &&
215 "Invalid ConstantPoolIndex!");
216 *(void**)CurByte = (char*)ConstantPoolAddresses[ConstantNum]+Offset;
217 CurByte += 4;
218}