1 //===-- SparcEmitter.cpp - Write machine code to executable memory --------===//
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.
6 //===----------------------------------------------------------------------===//
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"
17 #include "../../../lib/Target/Sparc/SparcV9CodeEmitter.h"
20 Statistic<> NumBytes("jello", "Number of bytes of machine code compiled");
22 class SparcEmitter : public MachineCodeEmitter {
25 unsigned char *CurBlock, *CurByte;
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;
31 std::vector<std::pair<BasicBlock*,
32 std::pair<unsigned*,MachineInstr*> > > BBRefs;
33 std::map<BasicBlock*, unsigned> BBLocations;
34 std::vector<void*> ConstantPoolAddresses;
35 std::vector<void*> funcMemory;
37 SparcEmitter(VM &vm) : TheVM(vm) {}
39 while (! funcMemory.empty()) {
40 void* addr = funcMemory.back();
42 funcMemory.pop_back();
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,
59 virtual void saveBBreference(BasicBlock *BB, MachineInstr &MI);
63 void emitAddress(void *Addr, bool isPCRelative);
64 void* getMemory(unsigned NumPages);
68 MachineCodeEmitter *VM::createSparcEmitter(VM &V) {
69 return new SparcEmitter(V);
73 #define _POSIX_MAPPED_FILES
77 // FIXME: This should be rewritten to support a real memory manager for
78 // executable memory pages!
79 void * SparcEmitter::getMemory(unsigned NumPages) {
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);
85 if (pa == MAP_FAILED) {
93 void SparcEmitter::startFunction(MachineFunction &F) {
94 CurBlock = (unsigned char *)getMemory(8);
95 std::cerr << "Starting function " << F.getFunction()->getName() << "\n";
96 CurByte = CurBlock; // Start writing at the beginning of the fn.
97 TheVM.addGlobalMapping(F.getFunction(), CurBlock);
100 void SparcEmitter::finishFunction(MachineFunction &F) {
101 ConstantPoolAddresses.clear();
102 // Re-write branches to BasicBlocks for the entire function
103 for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
104 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);
114 unsigned fixedInstr = SparcV9CodeEmitter::getBinaryCodeForInstr(*MI);
120 NumBytes += CurByte-CurBlock;
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");
128 void 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);
141 void SparcEmitter::startBasicBlock(MachineBasicBlock &BB) {
142 BBLocations[BB.getBasicBlock()] = (unsigned)(intptr_t)CurByte;
146 void 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.
153 void *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;
164 void SparcEmitter::emitByte(unsigned char B) {
165 *CurByte++ = B; // Write the byte to memory
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.
171 // Should be called by the emitter if its outputting a PCRelative disp
172 void SparcEmitter::saveBBreference(BasicBlock *BB, MachineInstr &MI) {
173 BBRefs.push_back(std::make_pair(BB, std::make_pair((unsigned*)CurByte, &MI)));
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.
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.
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.
188 void SparcEmitter::emitPCRelativeDisp(Value *V) {
190 BasicBlock *BB = cast<BasicBlock>(V); // Keep track of reference...
191 BBRefs.push_back(std::make_pair(BB, (unsigned*)CurByte));
196 // emitAddress - Emit an address in either direct or PCRelative form...
198 void SparcEmitter::emitAddress(void *Addr, bool isPCRelative) {
201 *(intptr_t*)CurByte = (intptr_t)Addr - (intptr_t)CurByte-4;
203 *(void**)CurByte = Addr;
209 void 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));
217 // Delayed resolution...
218 emitAddress((void*)VM::CompilationCallback, isPCRelative);
221 emitAddress(TheVM.getPointerToGlobal(V), isPCRelative);
225 void SparcEmitter::emitGlobalAddress(const std::string &Name, bool isPCRelative)
228 emitAddress(TheVM.getPointerToNamedFunction(Name), isPCRelative);
232 void SparcEmitter::emitFunctionConstantValueAddress(unsigned ConstantNum,
234 assert(ConstantNum < ConstantPoolAddresses.size() &&
235 "Invalid ConstantPoolIndex!");
236 *(void**)CurByte = (char*)ConstantPoolAddresses[ConstantNum]+Offset;