From ca3f25c1fb9bbf0d807985baee460670b7f195b4 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sat, 21 Mar 2009 08:24:40 +0000 Subject: [PATCH] fix several problems with asm renaming, by pulling it into the mangling code: 1. it wasn't applying to definitions, only declarations, e.g. int x __asm("foo") 2. multiple definitions were conflicting, they weren't getting merged. 3. the code was duplicated in several places. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@67442 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CodeGenModule.cpp | 18 ------------------ lib/CodeGen/Mangle.cpp | 25 +++++++++++++++++-------- lib/CodeGen/Mangle.h | 1 + test/CodeGen/mangle.c | 21 ++++++++++++++++++++- 4 files changed, 38 insertions(+), 27 deletions(-) diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp index c46db96bad..97f989dbe3 100644 --- a/lib/CodeGen/CodeGenModule.cpp +++ b/lib/CodeGen/CodeGenModule.cpp @@ -305,11 +305,6 @@ void CodeGenModule::SetGlobalValueAttributes(const Decl *D, setGlobalVisibility(GV, attr->getVisibility()); // FIXME: else handle -fvisibility - // Prefaced with special LLVM marker to indicate that the name - // should not be munged. - if (const AsmLabelAttr *ALA = D->getAttr()) - GV->setName("\01" + ALA->getLabel()); - if (const SectionAttr *SA = D->getAttr()) GV->setSection(SA->getName()); @@ -629,12 +624,6 @@ void CodeGenModule::EmitGlobalDefinition(const ValueDecl *D) { if (D->getAttr() || D->getAttr()) GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); - // FIXME: This should be handled by the mangler! - if (const AsmLabelAttr *ALA = D->getAttr()) { - // Prefaced with special LLVM marker to indicate that the name - // should not be munged. - GV->setName("\01" + ALA->getLabel()); - } return Entry = GV; } @@ -746,13 +735,6 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) { setGlobalVisibility(GV, attr->getVisibility()); // FIXME: else handle -fvisibility - // FIXME: This should be a mangling issue. - if (const AsmLabelAttr *ALA = D->getAttr()) { - // Prefaced with special LLVM marker to indicate that the name - // should not be munged. - GV->setName("\01" + ALA->getLabel()); - } - // Set the llvm linkage type as appropriate. if (D->getStorageClass() == VarDecl::Static) GV->setLinkage(llvm::Function::InternalLinkage); diff --git a/lib/CodeGen/Mangle.cpp b/lib/CodeGen/Mangle.cpp index e760b83fe6..7a7a480bdb 100644 --- a/lib/CodeGen/Mangle.cpp +++ b/lib/CodeGen/Mangle.cpp @@ -57,6 +57,15 @@ namespace { bool CXXNameMangler::mangle(const NamedDecl *D) { + // Any decl can be declared with __asm("foo") on it, and this takes + // precedence over all other naming in the .o file. + if (const AsmLabelAttr *ALA = D->getAttr()) { + // If we have an asm name, then we use it as the mangling. + Out << '\01'; // LLVM IR Marker for __asm("foo") + Out << ALA->getLabel(); + return true; + } + // ::= _Z // ::= // ::= @@ -68,15 +77,15 @@ bool CXXNameMangler::mangle(const NamedDecl *D) { // Clang's "overloadable" attribute extension to C/C++ implies // name mangling (always). - if (FD->getAttr()) + if (FD->getAttr()) { ; // fall into mangling code unconditionally. - else if (// C functions are not mangled - !Context.getLangOptions().CPlusPlus || - // "main" is not mangled in C++ - FD->isMain() || - // No mangling in an "implicit extern C" header. - Context.getSourceManager().getFileCharacteristic(FD->getLocation()) - == SrcMgr::C_ExternCSystem) + } else if (// C functions are not mangled + !Context.getLangOptions().CPlusPlus || + // "main" is not mangled in C++ + FD->isMain() || + // No mangling in an "implicit extern C" header. + Context.getSourceManager().getFileCharacteristic(FD->getLocation()) + == SrcMgr::C_ExternCSystem) return false; else { // No name mangling in a C linkage specification. diff --git a/lib/CodeGen/Mangle.h b/lib/CodeGen/Mangle.h index 60a5113ccc..627c16a08e 100644 --- a/lib/CodeGen/Mangle.h +++ b/lib/CodeGen/Mangle.h @@ -14,6 +14,7 @@ // http://www.codesourcery.com/public/cxx-abi/abi.html // //===----------------------------------------------------------------------===// + #ifndef LLVM_CLANG_CODEGEN_MANGLE_H #define LLVM_CLANG_CODEGEN_MANGLE_H diff --git a/test/CodeGen/mangle.c b/test/CodeGen/mangle.c index fbee4122d1..cdcce751cb 100644 --- a/test/CodeGen/mangle.c +++ b/test/CodeGen/mangle.c @@ -1,9 +1,28 @@ // RUN: clang -arch i386 -emit-llvm -o %t %s && // RUN: grep '@_Z2f0i' %t && -// RUN: grep '@_Z2f0l' %t +// RUN: grep '@_Z2f0l' %t && // Make sure we mangle overloadable, even in C system headers. # 1 "somesystemheader.h" 1 3 4 void __attribute__((__overloadable__)) f0(int a) {} void __attribute__((__overloadable__)) f0(long b) {} + + + +// These should get merged. +void foo() __asm__("bar"); +void foo2() __asm__("bar"); + +// RUN: grep '@"\\01foo"' %t && +// RUN: grep '@"\\01bar"' %t + +int nux __asm__("foo"); +extern float nux2 __asm__("foo"); + +int test() { + foo(); + foo2(); + + return nux + nux2; +} -- 2.40.0