1 //===- ErlangGCPrinter.cpp - Erlang/OTP frametable emitter ----------------===//
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
7 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/BinaryFormat/ELF.h"
16 #include "llvm/CodeGen/AsmPrinter.h"
17 #include "llvm/CodeGen/GCMetadata.h"
18 #include "llvm/CodeGen/GCMetadataPrinter.h"
19 #include "llvm/IR/BuiltinGCs.h"
20 #include "llvm/IR/DataLayout.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/MC/MCContext.h"
24 #include "llvm/MC/MCSectionELF.h"
25 #include "llvm/MC/MCStreamer.h"
26 #include "llvm/MC/MCSymbol.h"
27 #include "llvm/Target/TargetLoweringObjectFile.h"
33 class ErlangGCPrinter
: public GCMetadataPrinter
{
35 void finishAssembly(Module
&M
, GCModuleInfo
&Info
, AsmPrinter
&AP
) override
;
38 } // end anonymous namespace
40 static GCMetadataPrinterRegistry::Add
<ErlangGCPrinter
>
41 X("erlang", "erlang-compatible garbage collector");
43 void ErlangGCPrinter::finishAssembly(Module
&M
, GCModuleInfo
&Info
,
45 MCStreamer
&OS
= *AP
.OutStreamer
;
46 unsigned IntPtrSize
= M
.getDataLayout().getPointerSize();
48 // Put this in a custom .note section.
50 AP
.getObjFileLowering().getContext().getELFSection(".note.gc",
51 ELF::SHT_PROGBITS
, 0));
53 // For each function...
54 for (GCModuleInfo::FuncInfoVec::iterator FI
= Info
.funcinfo_begin(),
55 IE
= Info
.funcinfo_end();
57 GCFunctionInfo
&MD
= **FI
;
58 if (MD
.getStrategy().getName() != getStrategy().getName())
59 // this function is managed by some other GC
61 /** A compact GC layout. Emit this data structure:
65 * void *SafePointAddress[PointCount];
66 * int16_t StackFrameSize; (in words)
69 * int16_t LiveOffsets[LiveCount];
70 * } __gcmap_<FUNCTIONNAME>;
73 // Align to address width.
74 AP
.emitAlignment(IntPtrSize
== 4 ? Align(4) : Align(8));
77 OS
.AddComment("safe point count");
78 AP
.emitInt16(MD
.size());
80 // And each safe point...
81 for (const GCPoint
&P
: MD
) {
82 // Emit the address of the safe point.
83 OS
.AddComment("safe point address");
84 MCSymbol
*Label
= P
.Label
;
85 AP
.emitLabelPlusOffset(Label
/*Hi*/, 0 /*Offset*/, 4 /*Size*/);
88 // Stack information never change in safe points! Only print info from the
90 GCFunctionInfo::iterator PI
= MD
.begin();
92 // Emit the stack frame size.
93 OS
.AddComment("stack frame size (in words)");
94 AP
.emitInt16(MD
.getFrameSize() / IntPtrSize
);
96 // Emit stack arity, i.e. the number of stacked arguments.
97 unsigned RegisteredArgs
= IntPtrSize
== 4 ? 5 : 6;
98 unsigned StackArity
= MD
.getFunction().arg_size() > RegisteredArgs
99 ? MD
.getFunction().arg_size() - RegisteredArgs
101 OS
.AddComment("stack arity");
102 AP
.emitInt16(StackArity
);
104 // Emit the number of live roots in the function.
105 OS
.AddComment("live root count");
106 AP
.emitInt16(MD
.live_size(PI
));
108 // And for each live root...
109 for (GCFunctionInfo::live_iterator LI
= MD
.live_begin(PI
),
110 LE
= MD
.live_end(PI
);
112 // Emit live root's offset within the stack frame.
113 OS
.AddComment("stack index (offset / wordsize)");
114 AP
.emitInt16(LI
->StackOffset
/ IntPtrSize
);
119 void llvm::linkErlangGCPrinter() {}