LLVM  9.0.0svn
ErlangGCPrinter.cpp
Go to the documentation of this file.
1 //===- ErlangGCPrinter.cpp - Erlang/OTP frametable emitter ----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the compiler plugin that is used in order to emit
10 // garbage collection information in a convenient layout for parsing and
11 // loading in the Erlang/OTP runtime.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/BinaryFormat/ELF.h"
21 #include "llvm/IR/DataLayout.h"
22 #include "llvm/IR/Function.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/MC/MCContext.h"
25 #include "llvm/MC/MCSectionELF.h"
26 #include "llvm/MC/MCStreamer.h"
27 #include "llvm/MC/MCSymbol.h"
29 
30 using namespace llvm;
31 
32 namespace {
33 
34 class ErlangGCPrinter : public GCMetadataPrinter {
35 public:
36  void finishAssembly(Module &M, GCModuleInfo &Info, AsmPrinter &AP) override;
37 };
38 
39 } // end anonymous namespace
40 
42  X("erlang", "erlang-compatible garbage collector");
43 
44 void ErlangGCPrinter::finishAssembly(Module &M, GCModuleInfo &Info,
45  AsmPrinter &AP) {
46  MCStreamer &OS = *AP.OutStreamer;
47  unsigned IntPtrSize = M.getDataLayout().getPointerSize();
48 
49  // Put this in a custom .note section.
50  OS.SwitchSection(
52  ELF::SHT_PROGBITS, 0));
53 
54  // For each function...
55  for (GCModuleInfo::FuncInfoVec::iterator FI = Info.funcinfo_begin(),
56  IE = Info.funcinfo_end();
57  FI != IE; ++FI) {
58  GCFunctionInfo &MD = **FI;
59  if (MD.getStrategy().getName() != getStrategy().getName())
60  // this function is managed by some other GC
61  continue;
62  /** A compact GC layout. Emit this data structure:
63  *
64  * struct {
65  * int16_t PointCount;
66  * void *SafePointAddress[PointCount];
67  * int16_t StackFrameSize; (in words)
68  * int16_t StackArity;
69  * int16_t LiveCount;
70  * int16_t LiveOffsets[LiveCount];
71  * } __gcmap_<FUNCTIONNAME>;
72  **/
73 
74  // Align to address width.
75  AP.EmitAlignment(IntPtrSize == 4 ? 2 : 3);
76 
77  // Emit PointCount.
78  OS.AddComment("safe point count");
79  AP.emitInt16(MD.size());
80 
81  // And each safe point...
82  for (GCFunctionInfo::iterator PI = MD.begin(), PE = MD.end(); PI != PE;
83  ++PI) {
84  // Emit the address of the safe point.
85  OS.AddComment("safe point address");
86  MCSymbol *Label = PI->Label;
87  AP.EmitLabelPlusOffset(Label /*Hi*/, 0 /*Offset*/, 4 /*Size*/);
88  }
89 
90  // Stack information never change in safe points! Only print info from the
91  // first call-site.
93 
94  // Emit the stack frame size.
95  OS.AddComment("stack frame size (in words)");
96  AP.emitInt16(MD.getFrameSize() / IntPtrSize);
97 
98  // Emit stack arity, i.e. the number of stacked arguments.
99  unsigned RegisteredArgs = IntPtrSize == 4 ? 5 : 6;
100  unsigned StackArity = MD.getFunction().arg_size() > RegisteredArgs
101  ? MD.getFunction().arg_size() - RegisteredArgs
102  : 0;
103  OS.AddComment("stack arity");
104  AP.emitInt16(StackArity);
105 
106  // Emit the number of live roots in the function.
107  OS.AddComment("live root count");
108  AP.emitInt16(MD.live_size(PI));
109 
110  // And for each live root...
111  for (GCFunctionInfo::live_iterator LI = MD.live_begin(PI),
112  LE = MD.live_end(PI);
113  LI != LE; ++LI) {
114  // Emit live root's offset within the stack frame.
115  OS.AddComment("stack index (offset / wordsize)");
116  AP.emitInt16(LI->StackOffset / IntPtrSize);
117  }
118  }
119 }
120 
const TargetLoweringObjectFile & getObjFileLowering() const
Return information about object file lowering.
Definition: AsmPrinter.cpp:213
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
size_t live_size(const iterator &p) const
Definition: GCMetadata.h:146
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:93
This class represents lattice values for constants.
Definition: AllocatorList.h:23
size_t size() const
Definition: GCMetadata.h:136
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
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:369
An analysis pass which caches information about the entire Module.
Definition: GCMetadata.h:152
virtual void AddComment(const Twine &T, bool EOL=true)
Add a textual comment.
Definition: MCStreamer.h:309
const std::string & getName() const
Return the name of the GC strategy.
Definition: GCStrategy.h:86
Analysis containing CSE Info
Definition: CSEInfo.cpp:20
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:188
const Function & getFunction() const
getFunction - Return the function to which this metadata applies.
Definition: GCMetadata.h:105
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:648
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:108
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:78
size_t arg_size() const
Definition: Function.h:714
FuncInfoVec::iterator funcinfo_begin()
Definition: GCMetadata.h:168
live_iterator live_begin(const iterator &p)
live_begin/live_end - Iterators for live roots at a given safe point.
Definition: GCMetadata.h:144
Module.h This file contains the declarations for the Module class.
FuncInfoVec::iterator funcinfo_end()
Definition: GCMetadata.h:169
iterator begin()
begin/end - Iterators for safe points.
Definition: GCMetadata.h:134
std::vector< GCPoint >::iterator iterator
Definition: GCMetadata.h:79
uint64_t getFrameSize() const
getFrameSize/setFrameSize - Records the function&#39;s frame size.
Definition: GCMetadata.h:130
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
Definition: MCContext.h:388
std::vector< GCRoot >::const_iterator live_iterator
Definition: GCMetadata.h:81
GCMetadataPrinter - Emits GC metadata as assembly code.
live_iterator live_end(const iterator &p)
Definition: GCMetadata.h:145
Garbage collection metadata for a single function.
Definition: GCMetadata.h:77
A static registration template.
Definition: Registry.h:114
void emitInt16(int Value) const
Emit a short directive and value.