blob: 29c80f07bfdb76d7ca06670cabc9fae12288849e [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner52145182010-01-16 21:08:46 +000010// Unified name mangler for assembly backends.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner31a54742010-01-16 21:57:06 +000014#include "llvm/Target/Mangler.h"
Chris Lattner52145182010-01-16 21:08:46 +000015#include "llvm/GlobalValue.h"
Chris Lattner63ab9bb2010-01-17 18:22:35 +000016#include "llvm/MC/MCAsmInfo.h"
Chris Lattner83411fe2010-01-13 05:02:57 +000017#include "llvm/ADT/SmallString.h"
Chris Lattner52145182010-01-16 21:08:46 +000018#include "llvm/ADT/Twine.h"
Chris Lattnerbd7e1102009-09-11 05:40:42 +000019#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020using namespace llvm;
21
Chris Lattnercc3404e2010-01-13 07:01:09 +000022/// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
23/// and the specified name as the global variable name. GVName must not be
24/// empty.
25void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
26 const Twine &GVName, ManglerPrefixTy PrefixTy) {
27 SmallString<256> TmpData;
Benjamin Kramere05acd12010-01-13 12:45:23 +000028 StringRef Name = GVName.toStringRef(TmpData);
Chris Lattnercc3404e2010-01-13 07:01:09 +000029 assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
30
31 // If the global name is not led with \1, add the appropriate prefixes.
32 if (Name[0] != '\1') {
Chris Lattner63ab9bb2010-01-17 18:22:35 +000033 if (PrefixTy == Mangler::Private) {
34 const char *Prefix = MAI.getPrivateGlobalPrefix();
35 OutName.append(Prefix, Prefix+strlen(Prefix));
36 } else if (PrefixTy == Mangler::LinkerPrivate) {
37 const char *Prefix = MAI.getLinkerPrivateGlobalPrefix();
38 OutName.append(Prefix, Prefix+strlen(Prefix));
39 }
40
41 const char *Prefix = MAI.getGlobalPrefix();
Chris Lattnercc3404e2010-01-13 07:01:09 +000042 if (Prefix[0] == 0)
43 ; // Common noop, no prefix.
44 else if (Prefix[1] == 0)
45 OutName.push_back(Prefix[0]); // Common, one character prefix.
46 else
Chris Lattner63ab9bb2010-01-17 18:22:35 +000047 OutName.append(Prefix, Prefix+strlen(Prefix)); // Arbitrary length prefix.
Chris Lattnercc3404e2010-01-13 07:01:09 +000048 } else {
49 Name = Name.substr(1);
50 }
51
52 OutName.append(Name.begin(), Name.end());
53}
54
Chris Lattnerbd7e1102009-09-11 05:40:42 +000055
56/// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
57/// and the specified global variable's name. If the global variable doesn't
58/// have a name, this fills in a unique name for the global.
59void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
60 const GlobalValue *GV,
61 bool isImplicitlyPrivate) {
Chris Lattnercc3404e2010-01-13 07:01:09 +000062 // If this global has a name, handle it simply.
Chris Lattnerbd7e1102009-09-11 05:40:42 +000063 if (GV->hasName()) {
Chris Lattnercc3404e2010-01-13 07:01:09 +000064 ManglerPrefixTy PrefixTy = Mangler::Default;
65 if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
66 PrefixTy = Mangler::Private;
67 else if (GV->hasLinkerPrivateLinkage())
68 PrefixTy = Mangler::LinkerPrivate;
Chris Lattnerbd7e1102009-09-11 05:40:42 +000069
Chris Lattnercc3404e2010-01-13 07:01:09 +000070 return getNameWithPrefix(OutName, GV->getName(), PrefixTy);
Chris Lattnerbd7e1102009-09-11 05:40:42 +000071 }
72
73 // If the global variable doesn't have a name, return a unique name for the
74 // global based on a numbering.
Chris Lattner63ab9bb2010-01-17 18:22:35 +000075 if (GV->hasPrivateLinkage() || isImplicitlyPrivate) {
76 const char *Prefix = MAI.getPrivateGlobalPrefix();
77 OutName.append(Prefix, Prefix+strlen(Prefix));
78 } else if (GV->hasLinkerPrivateLinkage()) {
79 const char *Prefix = MAI.getLinkerPrivateGlobalPrefix();
80 OutName.append(Prefix, Prefix+strlen(Prefix));
81 }
Chris Lattnerbd7e1102009-09-11 05:40:42 +000082
Chris Lattner63ab9bb2010-01-17 18:22:35 +000083 const char *Prefix = MAI.getGlobalPrefix();
84 if (Prefix[0] == 0)
85 ; // Common noop, no prefix.
86 else if (Prefix[1] == 0)
87 OutName.push_back(Prefix[0]); // Common, one character prefix.
88 else
89 OutName.append(Prefix, Prefix+strlen(Prefix)); // Arbitrary length prefix.
Chris Lattnercc3404e2010-01-13 07:01:09 +000090
Chris Lattnerbd7e1102009-09-11 05:40:42 +000091 // Get the ID for the global, assigning a new one if we haven't got one
92 // already.
93 unsigned &ID = AnonGlobalIDs[GV];
94 if (ID == 0) ID = NextAnonGlobalID++;
95
96 // Must mangle the global into a unique ID.
97 raw_svector_ostream(OutName) << "__unnamed_" << ID;
98}
99
Chris Lattnerbfd97272010-01-16 18:06:34 +0000100/// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
101/// and the specified global variable's name. If the global variable doesn't
102/// have a name, this fills in a unique name for the global.
103std::string Mangler::getNameWithPrefix(const GlobalValue *GV,
104 bool isImplicitlyPrivate) {
105 SmallString<64> Buf;
106 getNameWithPrefix(Buf, GV, isImplicitlyPrivate);
107 return std::string(Buf.begin(), Buf.end());
108}