cpp/llvm; os.Args; build: add llvm.BinDir to PATH

This commit is contained in:
xushiwei
2024-07-15 00:22:10 +08:00
parent 9156466351
commit cbe190fa70
9 changed files with 210 additions and 6 deletions

View File

@@ -0,0 +1,34 @@
package main
import (
"fmt"
"os"
"unsafe"
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/cpp/llvm"
)
func Demangle(mangledName string) *c.Char {
if ret := llvm.ItaniumDemangle(mangledName, true); ret != nil {
return ret
}
if ret := llvm.RustDemangle(mangledName); ret != nil {
return ret
}
return llvm.MicrosoftDemangle(mangledName, nil, nil, 0)
}
func main() {
if len(os.Args) != 2 {
fmt.Fprintln(os.Stderr, "Usage: demangle symbol")
return
}
mangledName := os.Args[1]
if name := Demangle(mangledName); name != nil {
c.Printf(c.Str("%s\n"), name)
c.Free(unsafe.Pointer(name))
} else {
fmt.Fprintln(os.Stderr, "Failed to demangle")
}
}

71
cpp/llvm/demangle.go Normal file
View File

@@ -0,0 +1,71 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package llvm
import (
_ "unsafe"
"github.com/goplus/llgo/c"
)
// -----------------------------------------------------------------------------
// Returns a non-NULL pointer to a NUL-terminated C style string
// that should be explicitly freed, if successful. Otherwise, may return
// nullptr if mangled_name is not a valid mangling or is nullptr.
//
// char *itaniumDemangle(std::string_view mangled_name, bool ParseParams = true);
//
//go:linkname ItaniumDemangle C._ZN4llvm15itaniumDemangleENSt3__117basic_string_viewIcNS0_11char_traitsIcEEEEb
func ItaniumDemangle(mangledName StringView, parseParams bool) *c.Char
/*
enum MSDemangleFlags {
MSDF_None = 0,
MSDF_DumpBackrefs = 1 << 0,
MSDF_NoAccessSpecifier = 1 << 1,
MSDF_NoCallingConvention = 1 << 2,
MSDF_NoReturnType = 1 << 3,
MSDF_NoMemberType = 1 << 4,
MSDF_NoVariableType = 1 << 5,
};
*/
type MSDemangleFlags c.Int
// Demangles the Microsoft symbol pointed at by mangled_name and returns it.
// Returns a pointer to the start of a null-terminated demangled string on
// success, or nullptr on error.
//
// If n_read is non-null and demangling was successful, it receives how many
// bytes of the input string were consumed.
//
// status receives one of the demangle_ enum entries above if it's not nullptr.
// Flags controls various details of the demangled representation.
//
// char *microsoftDemangle(std::string_view mangled_name, size_t *n_read, int *status, MSDemangleFlags Flags = MSDF_None);
//
//go:linkname MicrosoftDemangle C._ZN4llvm17microsoftDemangleENSt3__117basic_string_viewIcNS0_11char_traitsIcEEEEPmPiNS_15MSDemangleFlagsE
func MicrosoftDemangle(mangledName StringView, nRead *uintptr, status *c.Int, flags MSDemangleFlags) *c.Char
// Demangles a Rust v0 mangled symbol.
//
// char *rustDemangle(std::string_view MangledName);
//
//go:linkname RustDemangle C._ZN4llvm12rustDemangleENSt3__117basic_string_viewIcNS0_11char_traitsIcEEEE
func RustDemangle(mangledName StringView) *c.Char
// -----------------------------------------------------------------------------

28
cpp/llvm/llvm.go Normal file
View File

@@ -0,0 +1,28 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package llvm
// -----------------------------------------------------------------------------
const (
LLGoPackage = "link: -L$(llvm-config --libdir) -lLLVM; -lLLVM"
)
// StringView represents a C++ std::string_view object.
type StringView = string
// -----------------------------------------------------------------------------

View File

@@ -16,6 +16,8 @@
package std
// -----------------------------------------------------------------------------
const (
LLGoFiles = "_wrap/string.cpp"
LLGoPackage = "link: c++"

View File

@@ -25,6 +25,11 @@ import (
// -----------------------------------------------------------------------------
// StringView represents a C++ std::string_view object.
type StringView = string
// -----------------------------------------------------------------------------
// String represents a C++ std::string object.
type String struct {
Unused [24]byte