blob: 5579bfc5d9d4b9ac4a97245b9b57556d54d9d806 [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 Lattner2cdd21c2003-12-14 21:35:53 +000019using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000020
Chris Lattner0e7ab8c2010-01-13 07:01:09 +000021/// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
22/// and the specified name as the global variable name. GVName must not be
23/// empty.
24void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
25 const Twine &GVName, ManglerPrefixTy PrefixTy) {
26 SmallString<256> TmpData;
Benjamin Kramerb357e062010-01-13 12:45:23 +000027 StringRef Name = GVName.toStringRef(TmpData);
Chris Lattner0e7ab8c2010-01-13 07:01:09 +000028 assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
29
30 // If the global name is not led with \1, add the appropriate prefixes.
31 if (Name[0] != '\1') {
Chris Lattnerc0dba722010-01-17 18:22:35 +000032 if (PrefixTy == Mangler::Private) {
33 const char *Prefix = MAI.getPrivateGlobalPrefix();
34 OutName.append(Prefix, Prefix+strlen(Prefix));
35 } else if (PrefixTy == Mangler::LinkerPrivate) {
36 const char *Prefix = MAI.getLinkerPrivateGlobalPrefix();
37 OutName.append(Prefix, Prefix+strlen(Prefix));
38 }
39
40 const char *Prefix = MAI.getGlobalPrefix();
Chris Lattner0e7ab8c2010-01-13 07:01:09 +000041 if (Prefix[0] == 0)
42 ; // Common noop, no prefix.
43 else if (Prefix[1] == 0)
44 OutName.push_back(Prefix[0]); // Common, one character prefix.
45 else
Chris Lattnerc0dba722010-01-17 18:22:35 +000046 OutName.append(Prefix, Prefix+strlen(Prefix)); // Arbitrary length prefix.
Chris Lattner0e7ab8c2010-01-13 07:01:09 +000047 } else {
48 Name = Name.substr(1);
49 }
50
51 OutName.append(Name.begin(), Name.end());
52}
53
Chris Lattner5b7dfee2009-09-11 05:40:42 +000054
55/// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
56/// and the specified global variable's name. If the global variable doesn't
57/// have a name, this fills in a unique name for the global.
58void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
59 const GlobalValue *GV,
60 bool isImplicitlyPrivate) {
Chris Lattnerff240052010-01-17 18:52:16 +000061 ManglerPrefixTy PrefixTy = Mangler::Default;
62 if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
63 PrefixTy = Mangler::Private;
64 else if (GV->hasLinkerPrivateLinkage())
65 PrefixTy = Mangler::LinkerPrivate;
66
Chris Lattner0e7ab8c2010-01-13 07:01:09 +000067 // If this global has a name, handle it simply.
Chris Lattnerff240052010-01-17 18:52:16 +000068 if (GV->hasName())
Chris Lattner0e7ab8c2010-01-13 07:01:09 +000069 return getNameWithPrefix(OutName, GV->getName(), PrefixTy);
Chris Lattner0e7ab8c2010-01-13 07:01:09 +000070
Chris Lattner5b7dfee2009-09-11 05:40:42 +000071 // Get the ID for the global, assigning a new one if we haven't got one
72 // already.
73 unsigned &ID = AnonGlobalIDs[GV];
74 if (ID == 0) ID = NextAnonGlobalID++;
75
76 // Must mangle the global into a unique ID.
Chris Lattnerff240052010-01-17 18:52:16 +000077 getNameWithPrefix(OutName, "__unnamed_" + Twine(ID), PrefixTy);
Chris Lattner5b7dfee2009-09-11 05:40:42 +000078}
79
Chris Lattner61f160a2010-01-16 18:06:34 +000080/// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
81/// and the specified global variable's name. If the global variable doesn't
82/// have a name, this fills in a unique name for the global.
83std::string Mangler::getNameWithPrefix(const GlobalValue *GV,
84 bool isImplicitlyPrivate) {
85 SmallString<64> Buf;
86 getNameWithPrefix(Buf, GV, isImplicitlyPrivate);
87 return std::string(Buf.begin(), Buf.end());
88}