Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/CodeGen/AsmPrinter/OcamlGCPrinter.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- OcamlGCPrinter.cpp - Ocaml 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 printing the assembly code for an Ocaml frametable.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "llvm/ADT/STLExtras.h"
14
#include "llvm/ADT/SmallString.h"
15
#include "llvm/ADT/Twine.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/IR/DataLayout.h"
21
#include "llvm/IR/Function.h"
22
#include "llvm/IR/Mangler.h"
23
#include "llvm/IR/Module.h"
24
#include "llvm/MC/MCContext.h"
25
#include "llvm/MC/MCDirectives.h"
26
#include "llvm/MC/MCStreamer.h"
27
#include "llvm/Support/ErrorHandling.h"
28
#include "llvm/Target/TargetLoweringObjectFile.h"
29
#include <cctype>
30
#include <cstddef>
31
#include <cstdint>
32
#include <string>
33
34
using namespace llvm;
35
36
namespace {
37
38
class OcamlGCMetadataPrinter : public GCMetadataPrinter {
39
public:
40
  void beginAssembly(Module &M, GCModuleInfo &Info, AsmPrinter &AP) override;
41
  void finishAssembly(Module &M, GCModuleInfo &Info, AsmPrinter &AP) override;
42
};
43
44
} // end anonymous namespace
45
46
static GCMetadataPrinterRegistry::Add<OcamlGCMetadataPrinter>
47
    Y("ocaml", "ocaml 3.10-compatible collector");
48
49
0
void llvm::linkOcamlGCPrinter() {}
50
51
15
static void EmitCamlGlobal(const Module &M, AsmPrinter &AP, const char *Id) {
52
15
  const std::string &MId = M.getModuleIdentifier();
53
15
54
15
  std::string SymName;
55
15
  SymName += "caml";
56
15
  size_t Letter = SymName.size();
57
15
  SymName.append(MId.begin(), llvm::find(MId, '.'));
58
15
  SymName += "__";
59
15
  SymName += Id;
60
15
61
15
  // Capitalize the first letter of the module name.
62
15
  SymName[Letter] = toupper(SymName[Letter]);
63
15
64
15
  SmallString<128> TmpStr;
65
15
  Mangler::getNameWithPrefix(TmpStr, SymName, M.getDataLayout());
66
15
67
15
  MCSymbol *Sym = AP.OutContext.getOrCreateSymbol(TmpStr);
68
15
69
15
  AP.OutStreamer->EmitSymbolAttribute(Sym, MCSA_Global);
70
15
  AP.OutStreamer->EmitLabel(Sym);
71
15
}
72
73
void OcamlGCMetadataPrinter::beginAssembly(Module &M, GCModuleInfo &Info,
74
3
                                           AsmPrinter &AP) {
75
3
  AP.OutStreamer->SwitchSection(AP.getObjFileLowering().getTextSection());
76
3
  EmitCamlGlobal(M, AP, "code_begin");
77
3
78
3
  AP.OutStreamer->SwitchSection(AP.getObjFileLowering().getDataSection());
79
3
  EmitCamlGlobal(M, AP, "data_begin");
80
3
}
81
82
/// emitAssembly - Print the frametable. The ocaml frametable format is thus:
83
///
84
///   extern "C" struct align(sizeof(intptr_t)) {
85
///     uint16_t NumDescriptors;
86
///     struct align(sizeof(intptr_t)) {
87
///       void *ReturnAddress;
88
///       uint16_t FrameSize;
89
///       uint16_t NumLiveOffsets;
90
///       uint16_t LiveOffsets[NumLiveOffsets];
91
///     } Descriptors[NumDescriptors];
92
///   } caml${module}__frametable;
93
///
94
/// Note that this precludes programs from stack frames larger than 64K
95
/// (FrameSize and LiveOffsets would overflow). FrameTablePrinter will abort if
96
/// either condition is detected in a function which uses the GC.
97
///
98
void OcamlGCMetadataPrinter::finishAssembly(Module &M, GCModuleInfo &Info,
99
3
                                            AsmPrinter &AP) {
100
3
  unsigned IntPtrSize = M.getDataLayout().getPointerSize();
101
3
102
3
  AP.OutStreamer->SwitchSection(AP.getObjFileLowering().getTextSection());
103
3
  EmitCamlGlobal(M, AP, "code_end");
104
3
105
3
  AP.OutStreamer->SwitchSection(AP.getObjFileLowering().getDataSection());
106
3
  EmitCamlGlobal(M, AP, "data_end");
107
3
108
3
  // FIXME: Why does ocaml emit this??
109
3
  AP.OutStreamer->EmitIntValue(0, IntPtrSize);
110
3
111
3
  AP.OutStreamer->SwitchSection(AP.getObjFileLowering().getDataSection());
112
3
  EmitCamlGlobal(M, AP, "frametable");
113
3
114
3
  int NumDescriptors = 0;
115
3
  for (GCModuleInfo::FuncInfoVec::iterator I = Info.funcinfo_begin(),
116
3
                                           IE = Info.funcinfo_end();
117
6
       I != IE; 
++I3
) {
118
3
    GCFunctionInfo &FI = **I;
119
3
    if (FI.getStrategy().getName() != getStrategy().getName())
120
0
      // this function is managed by some other GC
121
0
      continue;
122
4
    
for (GCFunctionInfo::iterator J = FI.begin(), JE = FI.end(); 3
J != JE;
++J1
) {
123
1
      NumDescriptors++;
124
1
    }
125
3
  }
126
3
127
3
  if (NumDescriptors >= 1 << 16) {
128
0
    // Very rude!
129
0
    report_fatal_error(" Too much descriptor for ocaml GC");
130
0
  }
131
3
  AP.emitInt16(NumDescriptors);
132
3
  AP.EmitAlignment(IntPtrSize == 4 ? 
20
: 3);
133
3
134
3
  for (GCModuleInfo::FuncInfoVec::iterator I = Info.funcinfo_begin(),
135
3
                                           IE = Info.funcinfo_end();
136
6
       I != IE; 
++I3
) {
137
3
    GCFunctionInfo &FI = **I;
138
3
    if (FI.getStrategy().getName() != getStrategy().getName())
139
0
      // this function is managed by some other GC
140
0
      continue;
141
3
142
3
    uint64_t FrameSize = FI.getFrameSize();
143
3
    if (FrameSize >= 1 << 16) {
144
0
      // Very rude!
145
0
      report_fatal_error("Function '" + FI.getFunction().getName() +
146
0
                         "' is too large for the ocaml GC! "
147
0
                         "Frame size " +
148
0
                         Twine(FrameSize) + ">= 65536.\n"
149
0
                                            "(" +
150
0
                         Twine(uintptr_t(&FI)) + ")");
151
0
    }
152
3
153
3
    AP.OutStreamer->AddComment("live roots for " +
154
3
                               Twine(FI.getFunction().getName()));
155
3
    AP.OutStreamer->AddBlankLine();
156
3
157
4
    for (GCFunctionInfo::iterator J = FI.begin(), JE = FI.end(); J != JE; 
++J1
) {
158
1
      size_t LiveCount = FI.live_size(J);
159
1
      if (LiveCount >= 1 << 16) {
160
0
        // Very rude!
161
0
        report_fatal_error("Function '" + FI.getFunction().getName() +
162
0
                           "' is too large for the ocaml GC! "
163
0
                           "Live root count " +
164
0
                           Twine(LiveCount) + " >= 65536.");
165
0
      }
166
1
167
1
      AP.OutStreamer->EmitSymbolValue(J->Label, IntPtrSize);
168
1
      AP.emitInt16(FrameSize);
169
1
      AP.emitInt16(LiveCount);
170
1
171
1
      for (GCFunctionInfo::live_iterator K = FI.live_begin(J),
172
1
                                         KE = FI.live_end(J);
173
1
           K != KE; 
++K0
) {
174
0
        if (K->StackOffset >= 1 << 16) {
175
0
          // Very rude!
176
0
          report_fatal_error(
177
0
              "GC root stack offset is outside of fixed stack frame and out "
178
0
              "of range for ocaml GC!");
179
0
        }
180
0
        AP.emitInt16(K->StackOffset);
181
0
      }
182
1
183
1
      AP.EmitAlignment(IntPtrSize == 4 ? 
20
: 3);
184
1
    }
185
3
  }
186
3
}