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