2009-06-03 03:43:31 +00:00
|
|
|
//===-- MachOEmitter.cpp - Target-independent Mach-O Emitter code --------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-07-06 06:40:51 +00:00
|
|
|
#include "MachO.h"
|
|
|
|
#include "MachOWriter.h"
|
2009-06-03 03:43:31 +00:00
|
|
|
#include "MachOCodeEmitter.h"
|
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/CodeGen/MachineConstantPool.h"
|
2009-08-19 22:08:26 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2009-06-03 03:43:31 +00:00
|
|
|
#include "llvm/CodeGen/MachineJumpTableInfo.h"
|
2009-07-06 06:40:51 +00:00
|
|
|
#include "llvm/CodeGen/MachineRelocation.h"
|
2009-08-22 20:48:53 +00:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2009-07-06 06:40:51 +00:00
|
|
|
#include "llvm/Target/TargetData.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2009-07-11 20:10:48 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2009-06-03 03:43:31 +00:00
|
|
|
#include "llvm/Support/Mangler.h"
|
|
|
|
#include "llvm/Support/OutputBuffer.h"
|
2009-07-06 05:09:34 +00:00
|
|
|
#include <vector>
|
2009-06-03 03:43:31 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MachOCodeEmitter Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace llvm {
|
2009-07-06 06:40:51 +00:00
|
|
|
|
|
|
|
MachOCodeEmitter::MachOCodeEmitter(MachOWriter &mow, MachOSection &mos) :
|
|
|
|
ObjectCodeEmitter(&mos), MOW(mow), TM(MOW.TM) {
|
|
|
|
is64Bit = TM.getTargetData()->getPointerSizeInBits() == 64;
|
|
|
|
isLittleEndian = TM.getTargetData()->isLittleEndian();
|
2009-08-22 21:43:10 +00:00
|
|
|
MAI = TM.getMCAsmInfo();
|
2009-07-06 06:40:51 +00:00
|
|
|
}
|
|
|
|
|
2009-06-03 03:43:31 +00:00
|
|
|
/// startFunction - This callback is invoked when a new machine function is
|
|
|
|
/// about to be emitted.
|
|
|
|
|
|
|
|
void MachOCodeEmitter::startFunction(MachineFunction &MF) {
|
|
|
|
const TargetData *TD = TM.getTargetData();
|
|
|
|
const Function *F = MF.getFunction();
|
|
|
|
|
|
|
|
// Align the output buffer to the appropriate alignment, power of 2.
|
|
|
|
unsigned FnAlign = F->getAlignment();
|
|
|
|
unsigned TDAlign = TD->getPrefTypeAlignment(F->getType());
|
|
|
|
unsigned Align = Log2_32(std::max(FnAlign, TDAlign));
|
|
|
|
assert(!(Align & (Align-1)) && "Alignment is not a power of two!");
|
|
|
|
|
|
|
|
// Get the Mach-O Section that this function belongs in.
|
|
|
|
MachOSection *MOS = MOW.getTextSection();
|
|
|
|
|
|
|
|
// Upgrade the section alignment if required.
|
|
|
|
if (MOS->align < Align) MOS->align = Align;
|
|
|
|
|
2009-07-06 05:09:34 +00:00
|
|
|
MOS->emitAlignment(Align);
|
2009-06-03 03:43:31 +00:00
|
|
|
|
2009-07-06 05:09:34 +00:00
|
|
|
// Create symbol for function entry
|
|
|
|
const GlobalValue *FuncV = MF.getFunction();
|
2009-08-22 21:43:10 +00:00
|
|
|
MachOSym FnSym(FuncV, MOW.Mang->getMangledName(FuncV), MOS->Index, MAI);
|
2009-07-06 05:09:34 +00:00
|
|
|
FnSym.n_value = getCurrentPCOffset();
|
|
|
|
|
|
|
|
// add it to the symtab.
|
|
|
|
MOW.SymbolTable.push_back(FnSym);
|
2009-06-03 03:43:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// finishFunction - This callback is invoked after the function is completely
|
|
|
|
/// finished.
|
|
|
|
|
|
|
|
bool MachOCodeEmitter::finishFunction(MachineFunction &MF) {
|
|
|
|
|
|
|
|
// Get the Mach-O Section that this function belongs in.
|
|
|
|
MachOSection *MOS = MOW.getTextSection();
|
|
|
|
|
|
|
|
// Emit constant pool to appropriate section(s)
|
|
|
|
emitConstantPool(MF.getConstantPool());
|
|
|
|
|
|
|
|
// Emit jump tables to appropriate section
|
|
|
|
emitJumpTables(MF.getJumpTableInfo());
|
|
|
|
|
|
|
|
// If we have emitted any relocations to function-specific objects such as
|
|
|
|
// basic blocks, constant pools entries, or jump tables, record their
|
|
|
|
// addresses now so that we can rewrite them with the correct addresses
|
|
|
|
// later.
|
|
|
|
for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
|
|
|
|
MachineRelocation &MR = Relocations[i];
|
|
|
|
intptr_t Addr;
|
|
|
|
|
|
|
|
if (MR.isBasicBlock()) {
|
|
|
|
Addr = getMachineBasicBlockAddress(MR.getBasicBlock());
|
|
|
|
MR.setConstantVal(MOS->Index);
|
|
|
|
MR.setResultPointer((void*)Addr);
|
|
|
|
} else if (MR.isJumpTableIndex()) {
|
|
|
|
Addr = getJumpTableEntryAddress(MR.getJumpTableIndex());
|
|
|
|
MR.setConstantVal(MOW.getJumpTableSection()->Index);
|
|
|
|
MR.setResultPointer((void*)Addr);
|
|
|
|
} else if (MR.isConstantPoolIndex()) {
|
|
|
|
Addr = getConstantPoolEntryAddress(MR.getConstantPoolIndex());
|
|
|
|
MR.setConstantVal(CPSections[MR.getConstantPoolIndex()]);
|
|
|
|
MR.setResultPointer((void*)Addr);
|
|
|
|
} else if (MR.isGlobalValue()) {
|
|
|
|
// FIXME: This should be a set or something that uniques
|
|
|
|
MOW.PendingGlobals.push_back(MR.getGlobalValue());
|
|
|
|
} else {
|
2009-07-14 16:55:14 +00:00
|
|
|
llvm_unreachable("Unhandled relocation type");
|
2009-06-03 03:43:31 +00:00
|
|
|
}
|
2009-07-06 05:09:34 +00:00
|
|
|
MOS->addRelocation(MR);
|
2009-06-03 03:43:31 +00:00
|
|
|
}
|
|
|
|
Relocations.clear();
|
|
|
|
|
|
|
|
// Clear per-function data structures.
|
|
|
|
CPLocations.clear();
|
|
|
|
CPSections.clear();
|
|
|
|
JTLocations.clear();
|
|
|
|
MBBLocations.clear();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// emitConstantPool - For each constant pool entry, figure out which section
|
|
|
|
/// the constant should live in, allocate space for it, and emit it to the
|
|
|
|
/// Section data buffer.
|
|
|
|
void MachOCodeEmitter::emitConstantPool(MachineConstantPool *MCP) {
|
|
|
|
const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
|
|
|
|
if (CP.empty()) return;
|
|
|
|
|
|
|
|
// FIXME: handle PIC codegen
|
|
|
|
assert(TM.getRelocationModel() != Reloc::PIC_ &&
|
|
|
|
"PIC codegen not yet handled for mach-o jump tables!");
|
|
|
|
|
|
|
|
// Although there is no strict necessity that I am aware of, we will do what
|
|
|
|
// gcc for OS X does and put each constant pool entry in a section of constant
|
|
|
|
// objects of a certain size. That means that float constants go in the
|
|
|
|
// literal4 section, and double objects go in literal8, etc.
|
|
|
|
//
|
|
|
|
// FIXME: revisit this decision if we ever do the "stick everything into one
|
|
|
|
// "giant object for PIC" optimization.
|
|
|
|
for (unsigned i = 0, e = CP.size(); i != e; ++i) {
|
|
|
|
const Type *Ty = CP[i].getType();
|
|
|
|
unsigned Size = TM.getTargetData()->getTypeAllocSize(Ty);
|
|
|
|
|
|
|
|
MachOSection *Sec = MOW.getConstSection(CP[i].Val.ConstVal);
|
2009-07-06 05:09:34 +00:00
|
|
|
OutputBuffer SecDataOut(Sec->getData(), is64Bit, isLittleEndian);
|
2009-06-03 03:43:31 +00:00
|
|
|
|
2009-07-06 05:09:34 +00:00
|
|
|
CPLocations.push_back(Sec->size());
|
2009-06-03 03:43:31 +00:00
|
|
|
CPSections.push_back(Sec->Index);
|
|
|
|
|
|
|
|
// Allocate space in the section for the global.
|
|
|
|
// FIXME: need alignment?
|
|
|
|
// FIXME: share between here and AddSymbolToSection?
|
|
|
|
for (unsigned j = 0; j < Size; ++j)
|
|
|
|
SecDataOut.outbyte(0);
|
|
|
|
|
2009-07-06 06:40:51 +00:00
|
|
|
MachOWriter::InitMem(CP[i].Val.ConstVal, CPLocations[i],
|
|
|
|
TM.getTargetData(), Sec);
|
2009-06-03 03:43:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// emitJumpTables - Emit all the jump tables for a given jump table info
|
|
|
|
/// record to the appropriate section.
|
|
|
|
void MachOCodeEmitter::emitJumpTables(MachineJumpTableInfo *MJTI) {
|
|
|
|
const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
|
|
|
|
if (JT.empty()) return;
|
|
|
|
|
|
|
|
// FIXME: handle PIC codegen
|
|
|
|
assert(TM.getRelocationModel() != Reloc::PIC_ &&
|
|
|
|
"PIC codegen not yet handled for mach-o jump tables!");
|
|
|
|
|
|
|
|
MachOSection *Sec = MOW.getJumpTableSection();
|
|
|
|
unsigned TextSecIndex = MOW.getTextSection()->Index;
|
2009-07-06 05:09:34 +00:00
|
|
|
OutputBuffer SecDataOut(Sec->getData(), is64Bit, isLittleEndian);
|
2009-06-03 03:43:31 +00:00
|
|
|
|
|
|
|
for (unsigned i = 0, e = JT.size(); i != e; ++i) {
|
|
|
|
// For each jump table, record its offset from the start of the section,
|
|
|
|
// reserve space for the relocations to the MBBs, and add the relocations.
|
|
|
|
const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs;
|
2009-07-06 05:09:34 +00:00
|
|
|
JTLocations.push_back(Sec->size());
|
2009-06-03 03:43:31 +00:00
|
|
|
for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) {
|
2009-07-06 05:09:34 +00:00
|
|
|
MachineRelocation MR(MOW.GetJTRelocation(Sec->size(), MBBs[mi]));
|
2009-06-03 03:43:31 +00:00
|
|
|
MR.setResultPointer((void *)JTLocations[i]);
|
|
|
|
MR.setConstantVal(TextSecIndex);
|
2009-07-06 05:09:34 +00:00
|
|
|
Sec->addRelocation(MR);
|
2009-06-03 03:43:31 +00:00
|
|
|
SecDataOut.outaddr(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace llvm
|
|
|
|
|