ErlangGCPrinter.cpp 4.0 KB

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