Coverage Report

Created: 2019-07-24 05:18

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