blob: e293acd43a88eaed83d5597c23931c086c2f66b5 [file] [log] [blame]
Yiannis Tsiourisdbb4adf2013-03-25 13:47:46 +00001//===-- ErlangGCPrinter.cpp - Erlang/OTP frametable emitter -----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the compiler plugin that is used in order to emit
11// garbage collection information in a convenient layout for parsing and
12// loading in the Erlang/OTP runtime.
13//
14//===----------------------------------------------------------------------===//
15
16#include "llvm/CodeGen/AsmPrinter.h"
Yiannis Tsiourisdbb4adf2013-03-25 13:47:46 +000017#include "llvm/CodeGen/GCMetadataPrinter.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000018#include "llvm/CodeGen/GCs.h"
Yiannis Tsiourisdbb4adf2013-03-25 13:47:46 +000019#include "llvm/IR/DataLayout.h"
20#include "llvm/IR/Function.h"
21#include "llvm/IR/Instruction.h"
22#include "llvm/IR/IntrinsicInst.h"
23#include "llvm/IR/Metadata.h"
24#include "llvm/MC/MCAsmInfo.h"
25#include "llvm/MC/MCContext.h"
26#include "llvm/MC/MCSectionELF.h"
27#include "llvm/MC/MCStreamer.h"
28#include "llvm/MC/MCSymbol.h"
29#include "llvm/Target/TargetLoweringObjectFile.h"
30#include "llvm/Target/TargetMachine.h"
Eric Christopherd9134482014-08-04 21:25:23 +000031#include "llvm/Target/TargetSubtargetInfo.h"
Yiannis Tsiourisdbb4adf2013-03-25 13:47:46 +000032
33using namespace llvm;
34
35namespace {
36
37 class ErlangGCPrinter : public GCMetadataPrinter {
38 public:
Philip Reames1e308972014-12-11 01:47:23 +000039 void finishAssembly(Module &M, GCModuleInfo &Info,
40 AsmPrinter &AP) override;
Yiannis Tsiourisdbb4adf2013-03-25 13:47:46 +000041 };
42
43}
44
45static GCMetadataPrinterRegistry::Add<ErlangGCPrinter>
46X("erlang", "erlang-compatible garbage collector");
47
48void llvm::linkErlangGCPrinter() { }
49
Philip Reames1e308972014-12-11 01:47:23 +000050void ErlangGCPrinter::finishAssembly(Module &M, GCModuleInfo &Info,
51 AsmPrinter &AP) {
Yiannis Tsiourisdbb4adf2013-03-25 13:47:46 +000052 MCStreamer &OS = AP.OutStreamer;
Eric Christopherd9134482014-08-04 21:25:23 +000053 unsigned IntPtrSize =
54 AP.TM.getSubtargetImpl()->getDataLayout()->getPointerSize();
Yiannis Tsiourisdbb4adf2013-03-25 13:47:46 +000055
56 // Put this in a custom .note section.
57 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getContext()
58 .getELFSection(".note.gc", ELF::SHT_PROGBITS, 0,
59 SectionKind::getDataRel()));
60
61 // For each function...
Philip Reames1e308972014-12-11 01:47:23 +000062 for (GCModuleInfo::FuncInfoVec::iterator FI = Info.funcinfo_begin(),
63 IE = Info.funcinfo_end();
64 FI != IE; ++FI) {
Yiannis Tsiourisdbb4adf2013-03-25 13:47:46 +000065 GCFunctionInfo &MD = **FI;
Philip Reames1e308972014-12-11 01:47:23 +000066 if (MD.getStrategy().getName() != getStrategy().getName())
67 // this function is managed by some other GC
68 continue;
Yiannis Tsiourisdbb4adf2013-03-25 13:47:46 +000069 /** A compact GC layout. Emit this data structure:
70 *
71 * struct {
72 * int16_t PointCount;
73 * void *SafePointAddress[PointCount];
74 * int16_t StackFrameSize; (in words)
75 * int16_t StackArity;
76 * int16_t LiveCount;
77 * int16_t LiveOffsets[LiveCount];
78 * } __gcmap_<FUNCTIONNAME>;
79 **/
80
81 // Align to address width.
82 AP.EmitAlignment(IntPtrSize == 4 ? 2 : 3);
83
84 // Emit PointCount.
85 OS.AddComment("safe point count");
86 AP.EmitInt16(MD.size());
87
88 // And each safe point...
89 for (GCFunctionInfo::iterator PI = MD.begin(), PE = MD.end(); PI != PE;
90 ++PI) {
91 // Emit the address of the safe point.
92 OS.AddComment("safe point address");
93 MCSymbol *Label = PI->Label;
94 AP.EmitLabelPlusOffset(Label/*Hi*/, 0/*Offset*/, 4/*Size*/);
95 }
96
97 // Stack information never change in safe points! Only print info from the
98 // first call-site.
99 GCFunctionInfo::iterator PI = MD.begin();
100
101 // Emit the stack frame size.
102 OS.AddComment("stack frame size (in words)");
103 AP.EmitInt16(MD.getFrameSize() / IntPtrSize);
104
105 // Emit stack arity, i.e. the number of stacked arguments.
106 unsigned RegisteredArgs = IntPtrSize == 4 ? 5 : 6;
107 unsigned StackArity = MD.getFunction().arg_size() > RegisteredArgs ?
108 MD.getFunction().arg_size() - RegisteredArgs : 0;
109 OS.AddComment("stack arity");
110 AP.EmitInt16(StackArity);
111
112 // Emit the number of live roots in the function.
113 OS.AddComment("live root count");
114 AP.EmitInt16(MD.live_size(PI));
115
116 // And for each live root...
117 for (GCFunctionInfo::live_iterator LI = MD.live_begin(PI),
118 LE = MD.live_end(PI);
119 LI != LE; ++LI) {
120 // Emit live root's offset within the stack frame.
121 OS.AddComment("stack index (offset / wordsize)");
122 AP.EmitInt16(LI->StackOffset / IntPtrSize);
123 }
124 }
125}