ErlangGCPrinter.cpp 4.2 KB

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