1 //===-- ErlangGCPrinter.cpp - Erlang/OTP frametable emitter -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
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 #include "llvm/Target/TargetSubtargetInfo.h"
37 class ErlangGCPrinter : public GCMetadataPrinter {
39 void beginAssembly(AsmPrinter &AP) override;
40 void finishAssembly(AsmPrinter &AP) override;
45 static GCMetadataPrinterRegistry::Add<ErlangGCPrinter>
46 X("erlang", "erlang-compatible garbage collector");
48 void llvm::linkErlangGCPrinter() { }
50 void ErlangGCPrinter::beginAssembly(AsmPrinter &AP) { }
52 void ErlangGCPrinter::finishAssembly(AsmPrinter &AP) {
53 MCStreamer &OS = AP.OutStreamer;
55 AP.TM.getSubtargetImpl()->getDataLayout()->getPointerSize();
57 // Put this in a custom .note section.
58 AP.OutStreamer.SwitchSection(AP.getObjFileLowering().getContext()
59 .getELFSection(".note.gc", ELF::SHT_PROGBITS, 0,
60 SectionKind::getDataRel()));
62 // For each function...
63 for (iterator FI = begin(), FE = end(); FI != FE; ++FI) {
64 GCFunctionInfo &MD = **FI;
66 /** A compact GC layout. Emit this data structure:
70 * void *SafePointAddress[PointCount];
71 * int16_t StackFrameSize; (in words)
74 * int16_t LiveOffsets[LiveCount];
75 * } __gcmap_<FUNCTIONNAME>;
78 // Align to address width.
79 AP.EmitAlignment(IntPtrSize == 4 ? 2 : 3);
82 OS.AddComment("safe point count");
83 AP.EmitInt16(MD.size());
85 // And each safe point...
86 for (GCFunctionInfo::iterator PI = MD.begin(), PE = MD.end(); PI != PE;
88 // Emit the address of the safe point.
89 OS.AddComment("safe point address");
90 MCSymbol *Label = PI->Label;
91 AP.EmitLabelPlusOffset(Label/*Hi*/, 0/*Offset*/, 4/*Size*/);
94 // Stack information never change in safe points! Only print info from the
96 GCFunctionInfo::iterator PI = MD.begin();
98 // Emit the stack frame size.
99 OS.AddComment("stack frame size (in words)");
100 AP.EmitInt16(MD.getFrameSize() / IntPtrSize);
102 // Emit stack arity, i.e. the number of stacked arguments.
103 unsigned RegisteredArgs = IntPtrSize == 4 ? 5 : 6;
104 unsigned StackArity = MD.getFunction().arg_size() > RegisteredArgs ?
105 MD.getFunction().arg_size() - RegisteredArgs : 0;
106 OS.AddComment("stack arity");
107 AP.EmitInt16(StackArity);
109 // Emit the number of live roots in the function.
110 OS.AddComment("live root count");
111 AP.EmitInt16(MD.live_size(PI));
113 // And for each live root...
114 for (GCFunctionInfo::live_iterator LI = MD.live_begin(PI),
115 LE = MD.live_end(PI);
117 // Emit live root's offset within the stack frame.
118 OS.AddComment("stack index (offset / wordsize)");
119 AP.EmitInt16(LI->StackOffset / IntPtrSize);