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