llvm.org GIT mirror llvm / b56c57b
Move the llvm mangler to lib/IR. This makes it available to tools that don't link with target (like llvm-ar). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@198708 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 6 years ago
43 changed file(s) with 231 addition(s) and 232 deletion(s). Raw diff Collapse all Expand all
0 //===-- llvm/IR/Mangler.h - Self-contained name mangler ---------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Unified name mangler for various backends.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_TARGET_MANGLER_H
14 #define LLVM_TARGET_MANGLER_H
15
16 #include "llvm/ADT/DenseMap.h"
17
18 namespace llvm {
19
20 class DataLayout;
21 class GlobalValue;
22 class MCContext;
23 template class SmallVectorImpl;
24 class Twine;
25
26 class Mangler {
27 public:
28 enum ManglerPrefixTy {
29 Default, ///< Emit default string before each symbol.
30 Private, ///< Emit "private" prefix before each symbol.
31 LinkerPrivate ///< Emit "linker private" prefix before each symbol.
32 };
33
34 private:
35 const DataLayout *DL;
36
37 /// AnonGlobalIDs - We need to give global values the same name every time
38 /// they are mangled. This keeps track of the number we give to anonymous
39 /// ones.
40 ///
41 DenseMap AnonGlobalIDs;
42
43 /// NextAnonGlobalID - This simple counter is used to unique value names.
44 ///
45 unsigned NextAnonGlobalID;
46
47 public:
48 Mangler(const DataLayout *DL) : DL(DL), NextAnonGlobalID(1) {}
49
50 /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
51 /// and the specified global variable's name. If the global variable doesn't
52 /// have a name, this fills in a unique name for the global.
53 void getNameWithPrefix(SmallVectorImpl &OutName, const GlobalValue *GV);
54
55 /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
56 /// and the specified name as the global variable name. GVName must not be
57 /// empty.
58 void getNameWithPrefix(SmallVectorImpl &OutName, const Twine &GVName,
59 ManglerPrefixTy PrefixTy = Mangler::Default);
60 };
61
62 } // End llvm namespace
63
64 #endif // LLVM_TARGET_MANGLER_H
1616 #include "llvm-c/lto.h"
1717 #include "llvm/ADT/OwningPtr.h"
1818 #include "llvm/ADT/StringMap.h"
19 #include "llvm/IR/Mangler.h"
1920 #include "llvm/IR/Module.h"
2021 #include "llvm/MC/MCContext.h"
2122 #include "llvm/MC/MCObjectFileInfo.h"
22 #include "llvm/Target/Mangler.h"
2323 #include "llvm/Target/TargetMachine.h"
2424 #include
2525 #include
+0
-65
include/llvm/Target/Mangler.h less more
None //===-- llvm/Target/Mangler.h - Self-contained name mangler -----*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Unified name mangler for various backends.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_TARGET_MANGLER_H
14 #define LLVM_TARGET_MANGLER_H
15
16 #include "llvm/ADT/DenseMap.h"
17
18 namespace llvm {
19
20 class DataLayout;
21 class GlobalValue;
22 class MCContext;
23 template class SmallVectorImpl;
24 class Twine;
25
26 class Mangler {
27 public:
28 enum ManglerPrefixTy {
29 Default, ///< Emit default string before each symbol.
30 Private, ///< Emit "private" prefix before each symbol.
31 LinkerPrivate ///< Emit "linker private" prefix before each symbol.
32 };
33
34 private:
35 const DataLayout *DL;
36
37 /// AnonGlobalIDs - We need to give global values the same name every time
38 /// they are mangled. This keeps track of the number we give to anonymous
39 /// ones.
40 ///
41 DenseMap AnonGlobalIDs;
42
43 /// NextAnonGlobalID - This simple counter is used to unique value names.
44 ///
45 unsigned NextAnonGlobalID;
46
47 public:
48 Mangler(const DataLayout *DL) : DL(DL), NextAnonGlobalID(1) {}
49
50 /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
51 /// and the specified global variable's name. If the global variable doesn't
52 /// have a name, this fills in a unique name for the global.
53 void getNameWithPrefix(SmallVectorImpl &OutName, const GlobalValue *GV);
54
55 /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
56 /// and the specified name as the global variable name. GVName must not be
57 /// empty.
58 void getNameWithPrefix(SmallVectorImpl &OutName, const Twine &GVName,
59 ManglerPrefixTy PrefixTy = Mangler::Default);
60 };
61
62 } // End llvm namespace
63
64 #endif // LLVM_TARGET_MANGLER_H
1919 #include "llvm/CodeGen/MachineFunction.h"
2020 #include "llvm/CodeGen/MachineModuleInfo.h"
2121 #include "llvm/IR/DataLayout.h"
22 #include "llvm/IR/Mangler.h"
2223 #include "llvm/IR/Module.h"
2324 #include "llvm/MC/MCAsmInfo.h"
2425 #include "llvm/MC/MCContext.h"
2930 #include "llvm/Support/CommandLine.h"
3031 #include "llvm/Support/Dwarf.h"
3132 #include "llvm/Support/FormattedStream.h"
32 #include "llvm/Target/Mangler.h"
3333 #include "llvm/Target/TargetFrameLowering.h"
3434 #include "llvm/Target/TargetOptions.h"
3535 #include "llvm/Target/TargetRegisterInfo.h"
2626 #include "llvm/CodeGen/MachineModuleInfo.h"
2727 #include "llvm/DebugInfo.h"
2828 #include "llvm/IR/DataLayout.h"
29 #include "llvm/IR/Mangler.h"
2930 #include "llvm/IR/Module.h"
3031 #include "llvm/IR/Operator.h"
3132 #include "llvm/IR/Writer.h"
4041 #include "llvm/Support/Format.h"
4142 #include "llvm/Support/MathExtras.h"
4243 #include "llvm/Support/Timer.h"
43 #include "llvm/Target/Mangler.h"
4444 #include "llvm/Target/TargetFrameLowering.h"
4545 #include "llvm/Target/TargetInstrInfo.h"
4646 #include "llvm/Target/TargetLowering.h"
1919 #include "llvm/CodeGen/MachineFunction.h"
2020 #include "llvm/CodeGen/MachineModuleInfo.h"
2121 #include "llvm/IR/DataLayout.h"
22 #include "llvm/IR/Mangler.h"
2223 #include "llvm/IR/Module.h"
2324 #include "llvm/MC/MCAsmInfo.h"
2425 #include "llvm/MC/MCContext.h"
3031 #include "llvm/Support/Dwarf.h"
3132 #include "llvm/Support/ErrorHandling.h"
3233 #include "llvm/Support/FormattedStream.h"
33 #include "llvm/Target/Mangler.h"
3434 #include "llvm/Target/TargetFrameLowering.h"
3535 #include "llvm/Target/TargetLoweringObjectFile.h"
3636 #include "llvm/Target/TargetMachine.h"
1919 #include "llvm/CodeGen/MachineFunction.h"
2020 #include "llvm/CodeGen/MachineModuleInfo.h"
2121 #include "llvm/IR/DataLayout.h"
22 #include "llvm/IR/Mangler.h"
2223 #include "llvm/IR/Module.h"
2324 #include "llvm/MC/MCAsmInfo.h"
2425 #include "llvm/MC/MCContext.h"
2930 #include "llvm/Support/Dwarf.h"
3031 #include "llvm/Support/ErrorHandling.h"
3132 #include "llvm/Support/FormattedStream.h"
32 #include "llvm/Target/Mangler.h"
3333 #include "llvm/Target/TargetFrameLowering.h"
3434 #include "llvm/Target/TargetLoweringObjectFile.h"
3535 #include "llvm/Target/TargetOptions.h"
2121 #include "llvm/IR/DataLayout.h"
2222 #include "llvm/IR/GlobalVariable.h"
2323 #include "llvm/IR/Instructions.h"
24 #include "llvm/IR/Mangler.h"
2425 #include "llvm/MC/MCAsmInfo.h"
2526 #include "llvm/MC/MCSection.h"
2627 #include "llvm/MC/MCStreamer.h"
2728 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Target/Mangler.h"
2929 #include "llvm/Target/TargetFrameLowering.h"
3030 #include "llvm/Target/TargetLoweringObjectFile.h"
3131 #include "llvm/Target/TargetMachine.h"
1515 #include "llvm/CodeGen/AsmPrinter.h"
1616 #include "llvm/CodeGen/GCMetadataPrinter.h"
1717 #include "llvm/IR/DataLayout.h"
18 #include "llvm/IR/Mangler.h"
1819 #include "llvm/IR/Module.h"
1920 #include "llvm/MC/MCAsmInfo.h"
2021 #include "llvm/MC/MCContext.h"
2223 #include "llvm/MC/MCSymbol.h"
2324 #include "llvm/Support/ErrorHandling.h"
2425 #include "llvm/Support/FormattedStream.h"
25 #include "llvm/Target/Mangler.h"
2626 #include "llvm/Target/TargetLoweringObjectFile.h"
2727 #include "llvm/Target/TargetMachine.h"
2828 #include
1919 #include "llvm/CodeGen/MachineFunction.h"
2020 #include "llvm/CodeGen/MachineModuleInfo.h"
2121 #include "llvm/IR/DataLayout.h"
22 #include "llvm/IR/Mangler.h"
2223 #include "llvm/IR/Module.h"
2324 #include "llvm/MC/MCAsmInfo.h"
2425 #include "llvm/MC/MCContext.h"
2930 #include "llvm/Support/Dwarf.h"
3031 #include "llvm/Support/ErrorHandling.h"
3132 #include "llvm/Support/FormattedStream.h"
32 #include "llvm/Target/Mangler.h"
3333 #include "llvm/Target/TargetFrameLowering.h"
3434 #include "llvm/Target/TargetLoweringObjectFile.h"
3535 #include "llvm/Target/TargetOptions.h"
2121 #include "llvm/IR/DerivedTypes.h"
2222 #include "llvm/IR/Function.h"
2323 #include "llvm/IR/GlobalVariable.h"
24 #include "llvm/IR/Mangler.h"
2425 #include "llvm/IR/Module.h"
2526 #include "llvm/MC/MCContext.h"
2627 #include "llvm/MC/MCExpr.h"
3334 #include "llvm/Support/ELF.h"
3435 #include "llvm/Support/ErrorHandling.h"
3536 #include "llvm/Support/raw_ostream.h"
36 #include "llvm/Target/Mangler.h"
3737 #include "llvm/Target/TargetMachine.h"
3838 using namespace llvm;
3939 using namespace dwarf;
1717 #include "llvm/IR/DataLayout.h"
1818 #include "llvm/IR/DerivedTypes.h"
1919 #include "llvm/IR/Function.h"
20 #include "llvm/IR/Mangler.h"
2021 #include "llvm/IR/Module.h"
2122 #include "llvm/MC/MCAsmInfo.h"
2223 #include "llvm/PassManager.h"
2425 #include "llvm/Support/ErrorHandling.h"
2526 #include "llvm/Support/MemoryBuffer.h"
2627 #include "llvm/Support/MutexGuard.h"
27 #include "llvm/Target/Mangler.h"
2828
2929 using namespace llvm;
3030
2525 LLVMContextImpl.cpp
2626 LeakDetector.cpp
2727 LegacyPassManager.cpp
28 Mangler.cpp
2829 Metadata.cpp
2930 Module.cpp
3031 Pass.cpp
0 //===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Unified name mangler for assembly backends.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "llvm/IR/Mangler.h"
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/ADT/Twine.h"
16 #include "llvm/IR/DataLayout.h"
17 #include "llvm/IR/DerivedTypes.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/MC/MCContext.h"
20 #include "llvm/Support/raw_ostream.h"
21 using namespace llvm;
22
23 /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
24 /// and the specified name as the global variable name. GVName must not be
25 /// empty.
26 void Mangler::getNameWithPrefix(SmallVectorImpl &OutName,
27 const Twine &GVName, ManglerPrefixTy PrefixTy) {
28 SmallString<256> TmpData;
29 StringRef Name = GVName.toStringRef(TmpData);
30 assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
31
32 // If the global name is not led with \1, add the appropriate prefixes.
33 if (Name[0] == '\1') {
34 Name = Name.substr(1);
35 } else {
36 if (PrefixTy == Mangler::Private) {
37 const char *Prefix = DL->getPrivateGlobalPrefix();
38 OutName.append(Prefix, Prefix+strlen(Prefix));
39 } else if (PrefixTy == Mangler::LinkerPrivate) {
40 const char *Prefix = DL->getLinkerPrivateGlobalPrefix();
41 OutName.append(Prefix, Prefix+strlen(Prefix));
42 }
43
44 char Prefix = DL->getGlobalPrefix();
45 if (Prefix != '\0')
46 OutName.push_back(Prefix);
47 }
48
49 // If this is a simple string that doesn't need escaping, just append it.
50 OutName.append(Name.begin(), Name.end());
51 }
52
53 /// AddFastCallStdCallSuffix - Microsoft fastcall and stdcall functions require
54 /// a suffix on their name indicating the number of words of arguments they
55 /// take.
56 static void AddFastCallStdCallSuffix(SmallVectorImpl &OutName,
57 const Function *F, const DataLayout &TD) {
58 // Calculate arguments size total.
59 unsigned ArgWords = 0;
60 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
61 AI != AE; ++AI) {
62 Type *Ty = AI->getType();
63 // 'Dereference' type in case of byval parameter attribute
64 if (AI->hasByValAttr())
65 Ty = cast(Ty)->getElementType();
66 // Size should be aligned to DWORD boundary
67 ArgWords += ((TD.getTypeAllocSize(Ty) + 3)/4)*4;
68 }
69
70 raw_svector_ostream(OutName) << '@' << ArgWords;
71 }
72
73
74 /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
75 /// and the specified global variable's name. If the global variable doesn't
76 /// have a name, this fills in a unique name for the global.
77 void Mangler::getNameWithPrefix(SmallVectorImpl &OutName,
78 const GlobalValue *GV) {
79 ManglerPrefixTy PrefixTy = Mangler::Default;
80 if (GV->hasPrivateLinkage())
81 PrefixTy = Mangler::Private;
82 else if (GV->hasLinkerPrivateLinkage() || GV->hasLinkerPrivateWeakLinkage())
83 PrefixTy = Mangler::LinkerPrivate;
84
85 // If this global has a name, handle it simply.
86 if (GV->hasName()) {
87 StringRef Name = GV->getName();
88 getNameWithPrefix(OutName, Name, PrefixTy);
89 // No need to do anything else if the global has the special "do not mangle"
90 // flag in the name.
91 if (Name[0] == 1)
92 return;
93 } else {
94 // Get the ID for the global, assigning a new one if we haven't got one
95 // already.
96 unsigned &ID = AnonGlobalIDs[GV];
97 if (ID == 0) ID = NextAnonGlobalID++;
98
99 // Must mangle the global into a unique ID.
100 getNameWithPrefix(OutName, "__unnamed_" + Twine(ID), PrefixTy);
101 }
102
103 // If we are supposed to add a microsoft-style suffix for stdcall/fastcall,
104 // add it.
105 if (DL->hasMicrosoftFastStdCallMangling()) {
106 if (const Function *F = dyn_cast(GV)) {
107 CallingConv::ID CC = F->getCallingConv();
108
109 // fastcall functions need to start with @.
110 // FIXME: This logic seems unlikely to be right.
111 if (CC == CallingConv::X86_FastCall) {
112 if (OutName[0] == '_')
113 OutName[0] = '@';
114 else
115 OutName.insert(OutName.begin(), '@');
116 }
117
118 // fastcall and stdcall functions usually need @42 at the end to specify
119 // the argument info.
120 FunctionType *FT = F->getFunctionType();
121 if ((CC == CallingConv::X86_FastCall || CC == CallingConv::X86_StdCall) &&
122 // "Pure" variadic functions do not receive @0 suffix.
123 (!FT->isVarArg() || FT->getNumParams() == 0 ||
124 (FT->getNumParams() == 1 && F->hasStructRetAttr())))
125 AddFastCallStdCallSuffix(OutName, F, *DL);
126 }
127 }
128 }
2222 #include "llvm/IR/DataLayout.h"
2323 #include "llvm/IR/DerivedTypes.h"
2424 #include "llvm/IR/LLVMContext.h"
25 #include "llvm/IR/Mangler.h"
2526 #include "llvm/IR/Module.h"
2627 #include "llvm/InitializePasses.h"
2728 #include "llvm/LTO/LTOModule.h"
4041 #include "llvm/Support/TargetSelect.h"
4142 #include "llvm/Support/ToolOutputFile.h"
4243 #include "llvm/Support/system_error.h"
43 #include "llvm/Target/Mangler.h"
4444 #include "llvm/Target/TargetLibraryInfo.h"
4545 #include "llvm/Target/TargetLowering.h"
4646 #include "llvm/Target/TargetOptions.h"
1818 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
1919 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
2020 #include "llvm/DebugInfo.h"
21 #include "llvm/IR/Mangler.h"
2122 #include "llvm/MC/MCAsmInfo.h"
2223 #include "llvm/MC/MCInst.h"
2324 #include "llvm/MC/MCSymbol.h"
2425 #include "llvm/Support/TargetRegistry.h"
25 #include "llvm/Target/Mangler.h"
2626
2727 using namespace llvm;
2828
1818 #include "llvm/ADT/SmallString.h"
1919 #include "llvm/CodeGen/AsmPrinter.h"
2020 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/IR/Mangler.h"
2122 #include "llvm/MC/MCAsmInfo.h"
2223 #include "llvm/MC/MCContext.h"
2324 #include "llvm/MC/MCExpr.h"
2425 #include "llvm/MC/MCInst.h"
25 #include "llvm/Target/Mangler.h"
2626
2727 using namespace llvm;
2828
3131 #include "llvm/DebugInfo.h"
3232 #include "llvm/IR/Constants.h"
3333 #include "llvm/IR/DataLayout.h"
34 #include "llvm/IR/Mangler.h"
3435 #include "llvm/IR/Module.h"
3536 #include "llvm/IR/Type.h"
3637 #include "llvm/IR/Writer.h"
5051 #include "llvm/Support/ErrorHandling.h"
5152 #include "llvm/Support/TargetRegistry.h"
5253 #include "llvm/Support/raw_ostream.h"
53 #include "llvm/Target/Mangler.h"
5454 #include "llvm/Target/TargetMachine.h"
5555 #include
5656 using namespace llvm;
1616 #include "MCTargetDesc/ARMMCExpr.h"
1717 #include "llvm/CodeGen/MachineBasicBlock.h"
1818 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/Mangler.h"
1920 #include "llvm/MC/MCExpr.h"
2021 #include "llvm/MC/MCInst.h"
21 #include "llvm/Target/Mangler.h"
2222 using namespace llvm;
2323
2424
99 #include "ARMTargetObjectFile.h"
1010 #include "ARMSubtarget.h"
1111 #include "llvm/ADT/StringExtras.h"
12 #include "llvm/IR/Mangler.h"
1213 #include "llvm/MC/MCContext.h"
1314 #include "llvm/MC/MCExpr.h"
1415 #include "llvm/MC/MCSectionELF.h"
1516 #include "llvm/Support/Dwarf.h"
1617 #include "llvm/Support/ELF.h"
17 #include "llvm/Target/Mangler.h"
1818 #include "llvm/Target/TargetMachine.h"
1919 using namespace llvm;
2020 using namespace dwarf;
0 add_llvm_library(LLVMTarget
1 Mangler.cpp
21 Target.cpp
32 TargetIntrinsicInfo.cpp
43 TargetJITInfo.cpp
3232 #include "llvm/IR/Constants.h"
3333 #include "llvm/IR/DataLayout.h"
3434 #include "llvm/IR/DerivedTypes.h"
35 #include "llvm/IR/Mangler.h"
3536 #include "llvm/IR/Module.h"
3637 #include "llvm/IR/Writer.h"
3738 #include "llvm/MC/MCAsmInfo.h"
4849 #include "llvm/Support/MathExtras.h"
4950 #include "llvm/Support/TargetRegistry.h"
5051 #include "llvm/Support/raw_ostream.h"
51 #include "llvm/Target/Mangler.h"
5252 #include "llvm/Target/TargetInstrInfo.h"
5353 #include "llvm/Target/TargetLoweringObjectFile.h"
5454 #include "llvm/Target/TargetOptions.h"
1717 #include "MCTargetDesc/HexagonMCInst.h"
1818 #include "llvm/CodeGen/MachineBasicBlock.h"
1919 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/Mangler.h"
2021 #include "llvm/MC/MCExpr.h"
2122 #include "llvm/MC/MCInst.h"
22 #include "llvm/Target/Mangler.h"
2323
2424 using namespace llvm;
2525
2424 #include "llvm/CodeGen/MachineModuleInfo.h"
2525 #include "llvm/IR/Constants.h"
2626 #include "llvm/IR/DerivedTypes.h"
27 #include "llvm/IR/Mangler.h"
2728 #include "llvm/IR/Module.h"
2829 #include "llvm/IR/Writer.h"
2930 #include "llvm/MC/MCAsmInfo.h"
3233 #include "llvm/MC/MCSymbol.h"
3334 #include "llvm/Support/TargetRegistry.h"
3435 #include "llvm/Support/raw_ostream.h"
35 #include "llvm/Target/Mangler.h"
3636 using namespace llvm;
3737
3838 namespace {
1717 #include "llvm/CodeGen/MachineBasicBlock.h"
1818 #include "llvm/CodeGen/MachineInstr.h"
1919 #include "llvm/IR/DataLayout.h"
20 #include "llvm/IR/Mangler.h"
2021 #include "llvm/MC/MCAsmInfo.h"
2122 #include "llvm/MC/MCContext.h"
2223 #include "llvm/MC/MCExpr.h"
2324 #include "llvm/MC/MCInst.h"
2425 #include "llvm/Support/ErrorHandling.h"
2526 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Target/Mangler.h"
2727 #include "llvm/Target/TargetMachine.h"
2828 using namespace llvm;
2929
+0
-129
lib/Target/Mangler.cpp less more
None //===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Unified name mangler for assembly backends.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "llvm/Target/Mangler.h"
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/ADT/Twine.h"
16 #include "llvm/IR/DataLayout.h"
17 #include "llvm/IR/DerivedTypes.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/MC/MCContext.h"
20 #include "llvm/Support/raw_ostream.h"
21 using namespace llvm;
22
23 /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
24 /// and the specified name as the global variable name. GVName must not be
25 /// empty.
26 void Mangler::getNameWithPrefix(SmallVectorImpl &OutName,
27 const Twine &GVName, ManglerPrefixTy PrefixTy) {
28 SmallString<256> TmpData;
29 StringRef Name = GVName.toStringRef(TmpData);
30 assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
31
32 // If the global name is not led with \1, add the appropriate prefixes.
33 if (Name[0] == '\1') {
34 Name = Name.substr(1);
35 } else {
36 if (PrefixTy == Mangler::Private) {
37 const char *Prefix = DL->getPrivateGlobalPrefix();
38 OutName.append(Prefix, Prefix+strlen(Prefix));
39 } else if (PrefixTy == Mangler::LinkerPrivate) {
40 const char *Prefix = DL->getLinkerPrivateGlobalPrefix();
41 OutName.append(Prefix, Prefix+strlen(Prefix));
42 }
43
44 char Prefix = DL->getGlobalPrefix();
45 if (Prefix != '\0')
46 OutName.push_back(Prefix);
47 }
48
49 // If this is a simple string that doesn't need escaping, just append it.
50 OutName.append(Name.begin(), Name.end());
51 }
52
53 /// AddFastCallStdCallSuffix - Microsoft fastcall and stdcall functions require
54 /// a suffix on their name indicating the number of words of arguments they
55 /// take.
56 static void AddFastCallStdCallSuffix(SmallVectorImpl &OutName,
57 const Function *F, const DataLayout &TD) {
58 // Calculate arguments size total.
59 unsigned ArgWords = 0;
60 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
61 AI != AE; ++AI) {
62 Type *Ty = AI->getType();
63 // 'Dereference' type in case of byval parameter attribute
64 if (AI->hasByValAttr())
65 Ty = cast(Ty)->getElementType();
66 // Size should be aligned to DWORD boundary
67 ArgWords += ((TD.getTypeAllocSize(Ty) + 3)/4)*4;
68 }
69
70 raw_svector_ostream(OutName) << '@' << ArgWords;
71 }
72
73
74 /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
75 /// and the specified global variable's name. If the global variable doesn't
76 /// have a name, this fills in a unique name for the global.
77 void Mangler::getNameWithPrefix(SmallVectorImpl &OutName,
78 const GlobalValue *GV) {
79 ManglerPrefixTy PrefixTy = Mangler::Default;
80 if (GV->hasPrivateLinkage())
81 PrefixTy = Mangler::Private;
82 else if (GV->hasLinkerPrivateLinkage() || GV->hasLinkerPrivateWeakLinkage())
83 PrefixTy = Mangler::LinkerPrivate;
84
85 // If this global has a name, handle it simply.
86 if (GV->hasName()) {
87 StringRef Name = GV->getName();
88 getNameWithPrefix(OutName, Name, PrefixTy);
89 // No need to do anything else if the global has the special "do not mangle"
90 // flag in the name.
91 if (Name[0] == 1)
92 return;
93 } else {
94 // Get the ID for the global, assigning a new one if we haven't got one
95 // already.
96 unsigned &ID = AnonGlobalIDs[GV];
97 if (ID == 0) ID = NextAnonGlobalID++;
98
99 // Must mangle the global into a unique ID.
100 getNameWithPrefix(OutName, "__unnamed_" + Twine(ID), PrefixTy);
101 }
102
103 // If we are supposed to add a microsoft-style suffix for stdcall/fastcall,
104 // add it.
105 if (DL->hasMicrosoftFastStdCallMangling()) {
106 if (const Function *F = dyn_cast(GV)) {
107 CallingConv::ID CC = F->getCallingConv();
108
109 // fastcall functions need to start with @.
110 // FIXME: This logic seems unlikely to be right.
111 if (CC == CallingConv::X86_FastCall) {
112 if (OutName[0] == '_')
113 OutName[0] = '@';
114 else
115 OutName.insert(OutName.begin(), '@');
116 }
117
118 // fastcall and stdcall functions usually need @42 at the end to specify
119 // the argument info.
120 FunctionType *FT = F->getFunctionType();
121 if ((CC == CallingConv::X86_FastCall || CC == CallingConv::X86_StdCall) &&
122 // "Pure" variadic functions do not receive @0 suffix.
123 (!FT->isVarArg() || FT->getNumParams() == 0 ||
124 (FT->getNumParams() == 1 && F->hasStructRetAttr())))
125 AddFastCallStdCallSuffix(OutName, F, *DL);
126 }
127 }
128 }
3131 #include "llvm/IR/DataLayout.h"
3232 #include "llvm/IR/InlineAsm.h"
3333 #include "llvm/IR/Instructions.h"
34 #include "llvm/IR/Mangler.h"
3435 #include "llvm/MC/MCAsmInfo.h"
3536 #include "llvm/MC/MCELFStreamer.h"
3637 #include "llvm/MC/MCInst.h"
3839 #include "llvm/Support/ELF.h"
3940 #include "llvm/Support/TargetRegistry.h"
4041 #include "llvm/Support/raw_ostream.h"
41 #include "llvm/Target/Mangler.h"
4242 #include "llvm/Target/TargetLoweringObjectFile.h"
4343 #include "llvm/Target/TargetOptions.h"
4444
1717 #include "llvm/CodeGen/MachineFunction.h"
1818 #include "llvm/CodeGen/MachineInstr.h"
1919 #include "llvm/CodeGen/MachineOperand.h"
20 #include "llvm/IR/Mangler.h"
2021 #include "llvm/MC/MCContext.h"
2122 #include "llvm/MC/MCExpr.h"
2223 #include "llvm/MC/MCInst.h"
23 #include "llvm/Target/Mangler.h"
2424
2525 using namespace llvm;
2626
3131 #include "llvm/IR/DerivedTypes.h"
3232 #include "llvm/IR/Function.h"
3333 #include "llvm/IR/GlobalVariable.h"
34 #include "llvm/IR/Mangler.h"
3435 #include "llvm/IR/Module.h"
3536 #include "llvm/IR/Operator.h"
3637 #include "llvm/IR/Writer.h"
4243 #include "llvm/Support/Path.h"
4344 #include "llvm/Support/TargetRegistry.h"
4445 #include "llvm/Support/TimeValue.h"
45 #include "llvm/Target/Mangler.h"
4646 #include "llvm/Target/TargetLoweringObjectFile.h"
4747 #include
4848 using namespace llvm;
2626 #include "llvm/MC/MCSymbol.h"
2727 #include "llvm/Support/CommandLine.h"
2828 #include "llvm/Support/FormattedStream.h"
29 #include "llvm/Target/Mangler.h"
3029 #include "llvm/Target/TargetMachine.h"
3130 #include
3231
3535 #include "llvm/DebugInfo.h"
3636 #include "llvm/IR/Constants.h"
3737 #include "llvm/IR/DerivedTypes.h"
38 #include "llvm/IR/Mangler.h"
3839 #include "llvm/IR/Module.h"
3940 #include "llvm/IR/Writer.h"
4041 #include "llvm/MC/MCAsmInfo.h"
5354 #include "llvm/Support/MathExtras.h"
5455 #include "llvm/Support/TargetRegistry.h"
5556 #include "llvm/Support/raw_ostream.h"
56 #include "llvm/Target/Mangler.h"
5757 #include "llvm/Target/TargetInstrInfo.h"
5858 #include "llvm/Target/TargetOptions.h"
5959 #include "llvm/Target/TargetRegisterInfo.h"
2020 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
2121 #include "llvm/IR/DataLayout.h"
2222 #include "llvm/IR/GlobalValue.h"
23 #include "llvm/IR/Mangler.h"
2324 #include "llvm/MC/MCAsmInfo.h"
2425 #include "llvm/MC/MCExpr.h"
2526 #include "llvm/MC/MCInst.h"
26 #include "llvm/Target/Mangler.h"
2727 #include "llvm/Target/TargetMachine.h"
2828 using namespace llvm;
2929
77 //===----------------------------------------------------------------------===//
88
99 #include "PPCTargetObjectFile.h"
10 #include "llvm/IR/Mangler.h"
1011 #include "llvm/MC/MCContext.h"
1112 #include "llvm/MC/MCExpr.h"
1213 #include "llvm/MC/MCSectionELF.h"
13 #include "llvm/Target/Mangler.h"
1414
1515 using namespace llvm;
1616
2323 #include "llvm/CodeGen/AsmPrinter.h"
2424 #include "llvm/CodeGen/MachineInstr.h"
2525 #include "llvm/CodeGen/MachineRegisterInfo.h"
26 #include "llvm/IR/Mangler.h"
2627 #include "llvm/MC/MCAsmInfo.h"
2728 #include "llvm/MC/MCContext.h"
2829 #include "llvm/MC/MCInst.h"
3031 #include "llvm/MC/MCSymbol.h"
3132 #include "llvm/Support/TargetRegistry.h"
3233 #include "llvm/Support/raw_ostream.h"
33 #include "llvm/Target/Mangler.h"
3434 using namespace llvm;
3535
3636 namespace {
1919 #include "llvm/CodeGen/MachineFunction.h"
2020 #include "llvm/CodeGen/MachineInstr.h"
2121 #include "llvm/CodeGen/MachineOperand.h"
22 #include "llvm/IR/Mangler.h"
2223 #include "llvm/MC/MCAsmInfo.h"
2324 #include "llvm/MC/MCContext.h"
2425 #include "llvm/MC/MCExpr.h"
2526 #include "llvm/MC/MCInst.h"
26 #include "llvm/Target/Mangler.h"
2727
2828 using namespace llvm;
2929
1717 #include "SystemZMCInstLower.h"
1818 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
1919 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
20 #include "llvm/IR/Mangler.h"
2021 #include "llvm/MC/MCExpr.h"
2122 #include "llvm/MC/MCInstBuilder.h"
2223 #include "llvm/MC/MCStreamer.h"
2324 #include "llvm/Support/TargetRegistry.h"
24 #include "llvm/Target/Mangler.h"
2525
2626 using namespace llvm;
2727
88
99 #include "SystemZMCInstLower.h"
1010 #include "SystemZAsmPrinter.h"
11 #include "llvm/IR/Mangler.h"
1112 #include "llvm/MC/MCExpr.h"
1213 #include "llvm/MC/MCStreamer.h"
13 #include "llvm/Target/Mangler.h"
1414
1515 using namespace llvm;
1616
1717 #include "llvm/IR/DerivedTypes.h"
1818 #include "llvm/IR/Function.h"
1919 #include "llvm/IR/GlobalVariable.h"
20 #include "llvm/IR/Mangler.h"
2021 #include "llvm/MC/MCAsmInfo.h"
2122 #include "llvm/MC/MCContext.h"
2223 #include "llvm/MC/MCExpr.h"
2526 #include "llvm/Support/Dwarf.h"
2627 #include "llvm/Support/ErrorHandling.h"
2728 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/Target/Mangler.h"
2929 #include "llvm/Target/TargetMachine.h"
3030 #include "llvm/Target/TargetOptions.h"
3131 using namespace llvm;
2424 #include "llvm/DebugInfo.h"
2525 #include "llvm/IR/CallingConv.h"
2626 #include "llvm/IR/DerivedTypes.h"
27 #include "llvm/IR/Mangler.h"
2728 #include "llvm/IR/Module.h"
2829 #include "llvm/IR/Type.h"
2930 #include "llvm/IR/Writer.h"
3738 #include "llvm/Support/Debug.h"
3839 #include "llvm/Support/ErrorHandling.h"
3940 #include "llvm/Support/TargetRegistry.h"
40 #include "llvm/Target/Mangler.h"
4141 #include "llvm/Target/TargetOptions.h"
4242 using namespace llvm;
4343
1717 #include "llvm/ADT/SmallString.h"
1818 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
1919 #include "llvm/CodeGen/StackMaps.h"
20 #include "llvm/IR/Mangler.h"
2021 #include "llvm/IR/Type.h"
2122 #include "llvm/MC/MCAsmInfo.h"
2223 #include "llvm/MC/MCContext.h"
2627 #include "llvm/MC/MCStreamer.h"
2728 #include "llvm/MC/MCSymbol.h"
2829 #include "llvm/Support/FormattedStream.h"
29 #include "llvm/Target/Mangler.h"
3030 using namespace llvm;
3131
3232 namespace {
1010 #include "llvm/MC/MCContext.h"
1111 #include "llvm/MC/MCExpr.h"
1212 #include "llvm/MC/MCSectionELF.h"
13 #include "llvm/IR/Mangler.h"
1314 #include "llvm/Support/Dwarf.h"
14 #include "llvm/Target/Mangler.h"
1515
1616 using namespace llvm;
1717 using namespace dwarf;
3030 #include "llvm/IR/Constants.h"
3131 #include "llvm/IR/DataLayout.h"
3232 #include "llvm/IR/DerivedTypes.h"
33 #include "llvm/IR/Mangler.h"
3334 #include "llvm/IR/Module.h"
3435 #include "llvm/MC/MCAsmInfo.h"
3536 #include "llvm/MC/MCExpr.h"
3940 #include "llvm/Support/ErrorHandling.h"
4041 #include "llvm/Support/TargetRegistry.h"
4142 #include "llvm/Support/raw_ostream.h"
42 #include "llvm/Target/Mangler.h"
4343 #include "llvm/Target/TargetLoweringObjectFile.h"
4444 #include
4545 #include
1616 #include "llvm/CodeGen/MachineFunction.h"
1717 #include "llvm/CodeGen/MachineInstr.h"
1818 #include "llvm/CodeGen/MachineOperand.h"
19 #include "llvm/IR/Mangler.h"
1920 #include "llvm/MC/MCContext.h"
2021 #include "llvm/MC/MCExpr.h"
2122 #include "llvm/MC/MCInst.h"
22 #include "llvm/Target/Mangler.h"
2323
2424 using namespace llvm;
2525