diff options
author | Chris Lattner <sabre@nondot.org> | 2001-10-28 23:44:03 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2001-10-28 23:44:03 +0000 |
commit | 0047a708afae584fae074071de0b8c7f24345bfd (patch) | |
tree | 17438beb42dcef07632aa5f530130a3916a69805 /support | |
parent | 1d050112f72c87bb1a53d2218c793ff44537dcef (diff) | |
download | external_llvm-0047a708afae584fae074071de0b8c7f24345bfd.zip external_llvm-0047a708afae584fae074071de0b8c7f24345bfd.tar.gz external_llvm-0047a708afae584fae074071de0b8c7f24345bfd.tar.bz2 |
Initial checkin of name mangling code moved from linker
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@1018 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'support')
-rw-r--r-- | support/lib/Support/NameMangling.cpp | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/support/lib/Support/NameMangling.cpp b/support/lib/Support/NameMangling.cpp new file mode 100644 index 0000000..675a7ff --- /dev/null +++ b/support/lib/Support/NameMangling.cpp @@ -0,0 +1,46 @@ +//===- NameMangling.cpp - Name Mangling for LLVM ----------------------------=// +// +// This file implements a consistent scheme for name mangling symbols. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Support/NameMangling.h" +#include "llvm/DerivedTypes.h" +#include "llvm/GlobalValue.h" + +// MangleTypeName - Implement a consistent name-mangling scheme for +// a given type. +// +string MangleTypeName(const Type *Ty) { + string mangledName; + if (Ty->isPrimitiveType()) { + const string &longName = Ty->getDescription(); + return string(longName.c_str(), (longName.length() < 2) ? 1 : 2); + } else if (PointerType *PTy = dyn_cast<PointerType>(Ty)) { + mangledName = string("P_" + MangleTypeName(PTy->getValueType())); + } else if (StructType *STy = dyn_cast<StructType>(Ty)) { + mangledName = string("S_"); + for (unsigned i=0; i < STy->getNumContainedTypes(); ++i) + mangledName += MangleTypeName(STy->getContainedType(i)); + } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { + mangledName = string("A_" +MangleTypeName(ATy->getElementType())); + } else if (MethodType *MTy = dyn_cast<MethodType>(Ty)) { + mangledName = string("M_") + MangleTypeName(MTy->getReturnType()); + for (unsigned i = 1; i < MTy->getNumContainedTypes(); ++i) + mangledName += string(MangleTypeName(MTy->getContainedType(i))); + } + + return mangledName; +} + +// mangleName - implement a consistent name-mangling scheme for all +// externally visible (i.e., global) objects. +// privateName should be unique within the module. +// +string MangleName(const string &privateName, const Value *V) { + // Lets drop the P_ before every global name since all globals are ptrs + return privateName + "_" + + MangleTypeName(isa<GlobalValue>(V) + ? cast<GlobalValue>(V)->getType()->getValueType() + : V->getType()); +} |