1//===-- 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"
17#include "llvm/CodeGen/GCMetadataPrinter.h"
18#include "llvm/CodeGen/GCs.h"
19#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"
31
32using namespace llvm;
33
34namespace {
35
36  class ErlangGCPrinter : public GCMetadataPrinter {
37  public:
38    void beginAssembly(AsmPrinter &AP) override;
39    void finishAssembly(AsmPrinter &AP) override;
40  };
41
42}
43
44static GCMetadataPrinterRegistry::Add<ErlangGCPrinter>
45X("erlang", "erlang-compatible garbage collector");
46
47void llvm::linkErlangGCPrinter() { }
48
49void ErlangGCPrinter::beginAssembly(AsmPrinter &AP) { }
50
51void ErlangGCPrinter::finishAssembly(AsmPrinter &AP) {
52  MCStreamer &OS = AP.OutStreamer;
53  unsigned IntPtrSize = AP.TM.getDataLayout()->getPointerSize();
54
55  // Put this in a custom .note section.
56  AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getContext()
57    .getELFSection(".note.gc", ELF::SHT_PROGBITS, 0,
58                   SectionKind::getDataRel()));
59
60  // For each function...
61  for (iterator FI = begin(), FE = end(); FI != FE; ++FI) {
62    GCFunctionInfo &MD = **FI;
63
64    /** A compact GC layout. Emit this data structure:
65     *
66     * struct {
67     *   int16_t PointCount;
68     *   void *SafePointAddress[PointCount];
69     *   int16_t StackFrameSize; (in words)
70     *   int16_t StackArity;
71     *   int16_t LiveCount;
72     *   int16_t LiveOffsets[LiveCount];
73     * } __gcmap_<FUNCTIONNAME>;
74     **/
75
76    // Align to address width.
77    AP.EmitAlignment(IntPtrSize == 4 ? 2 : 3);
78
79    // Emit PointCount.
80    OS.AddComment("safe point count");
81    AP.EmitInt16(MD.size());
82
83    // And each safe point...
84    for (GCFunctionInfo::iterator PI = MD.begin(), PE = MD.end(); PI != PE;
85         ++PI) {
86      // Emit the address of the safe point.
87      OS.AddComment("safe point address");
88      MCSymbol *Label = PI->Label;
89      AP.EmitLabelPlusOffset(Label/*Hi*/, 0/*Offset*/, 4/*Size*/);
90    }
91
92    // Stack information never change in safe points! Only print info from the
93    // first call-site.
94    GCFunctionInfo::iterator PI = MD.begin();
95
96    // Emit the stack frame size.
97    OS.AddComment("stack frame size (in words)");
98    AP.EmitInt16(MD.getFrameSize() / IntPtrSize);
99
100    // Emit stack arity, i.e. the number of stacked arguments.
101    unsigned RegisteredArgs = IntPtrSize == 4 ? 5 : 6;
102    unsigned StackArity = MD.getFunction().arg_size() > RegisteredArgs ?
103                          MD.getFunction().arg_size() - RegisteredArgs : 0;
104    OS.AddComment("stack arity");
105    AP.EmitInt16(StackArity);
106
107    // Emit the number of live roots in the function.
108    OS.AddComment("live root count");
109    AP.EmitInt16(MD.live_size(PI));
110
111    // And for each live root...
112    for (GCFunctionInfo::live_iterator LI = MD.live_begin(PI),
113                                       LE = MD.live_end(PI);
114                                       LI != LE; ++LI) {
115      // Emit live root's offset within the stack frame.
116      OS.AddComment("stack index (offset / wordsize)");
117      AP.EmitInt16(LI->StackOffset / IntPtrSize);
118    }
119  }
120}
121