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"
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"
29
30using namespace llvm;
31
32namespace {
33
34class ErlangGCPrinter : public GCMetadataPrinter {
35public:
36  void finishAssembly(Module &M, GCModuleInfo &Info, AsmPrinter &AP) override;
37};
38
39} // end anonymous namespace
40
41static GCMetadataPrinterRegistry::Add<ErlangGCPrinter>
42    X("erlang", "erlang-compatible garbage collector");
43
44void 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(
51      AP.getObjFileLowering().getContext().getELFSection(".note.gc",
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 ? Align(4) : Align(8));
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.
92    GCFunctionInfo::iterator PI = MD.begin();
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
121void llvm::linkErlangGCPrinter() {}
122