/// about to be code generated. This initializes the BufferBegin/End/Ptr
/// fields.
///
- virtual void startFunction(MachineFunction &F) {}
+ virtual void startFunction(MachineFunction &F) = 0;
/// finishFunction - This callback is invoked when the specified function has
/// finished code generation. If a buffer overflow has occurred, this method
/// returns true (the callee is required to try again), otherwise it returns
/// false.
///
- virtual bool finishFunction(MachineFunction &F) {
- return CurBufferPtr == BufferEnd;
- }
-
- /// emitConstantPool - This callback is invoked to output the constant pool
- /// for the function.
- virtual void emitConstantPool(MachineConstantPool *MCP) {}
-
- /// initJumpTableInfo - This callback is invoked by the JIT to allocate the
- /// necessary memory to hold the jump tables.
- virtual void initJumpTableInfo(MachineJumpTableInfo *MJTI) {}
+ virtual bool finishFunction(MachineFunction &F) = 0;
/// emitJumpTableInfo - This callback is invoked to output the jump tables
/// for the function. In addition to a pointer to the MachineJumpTableInfo,
/// this function also takes a map of MBBs to addresses, so that the final
/// addresses of the MBBs can be written to the jump tables.
virtual void emitJumpTableInfo(MachineJumpTableInfo *MJTI,
- std::map<MachineBasicBlock*,uint64_t> &MBBM) {}
+ std::map<MachineBasicBlock*,uint64_t> &MBBM) = 0;
/// startFunctionStub - This callback is invoked when the JIT needs the
/// address of a function that has not been code generated yet. The StubSize
/// have constant pools, the can only use the other emitByte*/emitWord*
/// methods.
///
- virtual void startFunctionStub(unsigned StubSize) {}
+ virtual void startFunctionStub(unsigned StubSize) = 0;
/// finishFunctionStub - This callback is invoked to terminate a function
/// stub.
///
- virtual void *finishFunctionStub(const Function *F) { return 0; }
+ virtual void *finishFunctionStub(const Function *F) = 0;
/// emitByte - This callback is invoked when a byte needs to be written to the
/// output stream.
class JITMemoryManager {
std::list<sys::MemoryBlock> Blocks; // List of blocks allocated by the JIT
unsigned char *FunctionBase; // Start of the function body area
- unsigned char *ConstantBase; // Memory allocated for constant pools
- unsigned char *CurStubPtr, *CurFunctionPtr, *CurConstantPtr;
+ unsigned char *CurStubPtr, *CurFunctionPtr;
unsigned char *GOTBase; // Target Specific reserved memory
// centralize memory block allocation
~JITMemoryManager();
inline unsigned char *allocateStub(unsigned StubSize);
- inline unsigned char *allocateConstant(unsigned ConstantSize,
- unsigned Alignment);
inline unsigned char *startFunctionBody();
inline void endFunctionBody(unsigned char *FunctionEnd);
JITMemoryManager::JITMemoryManager(bool useGOT) {
// Allocate a 16M block of memory for functions
sys::MemoryBlock FunBlock = getNewMemoryBlock(16 << 20);
- // Allocate a 1M block of memory for Constants
- sys::MemoryBlock ConstBlock = getNewMemoryBlock(1 << 20);
Blocks.push_front(FunBlock);
- Blocks.push_front(ConstBlock);
FunctionBase = reinterpret_cast<unsigned char*>(FunBlock.base());
- ConstantBase = reinterpret_cast<unsigned char*>(ConstBlock.base());
// Allocate stubs backwards from the base, allocate functions forward
// from the base.
CurStubPtr = CurFunctionPtr = FunctionBase + 512*1024;// Use 512k for stubs
- CurConstantPtr = ConstantBase + ConstBlock.size();
-
// Allocate the GOT.
GOTBase = NULL;
if (useGOT) GOTBase = (unsigned char*)malloc(sizeof(void*) * 8192);
return CurStubPtr;
}
-unsigned char *JITMemoryManager::allocateConstant(unsigned ConstantSize,
- unsigned Alignment) {
- // Reserve space and align pointer.
- CurConstantPtr -= ConstantSize;
- CurConstantPtr =
- (unsigned char *)((intptr_t)CurConstantPtr & ~((intptr_t)Alignment - 1));
-
- if (CurConstantPtr < ConstantBase) {
- //Either allocate another MB or 2xConstantSize
- sys::MemoryBlock ConstBlock = getNewMemoryBlock(2 * ConstantSize);
- ConstantBase = reinterpret_cast<unsigned char*>(ConstBlock.base());
- CurConstantPtr = ConstantBase + ConstBlock.size();
- return allocateConstant(ConstantSize, Alignment);
- }
- return CurConstantPtr;
-}
-
unsigned char *JITMemoryManager::startFunctionBody() {
// Round up to an even multiple of 8 bytes, this should eventually be target
// specific.
virtual void startFunction(MachineFunction &F);
virtual bool finishFunction(MachineFunction &F);
- virtual void emitConstantPool(MachineConstantPool *MCP);
- virtual void initJumpTableInfo(MachineJumpTableInfo *MJTI);
+
+ void emitConstantPool(MachineConstantPool *MCP);
+ void initJumpTableInfo(MachineJumpTableInfo *MJTI);
virtual void emitJumpTableInfo(MachineJumpTableInfo *MJTI,
std::map<MachineBasicBlock*,uint64_t> &MBBM);
+
virtual void startFunctionStub(unsigned StubSize);
virtual void* finishFunctionStub(const Function *F);
void JITEmitter::startFunction(MachineFunction &F) {
BufferBegin = CurBufferPtr = MemMgr.startFunctionBody();
- TheJIT->updateGlobalMapping(F.getFunction(), BufferBegin);
/// FIXME: implement out of space handling correctly!
BufferEnd = (unsigned char*)(intptr_t)~0ULL;
+
+ emitConstantPool(F.getConstantPool());
+ initJumpTableInfo(F.getJumpTableInfo());
+
+ // About to start emitting the machine code for the function.
+ // FIXME: align it?
+ TheJIT->updateGlobalMapping(F.getFunction(), CurBufferPtr);
}
bool JITEmitter::finishFunction(MachineFunction &F) {
unsigned Size = Constants.back().Offset;
Size += TheJIT->getTargetData().getTypeSize(Constants.back().Val->getType());
- ConstantPoolBase = MemMgr.allocateConstant(Size,
- 1 << MCP->getConstantPoolAlignment());
+ ConstantPoolBase = allocateSpace(Size, 1 << MCP->getConstantPoolAlignment());
ConstantPool = MCP;
-
+
+ if (ConstantPoolBase == 0) return; // Buffer overflow.
+
// Initialize the memory for all of the constant pool entries.
for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
void *CAddr = (char*)ConstantPoolBase+Constants[i].Offset;
const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
if (JT.empty()) return;
- unsigned Size = 0;
- unsigned EntrySize = MJTI->getEntrySize();
+ unsigned NumEntries = 0;
for (unsigned i = 0, e = JT.size(); i != e; ++i)
- Size += JT[i].MBBs.size() * EntrySize;
-
+ NumEntries += JT[i].MBBs.size();
+
+ unsigned EntrySize = MJTI->getEntrySize();
+
// Just allocate space for all the jump tables now. We will fix up the actual
// MBB entries in the tables after we emit the code for each block, since then
// we will know the final locations of the MBBs in memory.
JumpTable = MJTI;
- JumpTableBase = MemMgr.allocateConstant(Size, MJTI->getAlignment());
+ JumpTableBase = allocateSpace(NumEntries * EntrySize, MJTI->getAlignment());
}
void JITEmitter::emitJumpTableInfo(MachineJumpTableInfo *MJTI,
std::map<MachineBasicBlock*,uint64_t> &MBBM){
const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
- if (JT.empty()) return;
+ if (JT.empty() || JumpTableBase == 0) return;
unsigned Offset = 0;
unsigned EntrySize = MJTI->getEntrySize();