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/BuiltinGCs.h"
18 #include "llvm/CodeGen/GCMetadata.h"
19 #include "llvm/CodeGen/GCMetadataPrinter.h"
20 #include "llvm/CodeGen/GCStrategy.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"
28 #include "llvm/Target/TargetLoweringObjectFile.h"
34 class ErlangGCPrinter
: public GCMetadataPrinter
{
36 void finishAssembly(Module
&M
, GCModuleInfo
&Info
, AsmPrinter
&AP
) override
;
39 } // end anonymous namespace
41 static GCMetadataPrinterRegistry::Add
<ErlangGCPrinter
>
42 X("erlang", "erlang-compatible garbage collector");
44 void ErlangGCPrinter::finishAssembly(Module
&M
, GCModuleInfo
&Info
,
46 MCStreamer
&OS
= *AP
.OutStreamer
;
47 unsigned IntPtrSize
= M
.getDataLayout().getPointerSize();
49 // Put this in a custom .note section.
51 AP
.getObjFileLowering().getContext().getELFSection(".note.gc",
52 ELF::SHT_PROGBITS
, 0));
54 // For each function...
55 for (GCModuleInfo::FuncInfoVec::iterator FI
= Info
.funcinfo_begin(),
56 IE
= Info
.funcinfo_end();
58 GCFunctionInfo
&MD
= **FI
;
59 if (MD
.getStrategy().getName() != getStrategy().getName())
60 // this function is managed by some other GC
62 /** A compact GC layout. Emit this data structure:
66 * void *SafePointAddress[PointCount];
67 * int16_t StackFrameSize; (in words)
70 * int16_t LiveOffsets[LiveCount];
71 * } __gcmap_<FUNCTIONNAME>;
74 // Align to address width.
75 AP
.EmitAlignment(IntPtrSize
== 4 ? llvm::Align(4) : llvm::Align(8));
78 OS
.AddComment("safe point count");
79 AP
.emitInt16(MD
.size());
81 // And each safe point...
82 for (GCFunctionInfo::iterator PI
= MD
.begin(), PE
= MD
.end(); PI
!= PE
;
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*/);
90 // Stack information never change in safe points! Only print info from the
92 GCFunctionInfo::iterator PI
= MD
.begin();
94 // Emit the stack frame size.
95 OS
.AddComment("stack frame size (in words)");
96 AP
.emitInt16(MD
.getFrameSize() / IntPtrSize
);
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
103 OS
.AddComment("stack arity");
104 AP
.emitInt16(StackArity
);
106 // Emit the number of live roots in the function.
107 OS
.AddComment("live root count");
108 AP
.emitInt16(MD
.live_size(PI
));
110 // And for each live root...
111 for (GCFunctionInfo::live_iterator LI
= MD
.live_begin(PI
),
112 LE
= MD
.live_end(PI
);
114 // Emit live root's offset within the stack frame.
115 OS
.AddComment("stack index (offset / wordsize)");
116 AP
.emitInt16(LI
->StackOffset
/ IntPtrSize
);
121 void llvm::linkErlangGCPrinter() {}