blob: 29c80f07bfdb76d7ca06670cabc9fae12288849e [file] [log] [blame]
Brian Gaekeb198ca32003-07-24 20:20:58 +00001//===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Brian Gaekeb198ca32003-07-24 20:20:58 +00009//
Chris Lattnerc94c8252010-01-16 21:08:46 +000010// Unified name mangler for assembly backends.
Brian Gaekeb198ca32003-07-24 20:20:58 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner45111d12010-01-16 21:57:06 +000014#include "llvm/Target/Mangler.h"
Chris Lattnerc94c8252010-01-16 21:08:46 +000015#include "llvm/GlobalValue.h"
Chris Lattnerc0dba722010-01-17 18:22:35 +000016#include "llvm/MC/MCAsmInfo.h"
Chris Lattner36e69ae2010-01-13 05:02:57 +000017#include "llvm/ADT/SmallString.h"
Chris Lattnerc94c8252010-01-16 21:08:46 +000018#include "llvm/ADT/Twine.h"
Chris Lattner5b7dfee2009-09-11 05:40:42 +000019#include "llvm/Support/raw_ostream.h"
Chris Lattner2cdd21c2003-12-14 21:35:53 +000020using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000021
Chris Lattner0e7ab8c2010-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 Kramerb357e062010-01-13 12:45:23 +000028 StringRef Name = GVName.toStringRef(TmpData);
Chris Lattner0e7ab8c2010-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 Lattnerc0dba722010-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 Lattner0e7ab8c2010-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 Lattnerc0dba722010-01-17 18:22:35 +000047 OutName.append(Prefix, Prefix+strlen(Prefix)); // Arbitrary length prefix.
Chris Lattner0e7ab8c2010-01-13 07:01:09 +000048 } else {
49 Name = Name.substr(1);
50 }
51
52 OutName.append(Name.begin(), Name.end());
53}
54
Chris Lattner5b7dfee2009-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 Lattner0e7ab8c2010-01-13 07:01:09 +000062 // If this global has a name, handle it simply.
Chris Lattner5b7dfee2009-09-11 05:40:42 +000063 if (GV->hasName()) {
Chris Lattner0e7ab8c2010-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 Lattner5b7dfee2009-09-11 05:40:42 +000069
Chris Lattner0e7ab8c2010-01-13 07:01:09 +000070 return getNameWithPrefix(OutName, GV->getName(), PrefixTy);
Chris Lattner5b7dfee2009-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 Lattnerc0dba722010-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 Lattner5b7dfee2009-09-11 05:40:42 +000082
Chris Lattnerc0dba722010-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 Lattner0e7ab8c2010-01-13 07:01:09 +000090
Chris Lattner5b7dfee2009-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 Lattner61f160a2010-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}