Skip to content

Commit

Permalink
Support 'raw mangles' via leading "\1" in pragma(mangle) strings
Browse files Browse the repository at this point in the history
Such mangled names aren't affected by a target-specific default (C)
prefix, such as `_` on Mac.
  • Loading branch information
kinke committed Oct 21, 2021
1 parent c79a9ee commit e0a11b3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
9 changes: 9 additions & 0 deletions dmd/dsymbolsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -1526,6 +1526,15 @@ version (IN_LLVM)
dchar c = slice[i];
if (c < 0x80)
{
version (IN_LLVM)
{
// LDC: allow leading "\1" to prevent target-specific prefix
if (i == 0 && c == '\1')
{
++i;
continue;
}
}
if (c.isValidMangling)
{
++i;
Expand Down
8 changes: 6 additions & 2 deletions gen/mangling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,15 @@ std::string getIRMangledName(VarDeclaration *vd) {
}

std::string getIRMangledFuncName(std::string baseMangle, LINK link) {
return gABI->mangleFunctionForLLVM(std::move(baseMangle), link);
return baseMangle[0] == '\1'
? baseMangle
: gABI->mangleFunctionForLLVM(std::move(baseMangle), link);
}

std::string getIRMangledVarName(std::string baseMangle, LINK link) {
return gABI->mangleVariableForLLVM(std::move(baseMangle), link);
return baseMangle[0] == '\1'
? baseMangle
: gABI->mangleVariableForLLVM(std::move(baseMangle), link);
}

std::string getIRMangledAggregateName(AggregateDeclaration *ad,
Expand Down
11 changes: 11 additions & 0 deletions tests/codegen/mangling_raw.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Tests that 'raw mangles' starting with "\1" are correctly propagated to IR.

// RUN: %ldc -output-ll -of=%t.ll %s && FileCheck %s < %t.ll

// CHECK: @"\01myGlobal" = global i32
pragma(mangle, "\1myGlobal")
__gshared int myGlobal;

// CHECK: define {{.*}} @"\01myFunction"()
pragma(mangle, "\1myFunction")
void myFunction() {}

0 comments on commit e0a11b3

Please sign in to comment.