From e71ea9f0edfc24cbfaf066e906b0ab4df9d3e8c6 Mon Sep 17 00:00:00 2001 From: Josh Berdine Date: Fri, 12 Oct 2018 13:56:50 -0700 Subject: [PATCH] [sledge] Switch from Llvm.demangle to __cxa_demangle via Ctypes Summary: Call __cxa_demangle from libstdc++ using Ctypes instead of the wrapper in Llvm. Reviewed By: mbouaziz Differential Revision: D9939682 fbshipit-source-id: a9f02fff5 --- sledge/llair.opam | 2 ++ sledge/sledge.opam | 2 ++ sledge/src/llair/dune.in | 2 +- sledge/src/llair/global.ml | 24 +++++++++++++++++++++--- 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/sledge/llair.opam b/sledge/llair.opam index 847a31e1d..68355e69a 100644 --- a/sledge/llair.opam +++ b/sledge/llair.opam @@ -10,6 +10,8 @@ build: [ depends: [ "cmdliner" "core_kernel" {>= "v0.11.0"} + "ctypes" + "ctypes-foreign" "dune" {build} "llvm" {build & = "7.0.0"} "ppx_compare" {>= "v0.11.0"} diff --git a/sledge/sledge.opam b/sledge/sledge.opam index 60e026216..069d452c7 100644 --- a/sledge/sledge.opam +++ b/sledge/sledge.opam @@ -10,6 +10,8 @@ build: [ depends: [ "cmdliner" "core_kernel" {>= "v0.11.0"} + "ctypes" + "ctypes-foreign" "dune" {build} "llvm" {build & = "7.0.0"} "ppx_compare" {>= "v0.11.0"} diff --git a/sledge/src/llair/dune.in b/sledge/src/llair/dune.in index 8d3080474..0f5bec23b 100644 --- a/sledge/src/llair/dune.in +++ b/sledge/src/llair/dune.in @@ -10,6 +10,6 @@ Jbuild_plugin.V1.send (name llair_) (public_name llair) %s - (libraries llvm llvm.irreader llvm.analysis llvm.scalar_opts llvm.target %s)) + (libraries ctypes ctypes.foreign llvm llvm.irreader llvm.analysis llvm.scalar_opts llvm.target %s)) |} (flags deps) (libraries deps) diff --git a/sledge/src/llair/global.ml b/sledge/src/llair/global.ml index f47a09ef9..f860e6a91 100644 --- a/sledge/src/llair/global.ml +++ b/sledge/src/llair/global.ml @@ -12,6 +12,23 @@ type t = {var: Var.t; init: Exp.t option; siz: int; typ: Typ.t; loc: Loc.t} let equal = [%compare.equal: t] +let demangle = + let open Ctypes in + let cxa_demangle = + (* char *__cxa_demangle(const char *, char *, size_t *, int * ) *) + Foreign.foreign "__cxa_demangle" + ( string @-> ptr char @-> ptr size_t @-> ptr int + @-> returning string_opt ) + in + let null_ptr_char = from_voidp char null in + let null_ptr_size_t = from_voidp size_t null in + let status = allocate int 0 in + fun mangled -> + let demangled = + cxa_demangle mangled null_ptr_char null_ptr_size_t status + in + if !@status = 0 then demangled else None + let pp fs {var} = let name = Var.name var in let pf pp = @@ -19,9 +36,10 @@ let pp fs {var} = Format.kfprintf (fun fs -> Format.pp_close_box fs ()) fs pp in pf "@%s%t" name (fun fs -> - let demangled = Llvm.demangle name in - if not (String.is_empty demangled || String.equal name demangled) then - Format.fprintf fs "ā€œ%sā€" demangled ) + match demangle name with + | Some demangled when not (String.equal name demangled) -> + Format.fprintf fs "ā€œ%sā€" demangled + | _ -> () ) let pp_defn fs {var; init; typ} = Format.fprintf fs "@[<2>%a %a%a@]" Typ.pp typ Var.pp var