Eugene Zelenko | d96089b | 2017-02-14 00:33:36 +0000 | [diff] [blame] | 1 | //===- ErlangGCPrinter.cpp - Erlang/OTP frametable emitter ----------------===// |
Yiannis Tsiouris | dbb4adf | 2013-03-25 13:47:46 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Yiannis Tsiouris | dbb4adf | 2013-03-25 13:47:46 +0000 | [diff] [blame] | 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 | |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 15 | #include "llvm/BinaryFormat/ELF.h" |
Yiannis Tsiouris | dbb4adf | 2013-03-25 13:47:46 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/AsmPrinter.h" |
Philip Reames | 9b8c102 | 2018-11-10 16:08:10 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/BuiltinGCs.h" |
Eugene Zelenko | d96089b | 2017-02-14 00:33:36 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/GCMetadata.h" |
Yiannis Tsiouris | dbb4adf | 2013-03-25 13:47:46 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/GCMetadataPrinter.h" |
Eugene Zelenko | d96089b | 2017-02-14 00:33:36 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/GCStrategy.h" |
Yiannis Tsiouris | dbb4adf | 2013-03-25 13:47:46 +0000 | [diff] [blame] | 21 | #include "llvm/IR/DataLayout.h" |
| 22 | #include "llvm/IR/Function.h" |
Eugene Zelenko | d96089b | 2017-02-14 00:33:36 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Module.h" |
Yiannis Tsiouris | dbb4adf | 2013-03-25 13:47:46 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCContext.h" |
| 25 | #include "llvm/MC/MCSectionELF.h" |
| 26 | #include "llvm/MC/MCStreamer.h" |
| 27 | #include "llvm/MC/MCSymbol.h" |
David Blaikie | 6054e65 | 2018-03-23 23:58:19 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Yiannis Tsiouris | dbb4adf | 2013-03-25 13:47:46 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace llvm; |
| 31 | |
| 32 | namespace { |
| 33 | |
Philip Reames | 3631953 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 34 | class ErlangGCPrinter : public GCMetadataPrinter { |
| 35 | public: |
| 36 | void finishAssembly(Module &M, GCModuleInfo &Info, AsmPrinter &AP) override; |
| 37 | }; |
Eugene Zelenko | d96089b | 2017-02-14 00:33:36 +0000 | [diff] [blame] | 38 | |
| 39 | } // end anonymous namespace |
Yiannis Tsiouris | dbb4adf | 2013-03-25 13:47:46 +0000 | [diff] [blame] | 40 | |
| 41 | static GCMetadataPrinterRegistry::Add<ErlangGCPrinter> |
Philip Reames | 3631953 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 42 | X("erlang", "erlang-compatible garbage collector"); |
Yiannis Tsiouris | dbb4adf | 2013-03-25 13:47:46 +0000 | [diff] [blame] | 43 | |
Philip Reames | 1e30897 | 2014-12-11 01:47:23 +0000 | [diff] [blame] | 44 | void ErlangGCPrinter::finishAssembly(Module &M, GCModuleInfo &Info, |
| 45 | AsmPrinter &AP) { |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 46 | MCStreamer &OS = *AP.OutStreamer; |
Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 47 | unsigned IntPtrSize = M.getDataLayout().getPointerSize(); |
Yiannis Tsiouris | dbb4adf | 2013-03-25 13:47:46 +0000 | [diff] [blame] | 48 | |
| 49 | // Put this in a custom .note section. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 50 | OS.SwitchSection( |
Rafael Espindola | ba31e27 | 2015-01-29 17:33:21 +0000 | [diff] [blame] | 51 | AP.getObjFileLowering().getContext().getELFSection(".note.gc", |
| 52 | ELF::SHT_PROGBITS, 0)); |
Yiannis Tsiouris | dbb4adf | 2013-03-25 13:47:46 +0000 | [diff] [blame] | 53 | |
| 54 | // For each function... |
Philip Reames | 1e30897 | 2014-12-11 01:47:23 +0000 | [diff] [blame] | 55 | for (GCModuleInfo::FuncInfoVec::iterator FI = Info.funcinfo_begin(), |
Philip Reames | 3631953 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 56 | IE = Info.funcinfo_end(); |
Philip Reames | 1e30897 | 2014-12-11 01:47:23 +0000 | [diff] [blame] | 57 | FI != IE; ++FI) { |
Yiannis Tsiouris | dbb4adf | 2013-03-25 13:47:46 +0000 | [diff] [blame] | 58 | GCFunctionInfo &MD = **FI; |
Philip Reames | 1e30897 | 2014-12-11 01:47:23 +0000 | [diff] [blame] | 59 | if (MD.getStrategy().getName() != getStrategy().getName()) |
| 60 | // this function is managed by some other GC |
| 61 | continue; |
Yiannis Tsiouris | dbb4adf | 2013-03-25 13:47:46 +0000 | [diff] [blame] | 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. |
Guillaume Chatelet | 18f805a | 2019-09-27 12:54:21 +0000 | [diff] [blame] | 75 | AP.EmitAlignment(IntPtrSize == 4 ? Align(4) : Align(8)); |
Yiannis Tsiouris | dbb4adf | 2013-03-25 13:47:46 +0000 | [diff] [blame] | 76 | |
| 77 | // Emit PointCount. |
| 78 | OS.AddComment("safe point count"); |
Rafael Espindola | 4b4d85f | 2018-03-29 23:32:54 +0000 | [diff] [blame] | 79 | AP.emitInt16(MD.size()); |
Yiannis Tsiouris | dbb4adf | 2013-03-25 13:47:46 +0000 | [diff] [blame] | 80 | |
| 81 | // And each safe point... |
| 82 | for (GCFunctionInfo::iterator PI = MD.begin(), PE = MD.end(); PI != PE; |
| 83 | ++PI) { |
| 84 | // Emit the address of the safe point. |
| 85 | OS.AddComment("safe point address"); |
| 86 | MCSymbol *Label = PI->Label; |
Fangrui Song | 0bc77a0 | 2020-02-13 13:26:21 -0800 | [diff] [blame] | 87 | AP.emitLabelPlusOffset(Label /*Hi*/, 0 /*Offset*/, 4 /*Size*/); |
Yiannis Tsiouris | dbb4adf | 2013-03-25 13:47:46 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | // Stack information never change in safe points! Only print info from the |
| 91 | // first call-site. |
| 92 | GCFunctionInfo::iterator PI = MD.begin(); |
| 93 | |
| 94 | // Emit the stack frame size. |
| 95 | OS.AddComment("stack frame size (in words)"); |
Rafael Espindola | 4b4d85f | 2018-03-29 23:32:54 +0000 | [diff] [blame] | 96 | AP.emitInt16(MD.getFrameSize() / IntPtrSize); |
Yiannis Tsiouris | dbb4adf | 2013-03-25 13:47:46 +0000 | [diff] [blame] | 97 | |
| 98 | // Emit stack arity, i.e. the number of stacked arguments. |
| 99 | unsigned RegisteredArgs = IntPtrSize == 4 ? 5 : 6; |
Philip Reames | 3631953 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 100 | unsigned StackArity = MD.getFunction().arg_size() > RegisteredArgs |
| 101 | ? MD.getFunction().arg_size() - RegisteredArgs |
| 102 | : 0; |
Yiannis Tsiouris | dbb4adf | 2013-03-25 13:47:46 +0000 | [diff] [blame] | 103 | OS.AddComment("stack arity"); |
Rafael Espindola | 4b4d85f | 2018-03-29 23:32:54 +0000 | [diff] [blame] | 104 | AP.emitInt16(StackArity); |
Yiannis Tsiouris | dbb4adf | 2013-03-25 13:47:46 +0000 | [diff] [blame] | 105 | |
| 106 | // Emit the number of live roots in the function. |
| 107 | OS.AddComment("live root count"); |
Rafael Espindola | 4b4d85f | 2018-03-29 23:32:54 +0000 | [diff] [blame] | 108 | AP.emitInt16(MD.live_size(PI)); |
Yiannis Tsiouris | dbb4adf | 2013-03-25 13:47:46 +0000 | [diff] [blame] | 109 | |
| 110 | // And for each live root... |
| 111 | for (GCFunctionInfo::live_iterator LI = MD.live_begin(PI), |
| 112 | LE = MD.live_end(PI); |
Philip Reames | 3631953 | 2015-01-16 23:16:12 +0000 | [diff] [blame] | 113 | LI != LE; ++LI) { |
Yiannis Tsiouris | dbb4adf | 2013-03-25 13:47:46 +0000 | [diff] [blame] | 114 | // Emit live root's offset within the stack frame. |
| 115 | OS.AddComment("stack index (offset / wordsize)"); |
Rafael Espindola | 4b4d85f | 2018-03-29 23:32:54 +0000 | [diff] [blame] | 116 | AP.emitInt16(LI->StackOffset / IntPtrSize); |
Yiannis Tsiouris | dbb4adf | 2013-03-25 13:47:46 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | } |
Eugene Zelenko | d96089b | 2017-02-14 00:33:36 +0000 | [diff] [blame] | 120 | |
| 121 | void llvm::linkErlangGCPrinter() {} |