ErlangGCPrinter.cpp 4.2 KB

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