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