xref: /openbsd-src/gnu/llvm/llvm/lib/CodeGen/AsmPrinter/ErlangGCPrinter.cpp (revision d415bd752c734aee168c4ee86ff32e8cc249eb16)
109467b48Spatrick //===- ErlangGCPrinter.cpp - Erlang/OTP frametable emitter ----------------===//
209467b48Spatrick //
309467b48Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
409467b48Spatrick // See https://llvm.org/LICENSE.txt for license information.
509467b48Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
609467b48Spatrick //
709467b48Spatrick //===----------------------------------------------------------------------===//
809467b48Spatrick //
909467b48Spatrick // This file implements the compiler plugin that is used in order to emit
1009467b48Spatrick // garbage collection information in a convenient layout for parsing and
1109467b48Spatrick // loading in the Erlang/OTP runtime.
1209467b48Spatrick //
1309467b48Spatrick //===----------------------------------------------------------------------===//
1409467b48Spatrick 
1509467b48Spatrick #include "llvm/BinaryFormat/ELF.h"
1609467b48Spatrick #include "llvm/CodeGen/AsmPrinter.h"
1709467b48Spatrick #include "llvm/CodeGen/GCMetadata.h"
1809467b48Spatrick #include "llvm/CodeGen/GCMetadataPrinter.h"
1973471bf0Spatrick #include "llvm/IR/BuiltinGCs.h"
2009467b48Spatrick #include "llvm/IR/DataLayout.h"
2109467b48Spatrick #include "llvm/IR/Function.h"
2209467b48Spatrick #include "llvm/IR/Module.h"
2309467b48Spatrick #include "llvm/MC/MCContext.h"
2409467b48Spatrick #include "llvm/MC/MCSectionELF.h"
2509467b48Spatrick #include "llvm/MC/MCStreamer.h"
2609467b48Spatrick #include "llvm/Target/TargetLoweringObjectFile.h"
2709467b48Spatrick 
2809467b48Spatrick using namespace llvm;
2909467b48Spatrick 
3009467b48Spatrick namespace {
3109467b48Spatrick 
3209467b48Spatrick class ErlangGCPrinter : public GCMetadataPrinter {
3309467b48Spatrick public:
3409467b48Spatrick   void finishAssembly(Module &M, GCModuleInfo &Info, AsmPrinter &AP) override;
3509467b48Spatrick };
3609467b48Spatrick 
3709467b48Spatrick } // end anonymous namespace
3809467b48Spatrick 
3909467b48Spatrick static GCMetadataPrinterRegistry::Add<ErlangGCPrinter>
4009467b48Spatrick     X("erlang", "erlang-compatible garbage collector");
4109467b48Spatrick 
finishAssembly(Module & M,GCModuleInfo & Info,AsmPrinter & AP)4209467b48Spatrick void ErlangGCPrinter::finishAssembly(Module &M, GCModuleInfo &Info,
4309467b48Spatrick                                      AsmPrinter &AP) {
4409467b48Spatrick   MCStreamer &OS = *AP.OutStreamer;
4509467b48Spatrick   unsigned IntPtrSize = M.getDataLayout().getPointerSize();
4609467b48Spatrick 
4709467b48Spatrick   // Put this in a custom .note section.
48*d415bd75Srobert   OS.switchSection(AP.getObjFileLowering().getContext().getELFSection(
49*d415bd75Srobert       ".note.gc", ELF::SHT_PROGBITS, 0));
5009467b48Spatrick 
5109467b48Spatrick   // For each function...
5209467b48Spatrick   for (GCModuleInfo::FuncInfoVec::iterator FI = Info.funcinfo_begin(),
5309467b48Spatrick                                            IE = Info.funcinfo_end();
5409467b48Spatrick        FI != IE; ++FI) {
5509467b48Spatrick     GCFunctionInfo &MD = **FI;
5609467b48Spatrick     if (MD.getStrategy().getName() != getStrategy().getName())
5709467b48Spatrick       // this function is managed by some other GC
5809467b48Spatrick       continue;
5909467b48Spatrick     /** A compact GC layout. Emit this data structure:
6009467b48Spatrick      *
6109467b48Spatrick      * struct {
6209467b48Spatrick      *   int16_t PointCount;
6309467b48Spatrick      *   void *SafePointAddress[PointCount];
6409467b48Spatrick      *   int16_t StackFrameSize; (in words)
6509467b48Spatrick      *   int16_t StackArity;
6609467b48Spatrick      *   int16_t LiveCount;
6709467b48Spatrick      *   int16_t LiveOffsets[LiveCount];
6809467b48Spatrick      * } __gcmap_<FUNCTIONNAME>;
6909467b48Spatrick      **/
7009467b48Spatrick 
7109467b48Spatrick     // Align to address width.
72097a140dSpatrick     AP.emitAlignment(IntPtrSize == 4 ? Align(4) : Align(8));
7309467b48Spatrick 
7409467b48Spatrick     // Emit PointCount.
7509467b48Spatrick     OS.AddComment("safe point count");
7609467b48Spatrick     AP.emitInt16(MD.size());
7709467b48Spatrick 
7809467b48Spatrick     // And each safe point...
7973471bf0Spatrick     for (const GCPoint &P : MD) {
8009467b48Spatrick       // Emit the address of the safe point.
8109467b48Spatrick       OS.AddComment("safe point address");
8273471bf0Spatrick       MCSymbol *Label = P.Label;
83097a140dSpatrick       AP.emitLabelPlusOffset(Label /*Hi*/, 0 /*Offset*/, 4 /*Size*/);
8409467b48Spatrick     }
8509467b48Spatrick 
8609467b48Spatrick     // Stack information never change in safe points! Only print info from the
8709467b48Spatrick     // first call-site.
8809467b48Spatrick     GCFunctionInfo::iterator PI = MD.begin();
8909467b48Spatrick 
9009467b48Spatrick     // Emit the stack frame size.
9109467b48Spatrick     OS.AddComment("stack frame size (in words)");
9209467b48Spatrick     AP.emitInt16(MD.getFrameSize() / IntPtrSize);
9309467b48Spatrick 
9409467b48Spatrick     // Emit stack arity, i.e. the number of stacked arguments.
9509467b48Spatrick     unsigned RegisteredArgs = IntPtrSize == 4 ? 5 : 6;
9609467b48Spatrick     unsigned StackArity = MD.getFunction().arg_size() > RegisteredArgs
9709467b48Spatrick                               ? MD.getFunction().arg_size() - RegisteredArgs
9809467b48Spatrick                               : 0;
9909467b48Spatrick     OS.AddComment("stack arity");
10009467b48Spatrick     AP.emitInt16(StackArity);
10109467b48Spatrick 
10209467b48Spatrick     // Emit the number of live roots in the function.
10309467b48Spatrick     OS.AddComment("live root count");
10409467b48Spatrick     AP.emitInt16(MD.live_size(PI));
10509467b48Spatrick 
10609467b48Spatrick     // And for each live root...
10709467b48Spatrick     for (GCFunctionInfo::live_iterator LI = MD.live_begin(PI),
10809467b48Spatrick                                        LE = MD.live_end(PI);
10909467b48Spatrick          LI != LE; ++LI) {
11009467b48Spatrick       // Emit live root's offset within the stack frame.
11109467b48Spatrick       OS.AddComment("stack index (offset / wordsize)");
11209467b48Spatrick       AP.emitInt16(LI->StackOffset / IntPtrSize);
11309467b48Spatrick     }
11409467b48Spatrick   }
11509467b48Spatrick }
11609467b48Spatrick 
linkErlangGCPrinter()11709467b48Spatrick void llvm::linkErlangGCPrinter() {}
118