nico/demumble

[FR} Swift demumble-ing

sgraham opened this issue · 3 comments

swiftc is a very mangle-y compiler, and I sometimes have mixed C++/Swift stacks. IWBN if I didn't have to multiplex swift-demangle and demumble.

Ref: https://github.com/apple/swift/blob/main/stdlib/public/runtime/Demangle.cpp (I think)

nico commented

How hard could it be, I'll give it a try 🙂

(ps: I don't read GitHub notifications; ping me elsewhere for things you'd like me to see faster than "in a few months".)

nico commented

As far as I can tell, swift has two demanglers. The runtime demangler you linked to, and the toolchain's libDemangling (https://github.com/swiftlang/swift/tree/main/lib/Demangling).

(It looks like new demangling tends to be implemented in the toolchain first, and then reimplemented in the runtime later, e.g.

  1. swiftlang/swift@4da1032
  2. swiftlang/swift@40e07cf
    )

(There's also lib/SwiftDemangle, but it's only a thin wrapper around lib/Demangling that doesn't really add anything: https://github.com/swiftlang/swift/tree/main/lib/SwiftDemangle)

lib/Demangling is fairly self-contained. This builds it (in a subdir of a swift checkout):

#!/bin/bash

set -eu

clang -std=c++17 \
      -c ../lib/Demangling/*.cpp \
      -DLLVM_DISABLE_ABI_BREAKING_CHECKS_ENFORCING=1 \
      -DSWIFT_SUPPORT_OLD_MANGLING=1 \
      -DSWIFT_STDLIB_HAS_TYPE_PRINTING=1 \
      -I ../include \
      -I ~/src/llvm-project/llvm/include \
      -I ~/src/llvm-project/out/gn//gen/llvm/include

clang++ -shared -o libDemangling.dylib *.o 

It does depend on Compiler.h in llvm, which in turn depends on the generated llvm-config.h, which is a bit annoying -- but probably no major showstopper.

nico commented

This copies out all the files that are needed (at swiftlang/swift@a894dd2 and llvm/llvm-project@3a4c7cc) and builds:

% pwd
/Users/thakis/src/swift/build-demangle

% time ./standalone-build.sh
./standalone-build.sh  2.65s user 0.36s system 100% cpu 3.004 total

% echo $?
0

thakis@Nicos-MacBook-Pro build-demangle % cat standalone-build.sh 
#!/bin/bash

set -eu

LLVM_DIR=$HOME/src/llvm-project/llvm
LLVM_HEADER_GEN=$HOME/src/llvm-project/out/gn/gen
SWIFT_DIR=..

rm -rf llvm swift

mkdir -p llvm/include/llvm/ADT
cp $LLVM_DIR/include/llvm/ADT/ADL.h llvm/include/llvm/ADT
cp $LLVM_DIR/include/llvm/ADT/DenseMapInfo.h llvm/include/llvm/ADT
cp $LLVM_DIR/include/llvm/ADT/Hashing.h llvm/include/llvm/ADT
cp $LLVM_DIR/include/llvm/ADT/STLExtras.h llvm/include/llvm/ADT
cp $LLVM_DIR/include/llvm/ADT/STLForwardCompat.h llvm/include/llvm/ADT
cp $LLVM_DIR/include/llvm/ADT/STLFunctionalExtras.h llvm/include/llvm/ADT
cp $LLVM_DIR/include/llvm/ADT/StringRef.h llvm/include/llvm/ADT
cp $LLVM_DIR/include/llvm/ADT/StringSwitch.h llvm/include/llvm/ADT
cp $LLVM_DIR/include/llvm/ADT/bit.h llvm/include/llvm/ADT
cp $LLVM_DIR/include/llvm/ADT/iterator.h llvm/include/llvm/ADT
cp $LLVM_DIR/include/llvm/ADT/iterator_range.h llvm/include/llvm/ADT

mkdir -p llvm/include/llvm/Support
cp $LLVM_DIR/include/llvm/Support/Casting.h llvm/include/llvm/Support 
cp $LLVM_DIR/include/llvm/Support/Compiler.h llvm/include/llvm/Support 
cp $LLVM_DIR/include/llvm/Support/DataTypes.h llvm/include/llvm/Support 
cp $LLVM_DIR/include/llvm/Support/ErrorHandling.h llvm/include/llvm/Support
cp $LLVM_DIR/include/llvm/Support/SwapByteOrder.h llvm/include/llvm/Support
cp $LLVM_DIR/include/llvm/Support/type_traits.h llvm/include/llvm/Support

mkdir llvm/include/llvm-c
cp $LLVM_DIR/include/llvm-c/DataTypes.h llvm/include/llvm-c

mkdir -p swift/include/swift
cp -R $SWIFT_DIR/include/swift/Demangling swift/include/swift
cp $SWIFT_DIR/include/swift/Strings.h swift/include/swift

mkdir -p swift/include/swift/ABI
cp $SWIFT_DIR/include/swift/ABI/InvertibleProtocols.def swift/include/swift/ABI

mkdir -p swift/include/swift/AST
cp $SWIFT_DIR/include/swift/AST/Ownership.h swift/include/swift/AST
cp $SWIFT_DIR/include/swift/AST/ReferenceStorage.def swift/include/swift/AST

mkdir -p swift/include/swift/Basic
cp $SWIFT_DIR/include/swift/Basic/Assertions.h swift/include/swift/Basic 
cp $SWIFT_DIR/include/swift/Basic/InlineBitfield.h swift/include/swift/Basic 
cp $SWIFT_DIR/include/swift/Basic/LLVM.h swift/include/swift/Basic
cp $SWIFT_DIR/include/swift/Basic/MacroRoles.def swift/include/swift/Basic 
cp $SWIFT_DIR/include/swift/Basic/STLExtras.h swift/include/swift/Basic

mkdir -p swift/lib
cp -R $SWIFT_DIR/lib/Demangling swift/lib

mkdir -p llvm/include/llvm/Config
cp $LLVM_HEADER_GEN/llvm/include/llvm/Config/abi-breaking.h llvm/include/llvm/Config
cp $LLVM_HEADER_GEN/llvm/include/llvm/Config/llvm-config.h llvm/include/llvm/Config

clang -std=c++17 \
      -c swift/lib/Demangling/*.cpp \
      -DLLVM_DISABLE_ABI_BREAKING_CHECKS_ENFORCING=1 \
      -DSWIFT_SUPPORT_OLD_MANGLING=1 \
      -DSWIFT_STDLIB_HAS_TYPE_PRINTING=1 \
      -I swift/include \
      -I llvm/include

clang++ -shared -o libDemangling.dylib *.o