1 //===- ErlangGCPrinter.cpp - Erlang/OTP frametable emitter ----------------===// 2 // 3 // 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 6 // 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 15 #include "llvm/BinaryFormat/ELF.h" 16 #include "llvm/CodeGen/AsmPrinter.h" 17 #include "llvm/CodeGen/GCMetadata.h" 18 #include "llvm/CodeGen/GCMetadataPrinter.h" 19 #include "llvm/IR/BuiltinGCs.h" 20 #include "llvm/IR/DataLayout.h" 21 #include "llvm/IR/Function.h" 22 #include "llvm/IR/Module.h" 23 #include "llvm/MC/MCContext.h" 24 #include "llvm/MC/MCSectionELF.h" 25 #include "llvm/MC/MCStreamer.h" 26 #include "llvm/Target/TargetLoweringObjectFile.h" 27 28 using namespace llvm; 29 30 namespace { 31 32 class ErlangGCPrinter : public GCMetadataPrinter { 33 public: 34 void finishAssembly(Module &M, GCModuleInfo &Info, AsmPrinter &AP) override; 35 }; 36 37 } // end anonymous namespace 38 39 static GCMetadataPrinterRegistry::Add<ErlangGCPrinter> 40 X("erlang", "erlang-compatible garbage collector"); 41 42 void ErlangGCPrinter::finishAssembly(Module &M, GCModuleInfo &Info, 43 AsmPrinter &AP) { 44 MCStreamer &OS = *AP.OutStreamer; 45 unsigned IntPtrSize = M.getDataLayout().getPointerSize(); 46 47 // Put this in a custom .note section. 48 OS.SwitchSection( 49 AP.getObjFileLowering().getContext().getELFSection(".note.gc", 50 ELF::SHT_PROGBITS, 0)); 51 52 // For each function... 53 for (GCModuleInfo::FuncInfoVec::iterator FI = Info.funcinfo_begin(), 54 IE = Info.funcinfo_end(); 55 FI != IE; ++FI) { 56 GCFunctionInfo &MD = **FI; 57 if (MD.getStrategy().getName() != getStrategy().getName()) 58 // this function is managed by some other GC 59 continue; 60 /** A compact GC layout. Emit this data structure: 61 * 62 * struct { 63 * int16_t PointCount; 64 * void *SafePointAddress[PointCount]; 65 * int16_t StackFrameSize; (in words) 66 * int16_t StackArity; 67 * int16_t LiveCount; 68 * int16_t LiveOffsets[LiveCount]; 69 * } __gcmap_<FUNCTIONNAME>; 70 **/ 71 72 // Align to address width. 73 AP.emitAlignment(IntPtrSize == 4 ? Align(4) : Align(8)); 74 75 // Emit PointCount. 76 OS.AddComment("safe point count"); 77 AP.emitInt16(MD.size()); 78 79 // And each safe point... 80 for (const GCPoint &P : MD) { 81 // Emit the address of the safe point. 82 OS.AddComment("safe point address"); 83 MCSymbol *Label = P.Label; 84 AP.emitLabelPlusOffset(Label /*Hi*/, 0 /*Offset*/, 4 /*Size*/); 85 } 86 87 // Stack information never change in safe points! Only print info from the 88 // first call-site. 89 GCFunctionInfo::iterator PI = MD.begin(); 90 91 // Emit the stack frame size. 92 OS.AddComment("stack frame size (in words)"); 93 AP.emitInt16(MD.getFrameSize() / IntPtrSize); 94 95 // Emit stack arity, i.e. the number of stacked arguments. 96 unsigned RegisteredArgs = IntPtrSize == 4 ? 5 : 6; 97 unsigned StackArity = MD.getFunction().arg_size() > RegisteredArgs 98 ? MD.getFunction().arg_size() - RegisteredArgs 99 : 0; 100 OS.AddComment("stack arity"); 101 AP.emitInt16(StackArity); 102 103 // Emit the number of live roots in the function. 104 OS.AddComment("live root count"); 105 AP.emitInt16(MD.live_size(PI)); 106 107 // And for each live root... 108 for (GCFunctionInfo::live_iterator LI = MD.live_begin(PI), 109 LE = MD.live_end(PI); 110 LI != LE; ++LI) { 111 // Emit live root's offset within the stack frame. 112 OS.AddComment("stack index (offset / wordsize)"); 113 AP.emitInt16(LI->StackOffset / IntPtrSize); 114 } 115 } 116 } 117 118 void llvm::linkErlangGCPrinter() {} 119