ErlangGCPrinter.cpp [plain text]
#include "llvm/CodeGen/AsmPrinter.h"
#include "llvm/CodeGen/GCMetadataPrinter.h"
#include "llvm/CodeGen/GCs.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Metadata.h"
#include "llvm/MC/MCAsmInfo.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"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetSubtargetInfo.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 llvm::linkErlangGCPrinter() {}
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;
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);
}
}
}