Coverage Report

Created: 2017-10-03 07:32

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/lib/IR/Mangler.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
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
// Unified name mangler for assembly backends.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "llvm/IR/Mangler.h"
15
#include "llvm/ADT/SmallString.h"
16
#include "llvm/ADT/Triple.h"
17
#include "llvm/ADT/Twine.h"
18
#include "llvm/IR/DataLayout.h"
19
#include "llvm/IR/DerivedTypes.h"
20
#include "llvm/IR/Function.h"
21
#include "llvm/IR/Module.h"
22
#include "llvm/Support/raw_ostream.h"
23
using namespace llvm;
24
25
namespace {
26
enum ManglerPrefixTy {
27
  Default,      ///< Emit default string before each symbol.
28
  Private,      ///< Emit "private" prefix before each symbol.
29
  LinkerPrivate ///< Emit "linker private" prefix before each symbol.
30
};
31
}
32
33
static void getNameWithPrefixImpl(raw_ostream &OS, const Twine &GVName,
34
                                  ManglerPrefixTy PrefixTy,
35
6.91M
                                  const DataLayout &DL, char Prefix) {
36
6.91M
  SmallString<256> TmpData;
37
6.91M
  StringRef Name = GVName.toStringRef(TmpData);
38
6.91M
  assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
39
6.91M
40
6.91M
  // No need to do anything special if the global has the special "do not
41
6.91M
  // mangle" flag in the name.
42
6.91M
  if (
Name[0] == '\1'6.91M
) {
43
10.9k
    OS << Name.substr(1);
44
10.9k
    return;
45
10.9k
  }
46
6.90M
47
6.90M
  
if (6.90M
PrefixTy == Private6.90M
)
48
52.4k
    OS << DL.getPrivateGlobalPrefix();
49
6.84M
  else 
if (6.84M
PrefixTy == LinkerPrivate6.84M
)
50
2.87M
    OS << DL.getLinkerPrivateGlobalPrefix();
51
6.90M
52
6.90M
  if (Prefix != '\0')
53
6.69M
    OS << Prefix;
54
6.91M
55
6.91M
  // If this is a simple string that doesn't need escaping, just append it.
56
6.91M
  OS << Name;
57
6.91M
}
58
59
static void getNameWithPrefixImpl(raw_ostream &OS, const Twine &GVName,
60
                                  const DataLayout &DL,
61
17.6k
                                  ManglerPrefixTy PrefixTy) {
62
17.6k
  char Prefix = DL.getGlobalPrefix();
63
17.6k
  return getNameWithPrefixImpl(OS, GVName, PrefixTy, DL, Prefix);
64
17.6k
}
65
66
void Mangler::getNameWithPrefix(raw_ostream &OS, const Twine &GVName,
67
649
                                const DataLayout &DL) {
68
649
  return getNameWithPrefixImpl(OS, GVName, DL, Default);
69
649
}
70
71
void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
72
46.0k
                                const Twine &GVName, const DataLayout &DL) {
73
46.0k
  raw_svector_ostream OS(OutName);
74
46.0k
  char Prefix = DL.getGlobalPrefix();
75
46.0k
  return getNameWithPrefixImpl(OS, GVName, Default, DL, Prefix);
76
46.0k
}
77
78
1.79k
static bool hasByteCountSuffix(CallingConv::ID CC) {
79
1.79k
  switch (CC) {
80
199
  case CallingConv::X86_FastCall:
81
199
  case CallingConv::X86_StdCall:
82
199
  case CallingConv::X86_VectorCall:
83
199
    return true;
84
1.59k
  default:
85
1.59k
    return false;
86
0
  }
87
0
}
88
89
/// Microsoft fastcall and stdcall functions require a suffix on their name
90
/// indicating the number of words of arguments they take.
91
static void addByteCountSuffix(raw_ostream &OS, const Function *F,
92
194
                               const DataLayout &DL) {
93
194
  // Calculate arguments size total.
94
194
  unsigned ArgWords = 0;
95
194
  for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
96
455
       
AI != AE455
;
++AI261
) {
97
261
    Type *Ty = AI->getType();
98
261
    // 'Dereference' type in case of byval or inalloca parameter attribute.
99
261
    if (AI->hasByValOrInAllocaAttr())
100
9
      Ty = cast<PointerType>(Ty)->getElementType();
101
261
    // Size should be aligned to pointer size.
102
261
    unsigned PtrSize = DL.getPointerSize();
103
261
    ArgWords += alignTo(DL.getTypeAllocSize(Ty), PtrSize);
104
261
  }
105
194
106
194
  OS << '@' << ArgWords;
107
194
}
108
109
void Mangler::getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV,
110
6.86M
                                bool CannotUsePrivateLabel) const {
111
6.86M
  ManglerPrefixTy PrefixTy = Default;
112
6.86M
  if (
GV->hasPrivateLinkage()6.86M
) {
113
2.92M
    if (CannotUsePrivateLabel)
114
2.87M
      PrefixTy = LinkerPrivate;
115
2.92M
    else
116
52.4k
      PrefixTy = Private;
117
2.92M
  }
118
6.86M
119
6.86M
  const DataLayout &DL = GV->getParent()->getDataLayout();
120
6.86M
  if (
!GV->hasName()6.86M
) {
121
16.9k
    // Get the ID for the global, assigning a new one if we haven't got one
122
16.9k
    // already.
123
16.9k
    unsigned &ID = AnonGlobalIDs[GV];
124
16.9k
    if (ID == 0)
125
5.71k
      ID = AnonGlobalIDs.size();
126
16.9k
127
16.9k
    // Must mangle the global into a unique ID.
128
16.9k
    getNameWithPrefixImpl(OS, "__unnamed_" + Twine(ID), DL, PrefixTy);
129
16.9k
    return;
130
16.9k
  }
131
6.84M
132
6.84M
  StringRef Name = GV->getName();
133
6.84M
  char Prefix = DL.getGlobalPrefix();
134
6.84M
135
6.84M
  // Mangle functions with Microsoft calling conventions specially.  Only do
136
6.84M
  // this mangling for x86_64 vectorcall and 32-bit x86.
137
6.84M
  const Function *MSFunc = dyn_cast<Function>(GV);
138
6.84M
  if (Name.startswith("\01"))
139
10.9k
    MSFunc = nullptr; // Don't mangle when \01 is present.
140
6.84M
  CallingConv::ID CC =
141
6.84M
      MSFunc ? 
MSFunc->getCallingConv()2.83M
:
(unsigned)CallingConv::C4.01M
;
142
6.84M
  if (!DL.hasMicrosoftFastStdCallMangling() &&
143
6.84M
      CC != CallingConv::X86_VectorCall)
144
6.84M
    MSFunc = nullptr;
145
6.84M
  if (
MSFunc6.84M
) {
146
1.79k
    if (CC == CallingConv::X86_FastCall)
147
105
      Prefix = '@'; // fastcall functions have an @ prefix instead of _.
148
1.68k
    else 
if (1.68k
CC == CallingConv::X86_VectorCall1.68k
)
149
56
      Prefix = '\0'; // vectorcall functions have no prefix.
150
1.79k
  }
151
6.84M
152
6.84M
  getNameWithPrefixImpl(OS, Name, PrefixTy, DL, Prefix);
153
6.84M
154
6.84M
  if (!MSFunc)
155
6.84M
    return;
156
1.78k
157
1.78k
  // If we are supposed to add a microsoft-style suffix for stdcall, fastcall,
158
1.78k
  // or vectorcall, add it.  These functions have a suffix of @N where N is the
159
1.78k
  // cumulative byte size of all of the parameters to the function in decimal.
160
1.78k
  
if (1.78k
CC == CallingConv::X86_VectorCall1.78k
)
161
56
    OS << '@'; // vectorcall functions use a double @ suffix.
162
1.78k
  FunctionType *FT = MSFunc->getFunctionType();
163
1.78k
  if (hasByteCountSuffix(CC) &&
164
1.78k
      // "Pure" variadic functions do not receive @0 suffix.
165
199
      
(!FT->isVarArg() || 199
FT->getNumParams() == 018
||
166
5
       
(FT->getNumParams() == 1 && 5
MSFunc->hasStructRetAttr()4
)))
167
194
    addByteCountSuffix(OS, MSFunc, DL);
168
6.86M
}
169
170
void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
171
                                const GlobalValue *GV,
172
6.85M
                                bool CannotUsePrivateLabel) const {
173
6.85M
  raw_svector_ostream OS(OutName);
174
6.85M
  getNameWithPrefix(OS, GV, CannotUsePrivateLabel);
175
6.85M
}
176
177
void llvm::emitLinkerFlagsForGlobalCOFF(raw_ostream &OS, const GlobalValue *GV,
178
4.62k
                                        const Triple &TT, Mangler &Mangler) {
179
4.62k
  if (
!GV->hasDLLExportStorageClass() || 4.62k
GV->isDeclaration()266
)
180
4.37k
    return;
181
247
182
247
  
if (247
TT.isKnownWindowsMSVCEnvironment()247
)
183
104
    OS << " /EXPORT:";
184
247
  else
185
143
    OS << " -export:";
186
247
187
247
  if (
TT.isWindowsGNUEnvironment() || 247
TT.isWindowsCygwinEnvironment()153
) {
188
128
    std::string Flag;
189
128
    raw_string_ostream FlagOS(Flag);
190
128
    Mangler.getNameWithPrefix(FlagOS, GV, false);
191
128
    FlagOS.flush();
192
128
    if (Flag[0] == GV->getParent()->getDataLayout().getGlobalPrefix())
193
64
      OS << Flag.substr(1);
194
128
    else
195
64
      OS << Flag;
196
247
  } else {
197
119
    Mangler.getNameWithPrefix(OS, GV, false);
198
119
  }
199
247
200
247
  if (
!GV->getValueType()->isFunctionTy()247
) {
201
81
    if (TT.isKnownWindowsMSVCEnvironment())
202
34
      OS << ",DATA";
203
81
    else
204
47
      OS << ",data";
205
81
  }
206
4.62k
}