blob: 989da7e0cd413e7097dfb85e3c6b16ad07ba2cd8 [file] [log] [blame]
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001//===- ErlangGCPrinter.cpp - Erlang/OTP frametable emitter ----------------===//
Yiannis Tsiourisdbb4adf2013-03-25 13:47:46 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Yiannis Tsiourisdbb4adf2013-03-25 13:47:46 +00006//
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
Zachary Turner264b5d92017-06-07 03:48:56 +000015#include "llvm/BinaryFormat/ELF.h"
Yiannis Tsiourisdbb4adf2013-03-25 13:47:46 +000016#include "llvm/CodeGen/AsmPrinter.h"
Philip Reames9b8c1022018-11-10 16:08:10 +000017#include "llvm/CodeGen/BuiltinGCs.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000018#include "llvm/CodeGen/GCMetadata.h"
Yiannis Tsiourisdbb4adf2013-03-25 13:47:46 +000019#include "llvm/CodeGen/GCMetadataPrinter.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000020#include "llvm/CodeGen/GCStrategy.h"
Yiannis Tsiourisdbb4adf2013-03-25 13:47:46 +000021#include "llvm/IR/DataLayout.h"
22#include "llvm/IR/Function.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000023#include "llvm/IR/Module.h"
Yiannis Tsiourisdbb4adf2013-03-25 13:47:46 +000024#include "llvm/MC/MCContext.h"
25#include "llvm/MC/MCSectionELF.h"
26#include "llvm/MC/MCStreamer.h"
27#include "llvm/MC/MCSymbol.h"
David Blaikie6054e652018-03-23 23:58:19 +000028#include "llvm/Target/TargetLoweringObjectFile.h"
Yiannis Tsiourisdbb4adf2013-03-25 13:47:46 +000029
30using namespace llvm;
31
32namespace {
33
Philip Reames36319532015-01-16 23:16:12 +000034class ErlangGCPrinter : public GCMetadataPrinter {
35public:
36 void finishAssembly(Module &M, GCModuleInfo &Info, AsmPrinter &AP) override;
37};
Eugene Zelenkod96089b2017-02-14 00:33:36 +000038
39} // end anonymous namespace
Yiannis Tsiourisdbb4adf2013-03-25 13:47:46 +000040
41static GCMetadataPrinterRegistry::Add<ErlangGCPrinter>
Philip Reames36319532015-01-16 23:16:12 +000042 X("erlang", "erlang-compatible garbage collector");
Yiannis Tsiourisdbb4adf2013-03-25 13:47:46 +000043
Philip Reames1e308972014-12-11 01:47:23 +000044void ErlangGCPrinter::finishAssembly(Module &M, GCModuleInfo &Info,
45 AsmPrinter &AP) {
Lang Hames9ff69c82015-04-24 19:11:51 +000046 MCStreamer &OS = *AP.OutStreamer;
Mehdi Aminibd7287e2015-07-16 06:11:10 +000047 unsigned IntPtrSize = M.getDataLayout().getPointerSize();
Yiannis Tsiourisdbb4adf2013-03-25 13:47:46 +000048
49 // Put this in a custom .note section.
Lang Hames9ff69c82015-04-24 19:11:51 +000050 OS.SwitchSection(
Rafael Espindolaba31e272015-01-29 17:33:21 +000051 AP.getObjFileLowering().getContext().getELFSection(".note.gc",
52 ELF::SHT_PROGBITS, 0));
Yiannis Tsiourisdbb4adf2013-03-25 13:47:46 +000053
54 // For each function...
Philip Reames1e308972014-12-11 01:47:23 +000055 for (GCModuleInfo::FuncInfoVec::iterator FI = Info.funcinfo_begin(),
Philip Reames36319532015-01-16 23:16:12 +000056 IE = Info.funcinfo_end();
Philip Reames1e308972014-12-11 01:47:23 +000057 FI != IE; ++FI) {
Yiannis Tsiourisdbb4adf2013-03-25 13:47:46 +000058 GCFunctionInfo &MD = **FI;
Philip Reames1e308972014-12-11 01:47:23 +000059 if (MD.getStrategy().getName() != getStrategy().getName())
60 // this function is managed by some other GC
61 continue;
Yiannis Tsiourisdbb4adf2013-03-25 13:47:46 +000062 /** 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.
Guillaume Chatelet18f805a2019-09-27 12:54:21 +000075 AP.EmitAlignment(IntPtrSize == 4 ? Align(4) : Align(8));
Yiannis Tsiourisdbb4adf2013-03-25 13:47:46 +000076
77 // Emit PointCount.
78 OS.AddComment("safe point count");
Rafael Espindola4b4d85f2018-03-29 23:32:54 +000079 AP.emitInt16(MD.size());
Yiannis Tsiourisdbb4adf2013-03-25 13:47:46 +000080
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;
Fangrui Song0bc77a02020-02-13 13:26:21 -080087 AP.emitLabelPlusOffset(Label /*Hi*/, 0 /*Offset*/, 4 /*Size*/);
Yiannis Tsiourisdbb4adf2013-03-25 13:47:46 +000088 }
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)");
Rafael Espindola4b4d85f2018-03-29 23:32:54 +000096 AP.emitInt16(MD.getFrameSize() / IntPtrSize);
Yiannis Tsiourisdbb4adf2013-03-25 13:47:46 +000097
98 // Emit stack arity, i.e. the number of stacked arguments.
99 unsigned RegisteredArgs = IntPtrSize == 4 ? 5 : 6;
Philip Reames36319532015-01-16 23:16:12 +0000100 unsigned StackArity = MD.getFunction().arg_size() > RegisteredArgs
101 ? MD.getFunction().arg_size() - RegisteredArgs
102 : 0;
Yiannis Tsiourisdbb4adf2013-03-25 13:47:46 +0000103 OS.AddComment("stack arity");
Rafael Espindola4b4d85f2018-03-29 23:32:54 +0000104 AP.emitInt16(StackArity);
Yiannis Tsiourisdbb4adf2013-03-25 13:47:46 +0000105
106 // Emit the number of live roots in the function.
107 OS.AddComment("live root count");
Rafael Espindola4b4d85f2018-03-29 23:32:54 +0000108 AP.emitInt16(MD.live_size(PI));
Yiannis Tsiourisdbb4adf2013-03-25 13:47:46 +0000109
110 // And for each live root...
111 for (GCFunctionInfo::live_iterator LI = MD.live_begin(PI),
112 LE = MD.live_end(PI);
Philip Reames36319532015-01-16 23:16:12 +0000113 LI != LE; ++LI) {
Yiannis Tsiourisdbb4adf2013-03-25 13:47:46 +0000114 // Emit live root's offset within the stack frame.
115 OS.AddComment("stack index (offset / wordsize)");
Rafael Espindola4b4d85f2018-03-29 23:32:54 +0000116 AP.emitInt16(LI->StackOffset / IntPtrSize);
Yiannis Tsiourisdbb4adf2013-03-25 13:47:46 +0000117 }
118 }
119}
Eugene Zelenkod96089b2017-02-14 00:33:36 +0000120
121void llvm::linkErlangGCPrinter() {}