ErlangGCPrinter.cpp 4.1 KB

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