2013-03-25 13:47:46 +00:00
|
|
|
//===-- ErlangGCPrinter.cpp - Erlang/OTP frametable emitter -----*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the compiler plugin that is used in order to emit
|
|
|
|
// garbage collection information in a convenient layout for parsing and
|
|
|
|
// loading in the Erlang/OTP runtime.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/CodeGen/AsmPrinter.h"
|
|
|
|
#include "llvm/CodeGen/GCMetadataPrinter.h"
|
2014-01-07 11:48:04 +00:00
|
|
|
#include "llvm/CodeGen/GCs.h"
|
2013-03-25 13:47:46 +00:00
|
|
|
#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"
|
2014-08-04 21:25:23 +00:00
|
|
|
#include "llvm/Target/TargetSubtargetInfo.h"
|
2013-03-25 13:47:46 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class ErlangGCPrinter : public GCMetadataPrinter {
|
|
|
|
public:
|
Remove the Module pointer from GCStrategy and GCMetadataPrinter
In the current implementation, GCStrategy is a part of the ownership structure for the gc metadata which describes a Module. It also contains a reference to the module in question. As a result, GCStrategy instances are essentially Module specific.
I plan to transition away from this design. Instead, a GCStrategy will be owned by the LLVMContext. It will be a lightweight policy object which contains no information about the Modules or Functions involved, but can be easily reached given a Function.
The first step in this transition is to remove the direct Module reference from GCStrategy. This also requires removing the single user of this reference, the GCMetadataPrinter hierarchy. In theory, this will allow the lifetime of the printers to be scoped to the LLVMContext as well, but in practice, I'm not actually changing that. (Yet?)
An alternate design would have been to move the direct Module reference into the GCMetadataPrinter and change the keying of the owning maps to explicitly key off both GCStrategy and Module. I'm open to doing it that way instead, but didn't see much value in preserving the per Module association for GCMetadataPrinters.
The next change in this sequence will be to start unwinding the intertwined ownership between GCStrategy, GCModuleInfo, and GCFunctionInfo.
Differential Revision: http://reviews.llvm.org/D6566
llvm-svn: 223859
2014-12-09 23:57:54 +00:00
|
|
|
void beginAssembly(Module &M, AsmPrinter &AP) override;
|
|
|
|
void finishAssembly(Module &M, AsmPrinter &AP) override;
|
2013-03-25 13:47:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static GCMetadataPrinterRegistry::Add<ErlangGCPrinter>
|
|
|
|
X("erlang", "erlang-compatible garbage collector");
|
|
|
|
|
|
|
|
void llvm::linkErlangGCPrinter() { }
|
|
|
|
|
Remove the Module pointer from GCStrategy and GCMetadataPrinter
In the current implementation, GCStrategy is a part of the ownership structure for the gc metadata which describes a Module. It also contains a reference to the module in question. As a result, GCStrategy instances are essentially Module specific.
I plan to transition away from this design. Instead, a GCStrategy will be owned by the LLVMContext. It will be a lightweight policy object which contains no information about the Modules or Functions involved, but can be easily reached given a Function.
The first step in this transition is to remove the direct Module reference from GCStrategy. This also requires removing the single user of this reference, the GCMetadataPrinter hierarchy. In theory, this will allow the lifetime of the printers to be scoped to the LLVMContext as well, but in practice, I'm not actually changing that. (Yet?)
An alternate design would have been to move the direct Module reference into the GCMetadataPrinter and change the keying of the owning maps to explicitly key off both GCStrategy and Module. I'm open to doing it that way instead, but didn't see much value in preserving the per Module association for GCMetadataPrinters.
The next change in this sequence will be to start unwinding the intertwined ownership between GCStrategy, GCModuleInfo, and GCFunctionInfo.
Differential Revision: http://reviews.llvm.org/D6566
llvm-svn: 223859
2014-12-09 23:57:54 +00:00
|
|
|
void ErlangGCPrinter::beginAssembly(Module &M, AsmPrinter &AP) { }
|
2013-03-25 13:47:46 +00:00
|
|
|
|
Remove the Module pointer from GCStrategy and GCMetadataPrinter
In the current implementation, GCStrategy is a part of the ownership structure for the gc metadata which describes a Module. It also contains a reference to the module in question. As a result, GCStrategy instances are essentially Module specific.
I plan to transition away from this design. Instead, a GCStrategy will be owned by the LLVMContext. It will be a lightweight policy object which contains no information about the Modules or Functions involved, but can be easily reached given a Function.
The first step in this transition is to remove the direct Module reference from GCStrategy. This also requires removing the single user of this reference, the GCMetadataPrinter hierarchy. In theory, this will allow the lifetime of the printers to be scoped to the LLVMContext as well, but in practice, I'm not actually changing that. (Yet?)
An alternate design would have been to move the direct Module reference into the GCMetadataPrinter and change the keying of the owning maps to explicitly key off both GCStrategy and Module. I'm open to doing it that way instead, but didn't see much value in preserving the per Module association for GCMetadataPrinters.
The next change in this sequence will be to start unwinding the intertwined ownership between GCStrategy, GCModuleInfo, and GCFunctionInfo.
Differential Revision: http://reviews.llvm.org/D6566
llvm-svn: 223859
2014-12-09 23:57:54 +00:00
|
|
|
void ErlangGCPrinter::finishAssembly(Module &M, AsmPrinter &AP) {
|
2013-03-25 13:47:46 +00:00
|
|
|
MCStreamer &OS = AP.OutStreamer;
|
2014-08-04 21:25:23 +00:00
|
|
|
unsigned IntPtrSize =
|
|
|
|
AP.TM.getSubtargetImpl()->getDataLayout()->getPointerSize();
|
2013-03-25 13:47:46 +00:00
|
|
|
|
|
|
|
// Put this in a custom .note section.
|
|
|
|
AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getContext()
|
|
|
|
.getELFSection(".note.gc", ELF::SHT_PROGBITS, 0,
|
|
|
|
SectionKind::getDataRel()));
|
|
|
|
|
|
|
|
// For each function...
|
|
|
|
for (iterator FI = begin(), FE = end(); FI != FE; ++FI) {
|
|
|
|
GCFunctionInfo &MD = **FI;
|
|
|
|
|
|
|
|
/** A compact GC layout. Emit this data structure:
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* int16_t PointCount;
|
|
|
|
* void *SafePointAddress[PointCount];
|
|
|
|
* int16_t StackFrameSize; (in words)
|
|
|
|
* int16_t StackArity;
|
|
|
|
* int16_t LiveCount;
|
|
|
|
* int16_t LiveOffsets[LiveCount];
|
|
|
|
* } __gcmap_<FUNCTIONNAME>;
|
|
|
|
**/
|
|
|
|
|
|
|
|
// Align to address width.
|
|
|
|
AP.EmitAlignment(IntPtrSize == 4 ? 2 : 3);
|
|
|
|
|
|
|
|
// Emit PointCount.
|
|
|
|
OS.AddComment("safe point count");
|
|
|
|
AP.EmitInt16(MD.size());
|
|
|
|
|
|
|
|
// And each safe point...
|
|
|
|
for (GCFunctionInfo::iterator PI = MD.begin(), PE = MD.end(); PI != PE;
|
|
|
|
++PI) {
|
|
|
|
// Emit the address of the safe point.
|
|
|
|
OS.AddComment("safe point address");
|
|
|
|
MCSymbol *Label = PI->Label;
|
|
|
|
AP.EmitLabelPlusOffset(Label/*Hi*/, 0/*Offset*/, 4/*Size*/);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stack information never change in safe points! Only print info from the
|
|
|
|
// first call-site.
|
|
|
|
GCFunctionInfo::iterator PI = MD.begin();
|
|
|
|
|
|
|
|
// Emit the stack frame size.
|
|
|
|
OS.AddComment("stack frame size (in words)");
|
|
|
|
AP.EmitInt16(MD.getFrameSize() / IntPtrSize);
|
|
|
|
|
|
|
|
// Emit stack arity, i.e. the number of stacked arguments.
|
|
|
|
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);
|
|
|
|
|
|
|
|
// Emit the number of live roots in the function.
|
|
|
|
OS.AddComment("live root count");
|
|
|
|
AP.EmitInt16(MD.live_size(PI));
|
|
|
|
|
|
|
|
// And for each live root...
|
|
|
|
for (GCFunctionInfo::live_iterator LI = MD.live_begin(PI),
|
|
|
|
LE = MD.live_end(PI);
|
|
|
|
LI != LE; ++LI) {
|
|
|
|
// Emit live root's offset within the stack frame.
|
|
|
|
OS.AddComment("stack index (offset / wordsize)");
|
|
|
|
AP.EmitInt16(LI->StackOffset / IntPtrSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|