#include "llvm/BinaryFormat/ELF.h"
#include "llvm/CodeGen/AsmPrinter.h"
#include "llvm/CodeGen/GCMetadata.h"
#include "llvm/CodeGen/GCMetadataPrinter.h"
#include "llvm/CodeGen/GCStrategy.h"
#include "llvm/CodeGen/GCs.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Module.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCSectionELF.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/Target/TargetLoweringObjectFile.h"
using namespace llvm;
namespace {
class ErlangGCPrinter : public GCMetadataPrinter {
public:
void finishAssembly(Module &M, GCModuleInfo &Info, AsmPrinter &AP) override;
};
}
static GCMetadataPrinterRegistry::Add<ErlangGCPrinter>
X("erlang", "erlang-compatible garbage collector");
void ErlangGCPrinter::finishAssembly(Module &M, GCModuleInfo &Info,
AsmPrinter &AP) {
MCStreamer &OS = *AP.OutStreamer;
unsigned IntPtrSize = M.getDataLayout().getPointerSize();
OS.SwitchSection(
AP.getObjFileLowering().getContext().getELFSection(".note.gc",
ELF::SHT_PROGBITS, 0));
for (GCModuleInfo::FuncInfoVec::iterator FI = Info.funcinfo_begin(),
IE = Info.funcinfo_end();
FI != IE; ++FI) {
GCFunctionInfo &MD = **FI;
if (MD.getStrategy().getName() != getStrategy().getName())
continue;
*
* struct {
* int16_t PointCount;
* void *SafePointAddress[PointCount];
* int16_t StackFrameSize; (in words)
* int16_t StackArity;
* int16_t LiveCount;
* int16_t LiveOffsets[LiveCount];
* } __gcmap_<FUNCTIONNAME>;
**/
AP.EmitAlignment(IntPtrSize == 4 ? 2 : 3);
OS.AddComment("safe point count");
AP.emitInt16(MD.size());
for (GCFunctionInfo::iterator PI = MD.begin(), PE = MD.end(); PI != PE;
++PI) {
OS.AddComment("safe point address");
MCSymbol *Label = PI->Label;
AP.EmitLabelPlusOffset(Label , 0 , 4 );
}
GCFunctionInfo::iterator PI = MD.begin();
OS.AddComment("stack frame size (in words)");
AP.emitInt16(MD.getFrameSize() / IntPtrSize);
unsigned RegisteredArgs = IntPtrSize == 4 ? 5 : 6;
unsigned StackArity = MD.getFunction().arg_size() > RegisteredArgs
? MD.getFunction().arg_size() - RegisteredArgs
: 0;
OS.AddComment("stack arity");
AP.emitInt16(StackArity);
OS.AddComment("live root count");
AP.emitInt16(MD.live_size(PI));
for (GCFunctionInfo::live_iterator LI = MD.live_begin(PI),
LE = MD.live_end(PI);
LI != LE; ++LI) {
OS.AddComment("stack index (offset / wordsize)");
AP.emitInt16(LI->StackOffset / IntPtrSize);
}
}
}
void llvm::linkErlangGCPrinter() {}