LLVM  8.0.1
ErlangGCPrinter.cpp
Go to the documentation of this file.
1 //===- ErlangGCPrinter.cpp - Erlang/OTP frametable emitter ----------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the compiler plugin that is used in order to emit
11 // garbage collection information in a convenient layout for parsing and
12 // loading in the Erlang/OTP runtime.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "llvm/BinaryFormat/ELF.h"
22 #include "llvm/IR/DataLayout.h"
23 #include "llvm/IR/Function.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/MC/MCContext.h"
26 #include "llvm/MC/MCSectionELF.h"
27 #include "llvm/MC/MCStreamer.h"
28 #include "llvm/MC/MCSymbol.h"
30 
31 using namespace llvm;
32 
33 namespace {
34 
35 class ErlangGCPrinter : public GCMetadataPrinter {
36 public:
37  void finishAssembly(Module &M, GCModuleInfo &Info, AsmPrinter &AP) override;
38 };
39 
40 } // end anonymous namespace
41 
43  X("erlang", "erlang-compatible garbage collector");
44 
45 void ErlangGCPrinter::finishAssembly(Module &M, GCModuleInfo &Info,
46  AsmPrinter &AP) {
47  MCStreamer &OS = *AP.OutStreamer;
48  unsigned IntPtrSize = M.getDataLayout().getPointerSize();
49 
50  // Put this in a custom .note section.
51  OS.SwitchSection(
53  ELF::SHT_PROGBITS, 0));
54 
55  // For each function...
56  for (GCModuleInfo::FuncInfoVec::iterator FI = Info.funcinfo_begin(),
57  IE = Info.funcinfo_end();
58  FI != IE; ++FI) {
59  GCFunctionInfo &MD = **FI;
60  if (MD.getStrategy().getName() != getStrategy().getName())
61  // this function is managed by some other GC
62  continue;
63  /** A compact GC layout. Emit this data structure:
64  *
65  * struct {
66  * int16_t PointCount;
67  * void *SafePointAddress[PointCount];
68  * int16_t StackFrameSize; (in words)
69  * int16_t StackArity;
70  * int16_t LiveCount;
71  * int16_t LiveOffsets[LiveCount];
72  * } __gcmap_<FUNCTIONNAME>;
73  **/
74 
75  // Align to address width.
76  AP.EmitAlignment(IntPtrSize == 4 ? 2 : 3);
77 
78  // Emit PointCount.
79  OS.AddComment("safe point count");
80  AP.emitInt16(MD.size());
81 
82  // And each safe point...
83  for (GCFunctionInfo::iterator PI = MD.begin(), PE = MD.end(); PI != PE;
84  ++PI) {
85  // Emit the address of the safe point.
86  OS.AddComment("safe point address");
87  MCSymbol *Label = PI->Label;
88  AP.EmitLabelPlusOffset(Label /*Hi*/, 0 /*Offset*/, 4 /*Size*/);
89  }
90 
91  // Stack information never change in safe points! Only print info from the
92  // first call-site.
94 
95  // Emit the stack frame size.
96  OS.AddComment("stack frame size (in words)");
97  AP.emitInt16(MD.getFrameSize() / IntPtrSize);
98 
99  // Emit stack arity, i.e. the number of stacked arguments.
100  unsigned RegisteredArgs = IntPtrSize == 4 ? 5 : 6;
101  unsigned StackArity = MD.getFunction().arg_size() > RegisteredArgs
102  ? MD.getFunction().arg_size() - RegisteredArgs
103  : 0;
104  OS.AddComment("stack arity");
105  AP.emitInt16(StackArity);
106 
107  // Emit the number of live roots in the function.
108  OS.AddComment("live root count");
109  AP.emitInt16(MD.live_size(PI));
110 
111  // And for each live root...
112  for (GCFunctionInfo::live_iterator LI = MD.live_begin(PI),
113  LE = MD.live_end(PI);
114  LI != LE; ++LI) {
115  // Emit live root's offset within the stack frame.
116  OS.AddComment("stack index (offset / wordsize)");
117  AP.emitInt16(LI->StackOffset / IntPtrSize);
118  }
119  }
120 }
121 
const TargetLoweringObjectFile & getObjFileLowering() const
Return information about object file lowering.
Definition: AsmPrinter.cpp:212
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
size_t live_size(const iterator &p) const
Definition: GCMetadata.h:147
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:94
This class represents lattice values for constants.
Definition: AllocatorList.h:24
size_t size() const
Definition: GCMetadata.h:137
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
void linkErlangGCPrinter()
Creates an erlang-compatible metadata printer.
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:371
An analysis pass which caches information about the entire Module.
Definition: GCMetadata.h:153
virtual void AddComment(const Twine &T, bool EOL=true)
Add a textual comment.
Definition: MCStreamer.h:312
const std::string & getName() const
Return the name of the GC strategy.
Definition: GCStrategy.h:87
Analysis containing CSE Info
Definition: CSEInfo.cpp:21
void EmitLabelPlusOffset(const MCSymbol *Label, uint64_t Offset, unsigned Size, bool IsSectionRelative=false) const
Emit something like ".long Label+Offset" where the size in bytes of the directive is specified by Siz...
Streaming machine code generation interface.
Definition: MCStreamer.h:189
const Function & getFunction() const
getFunction - Return the function to which this metadata applies.
Definition: GCMetadata.h:106
unsigned getPointerSize(unsigned AS=0) const
Layout pointer size FIXME: The defaults need to be removed once all of the backends/clients are updat...
Definition: DataLayout.cpp:629
virtual void SwitchSection(MCSection *Section, const MCExpr *Subsection=nullptr)
Set the current section where code is being emitted to Section.
void EmitAlignment(unsigned NumBits, const GlobalObject *GV=nullptr) const
Emit an alignment directive to the specified power of two boundary.
GCStrategy & getStrategy()
getStrategy - Return the GC strategy for the function.
Definition: GCMetadata.h:109
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:79
size_t arg_size() const
Definition: Function.h:698
FuncInfoVec::iterator funcinfo_begin()
Definition: GCMetadata.h:169
live_iterator live_begin(const iterator &p)
live_begin/live_end - Iterators for live roots at a given safe point.
Definition: GCMetadata.h:145
Module.h This file contains the declarations for the Module class.
FuncInfoVec::iterator funcinfo_end()
Definition: GCMetadata.h:170
iterator begin()
begin/end - Iterators for safe points.
Definition: GCMetadata.h:135
std::vector< GCPoint >::iterator iterator
Definition: GCMetadata.h:80
uint64_t getFrameSize() const
getFrameSize/setFrameSize - Records the function&#39;s frame size.
Definition: GCMetadata.h:131
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
Definition: MCContext.h:389
std::vector< GCRoot >::const_iterator live_iterator
Definition: GCMetadata.h:82
GCMetadataPrinter - Emits GC metadata as assembly code.
live_iterator live_end(const iterator &p)
Definition: GCMetadata.h:146
Garbage collection metadata for a single function.
Definition: GCMetadata.h:78
A static registration template.
Definition: Registry.h:115
void emitInt16(int Value) const
Emit a short directive and value.