blob: a9547ac38ab9c591c0175181bac0284eedf97fa4 [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;
Misha Brukmanf86d6352003-05-31 06:26:48 +000033 std::map<BasicBlock*, long> BBLocations;
Misha Brukmanabb027c2003-05-27 21:40:39 +000034 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);
Misha Brukman8e5bf702003-05-28 18:44:38 +000052
Misha Brukmanabb027c2003-05-27 21:40:39 +000053
54 private:
55 void emitAddress(void *Addr, bool isPCRelative);
Misha Brukman8e5bf702003-05-28 18:44:38 +000056 void* getMemory(unsigned NumPages);
Misha Brukmanabb027c2003-05-27 21:40:39 +000057 };
58}
59
60MachineCodeEmitter *VM::createSparcEmitter(VM &V) {
61 return new SparcEmitter(V);
62}
63
64
65#define _POSIX_MAPPED_FILES
66#include <unistd.h>
67#include <sys/mman.h>
68
69// FIXME: This should be rewritten to support a real memory manager for
70// executable memory pages!
Misha Brukman417a7c02003-05-30 20:39:37 +000071void* SparcEmitter::getMemory(unsigned NumPages) {
Brian Gaeke6607fbe2003-05-30 03:37:13 +000072 void *pa;
73 if (NumPages == 0) return 0;
74 static const long pageSize = sysconf (_SC_PAGESIZE);
75 pa = mmap(0, pageSize*NumPages, PROT_READ|PROT_WRITE|PROT_EXEC,
Misha Brukman417a7c02003-05-30 20:39:37 +000076 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
Misha Brukman8e5bf702003-05-28 18:44:38 +000077 if (pa == MAP_FAILED) {
78 perror("mmap");
79 abort();
80 }
Misha Brukman8e5bf702003-05-28 18:44:38 +000081 return pa;
Misha Brukmanabb027c2003-05-27 21:40:39 +000082}
83
84
85void SparcEmitter::startFunction(MachineFunction &F) {
Misha Brukman8e5bf702003-05-28 18:44:38 +000086 std::cerr << "Starting function " << F.getFunction()->getName() << "\n";
Misha Brukman417a7c02003-05-30 20:39:37 +000087 CurBlock = (unsigned char *)getMemory(8);
Misha Brukmanabb027c2003-05-27 21:40:39 +000088 CurByte = CurBlock; // Start writing at the beginning of the fn.
89 TheVM.addGlobalMapping(F.getFunction(), CurBlock);
90}
91
92void SparcEmitter::finishFunction(MachineFunction &F) {
Misha Brukman417a7c02003-05-30 20:39:37 +000093 std::cerr << "Finishing function " << F.getFunction()->getName() << "\n";
Misha Brukmanabb027c2003-05-27 21:40:39 +000094 ConstantPoolAddresses.clear();
Misha Brukman8e5bf702003-05-28 18:44:38 +000095 // Re-write branches to BasicBlocks for the entire function
Misha Brukmanabb027c2003-05-27 21:40:39 +000096 for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
Misha Brukmanf86d6352003-05-31 06:26:48 +000097 long Location = BBLocations[BBRefs[i].first];
Misha Brukmanabb027c2003-05-27 21:40:39 +000098 unsigned *Ref = BBRefs[i].second.first;
99 MachineInstr *MI = BBRefs[i].second.second;
Misha Brukman417a7c02003-05-30 20:39:37 +0000100 std::cerr << "attempting to resolve BB: " << i << "\n";
Brian Gaeke63b99f92003-06-01 22:08:29 +0000101 for (unsigned ii = 0, ee = MI->getNumOperands(); ii != ee; ++ii) {
102 MachineOperand &op = MI->getOperand(ii);
Misha Brukman417a7c02003-05-30 20:39:37 +0000103 if (op.isPCRelativeDisp()) {
Misha Brukmanf86d6352003-05-31 06:26:48 +0000104 // the instruction's branch target is made such that it branches to
105 // PC + (br target * 4), so undo that arithmetic here:
106 // Location is the target of the branch
107 // Ref is the location of the instruction, and hence the PC
108 unsigned branchTarget = (Location - (long)Ref) >> 2;
Brian Gaeke63b99f92003-06-01 22:08:29 +0000109 MI->SetMachineOperandConst(ii, MachineOperand::MO_SignExtendedImmed,
Misha Brukmanf86d6352003-05-31 06:26:48 +0000110 branchTarget);
Brian Gaeke63b99f92003-06-01 22:08:29 +0000111 // Copy the flags.
112 if (op.opLoBits32()) { MI->setOperandLo32(ii); }
113 else if (op.opHiBits32()) { MI->setOperandHi32(ii); }
114 else if (op.opLoBits64()) { MI->setOperandLo64(ii); }
115 else if (op.opHiBits64()) { MI->setOperandHi64(ii); }
Misha Brukman417a7c02003-05-30 20:39:37 +0000116 std::cerr << "Rewrote BB ref: ";
117 unsigned fixedInstr = SparcV9CodeEmitter::getBinaryCodeForInstr(*MI);
118 *Ref = fixedInstr;
Misha Brukmanabb027c2003-05-27 21:40:39 +0000119 break;
120 }
121 }
Misha Brukmanabb027c2003-05-27 21:40:39 +0000122 }
123 BBRefs.clear();
124 BBLocations.clear();
125
126 NumBytes += CurByte-CurBlock;
127
128 DEBUG(std::cerr << "Finished CodeGen of [0x" << std::hex
129 << (unsigned)(intptr_t)CurBlock
130 << std::dec << "] Function: " << F.getFunction()->getName()
131 << ": " << CurByte-CurBlock << " bytes of text\n");
132}
133
134void SparcEmitter::emitConstantPool(MachineConstantPool *MCP) {
135 const std::vector<Constant*> &Constants = MCP->getConstants();
136 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
137 // For now we just allocate some memory on the heap, this can be
138 // dramatically improved.
139 const Type *Ty = ((Value*)Constants[i])->getType();
140 void *Addr = malloc(TheVM.getTargetData().getTypeSize(Ty));
141 TheVM.InitializeMemory(Constants[i], Addr);
142 ConstantPoolAddresses.push_back(Addr);
143 }
144}
145
146
147void SparcEmitter::startBasicBlock(MachineBasicBlock &BB) {
Misha Brukmanf86d6352003-05-31 06:26:48 +0000148 BBLocations[BB.getBasicBlock()] = (long)(intptr_t)CurByte;
Misha Brukmanabb027c2003-05-27 21:40:39 +0000149}
150
151
152void SparcEmitter::startFunctionStub(const Function &F, unsigned StubSize) {
153 SavedCurBlock = CurBlock; SavedCurByte = CurByte;
154 // FIXME: this is a huge waste of memory.
155 CurBlock = (unsigned char *)getMemory((StubSize+4095)/4096);
156 CurByte = CurBlock; // Start writing at the beginning of the fn.
157}
158
159void *SparcEmitter::finishFunctionStub(const Function &F) {
160 NumBytes += CurByte-CurBlock;
161 DEBUG(std::cerr << "Finished CodeGen of [0x" << std::hex
162 << (unsigned)(intptr_t)CurBlock
163 << std::dec << "] Function stub for: " << F.getName()
164 << ": " << CurByte-CurBlock << " bytes of text\n");
165 std::swap(CurBlock, SavedCurBlock);
166 CurByte = SavedCurByte;
167 return SavedCurBlock;
168}
169
170void SparcEmitter::emitByte(unsigned char B) {
171 *CurByte++ = B; // Write the byte to memory
172}
173
174// BasicBlock -> pair<memloc, MachineInstr>
175// when the BB is emitted, machineinstr is modified with then-currbyte,
176// processed with MCE, and written out at memloc.
Misha Brukman8e5bf702003-05-28 18:44:38 +0000177// Should be called by the emitter if its outputting a PCRelative disp
Misha Brukmanabb027c2003-05-27 21:40:39 +0000178void SparcEmitter::saveBBreference(BasicBlock *BB, MachineInstr &MI) {
179 BBRefs.push_back(std::make_pair(BB, std::make_pair((unsigned*)CurByte, &MI)));
180}
181
182
183// emitPCRelativeDisp - For functions, just output a displacement that will
184// cause a reference to the zero page, which will cause a seg-fault, causing
185// things to get resolved on demand. Keep track of these markers.
186//
187// For basic block references, keep track of where the references are so they
188// may be patched up when the basic block is defined.
189//
190// BasicBlock -> pair<memloc, MachineInstr>
191// when the BB is emitted, machineinstr is modified with then-currbyte,
192// processed with MCE, and written out at memloc.
193
194void SparcEmitter::emitPCRelativeDisp(Value *V) {
195#if 0
196 BasicBlock *BB = cast<BasicBlock>(V); // Keep track of reference...
197 BBRefs.push_back(std::make_pair(BB, (unsigned*)CurByte));
198 CurByte += 4;
199#endif
200}
201
202// emitAddress - Emit an address in either direct or PCRelative form...
203//
204void SparcEmitter::emitAddress(void *Addr, bool isPCRelative) {
205#if 0
206 if (isPCRelative) {
207 *(intptr_t*)CurByte = (intptr_t)Addr - (intptr_t)CurByte-4;
208 } else {
209 *(void**)CurByte = Addr;
210 }
211 CurByte += 4;
212#endif
213}
214
215void SparcEmitter::emitGlobalAddress(GlobalValue *V, bool isPCRelative) {
216 if (isPCRelative) { // must be a call, this is a major hack!
217 // Try looking up the function to see if it is already compiled!
218 if (void *Addr = TheVM.getPointerToGlobalIfAvailable(V)) {
219 emitAddress(Addr, isPCRelative);
220 } else { // Function has not yet been code generated!
221 TheVM.addFunctionRef(CurByte, cast<Function>(V));
222
223 // Delayed resolution...
224 emitAddress((void*)VM::CompilationCallback, isPCRelative);
225 }
226 } else {
227 emitAddress(TheVM.getPointerToGlobal(V), isPCRelative);
228 }
229}
230
231void SparcEmitter::emitGlobalAddress(const std::string &Name, bool isPCRelative)
232{
233#if 0
234 emitAddress(TheVM.getPointerToNamedFunction(Name), isPCRelative);
235#endif
236}
237
238void SparcEmitter::emitFunctionConstantValueAddress(unsigned ConstantNum,
239 int Offset) {
240 assert(ConstantNum < ConstantPoolAddresses.size() &&
241 "Invalid ConstantPoolIndex!");
242 *(void**)CurByte = (char*)ConstantPoolAddresses[ConstantNum]+Offset;
243 CurByte += 4;
244}