diff --git a/_demo/setjmp/setjmp.go b/_demo/setjmp/setjmp.go index b8508de5..e8955695 100644 --- a/_demo/setjmp/setjmp.go +++ b/_demo/setjmp/setjmp.go @@ -2,6 +2,7 @@ package main import ( "github.com/goplus/llgo/c/setjmp" + "github.com/goplus/llgo/c/setjmp/demo" ) func main() { @@ -9,6 +10,7 @@ func main() { switch ret := setjmp.Sigsetjmp(&jb, 0); ret { case 0: println("Hello, setjmp!") + demo.ThrowCppException() setjmp.Siglongjmp(&jb, 1) default: println("exception:", ret) diff --git a/c/setjmp/demo/_code/demo.cpp b/c/setjmp/demo/_code/demo.cpp new file mode 100644 index 00000000..92f82eb6 --- /dev/null +++ b/c/setjmp/demo/_code/demo.cpp @@ -0,0 +1,5 @@ +#include + +extern "C" void throwCppException() { + throw std::runtime_error("C++ exception"); +} diff --git a/c/setjmp/demo/demo.go b/c/setjmp/demo/demo.go new file mode 100644 index 00000000..d0fed258 --- /dev/null +++ b/c/setjmp/demo/demo.go @@ -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 demo + +import ( + _ "unsafe" +) + +const ( + LLGoPackage = "link: c++" +) + +//go:linkname ThrowCppException C.throwCppException +func ThrowCppException() diff --git a/c/setjmp/demo/demo.ll b/c/setjmp/demo/demo.ll new file mode 100644 index 00000000..0e811d18 --- /dev/null +++ b/c/setjmp/demo/demo.ll @@ -0,0 +1,73 @@ +; ModuleID = '_code/demo.cpp' +source_filename = "_code/demo.cpp" + +%"class.std::runtime_error" = type { %"class.std::exception", %"class.std::__1::__libcpp_refstring" } +%"class.std::exception" = type { i32 (...)** } +%"class.std::__1::__libcpp_refstring" = type { i8* } + +@.str = private unnamed_addr constant [14 x i8] c"C++ exception\00", align 1 +@_ZTISt13runtime_error = external constant i8* + +; Function Attrs: noinline optnone ssp uwtable(sync) +define void @throwCppException() #0 personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) { + %1 = alloca i8*, align 8 + %2 = alloca i32, align 4 + %3 = call i8* @__cxa_allocate_exception(i64 16) #3 + %4 = bitcast i8* %3 to %"class.std::runtime_error"* + %5 = invoke %"class.std::runtime_error"* @_ZNSt13runtime_errorC1EPKc(%"class.std::runtime_error"* noundef %4, i8* noundef getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0)) + to label %6 unwind label %7 + +6: ; preds = %0 + call void @__cxa_throw(i8* %3, i8* bitcast (i8** @_ZTISt13runtime_error to i8*), i8* bitcast (%"class.std::runtime_error"* (%"class.std::runtime_error"*)* @_ZNSt13runtime_errorD1Ev to i8*)) #4 + unreachable + +7: ; preds = %0 + %8 = landingpad { i8*, i32 } + cleanup + %9 = extractvalue { i8*, i32 } %8, 0 + store i8* %9, i8** %1, align 8 + %10 = extractvalue { i8*, i32 } %8, 1 + store i32 %10, i32* %2, align 4 + call void @__cxa_free_exception(i8* %3) #3 + br label %11 + +11: ; preds = %7 + %12 = load i8*, i8** %1, align 8 + %13 = load i32, i32* %2, align 4 + %14 = insertvalue { i8*, i32 } undef, i8* %12, 0 + %15 = insertvalue { i8*, i32 } %14, i32 %13, 1 + resume { i8*, i32 } %15 +} + +declare i8* @__cxa_allocate_exception(i64) + +declare %"class.std::runtime_error"* @_ZNSt13runtime_errorC1EPKc(%"class.std::runtime_error"* noundef returned, i8* noundef) unnamed_addr #1 + +declare i32 @__gxx_personality_v0(...) + +declare void @__cxa_free_exception(i8*) + +; Function Attrs: nounwind +declare %"class.std::runtime_error"* @_ZNSt13runtime_errorD1Ev(%"class.std::runtime_error"* noundef returned) unnamed_addr #2 + +declare void @__cxa_throw(i8*, i8*, i8*) + +attributes #0 = { noinline optnone ssp uwtable(sync) "frame-pointer"="non-leaf" "min-legal-vector-width"="0" "no-trapping-math"="true" "probe-stack"="__chkstk_darwin" "stack-protector-buffer-size"="8" "target-cpu"="apple-m1" "target-features"="+aes,+crc,+crypto,+dotprod,+fp-armv8,+fp16fml,+fullfp16,+lse,+neon,+ras,+rcpc,+rdm,+sha2,+sha3,+sm4,+v8.5a,+zcm,+zcz" } +attributes #1 = { "frame-pointer"="non-leaf" "no-trapping-math"="true" "probe-stack"="__chkstk_darwin" "stack-protector-buffer-size"="8" "target-cpu"="apple-m1" "target-features"="+aes,+crc,+crypto,+dotprod,+fp-armv8,+fp16fml,+fullfp16,+lse,+neon,+ras,+rcpc,+rdm,+sha2,+sha3,+sm4,+v8.5a,+zcm,+zcz" } +attributes #2 = { nounwind "frame-pointer"="non-leaf" "no-trapping-math"="true" "probe-stack"="__chkstk_darwin" "stack-protector-buffer-size"="8" "target-cpu"="apple-m1" "target-features"="+aes,+crc,+crypto,+dotprod,+fp-armv8,+fp16fml,+fullfp16,+lse,+neon,+ras,+rcpc,+rdm,+sha2,+sha3,+sm4,+v8.5a,+zcm,+zcz" } +attributes #3 = { nounwind } +attributes #4 = { noreturn } + +!llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8} +!llvm.ident = !{!9} + +!0 = !{i32 2, !"SDK Version", [2 x i32] [i32 13, i32 3]} +!1 = !{i32 1, !"wchar_size", i32 4} +!2 = !{i32 8, !"branch-target-enforcement", i32 0} +!3 = !{i32 8, !"sign-return-address", i32 0} +!4 = !{i32 8, !"sign-return-address-all", i32 0} +!5 = !{i32 8, !"sign-return-address-with-bkey", i32 0} +!6 = !{i32 7, !"PIC Level", i32 2} +!7 = !{i32 7, !"uwtable", i32 1} +!8 = !{i32 7, !"frame-pointer", i32 1} +!9 = !{!"Apple clang version 14.0.3 (clang-1403.0.22.14.1)"} diff --git a/c/setjmp/demo/llgo.cfg b/c/setjmp/demo/llgo.cfg new file mode 100644 index 00000000..c5ed417c --- /dev/null +++ b/c/setjmp/demo/llgo.cfg @@ -0,0 +1,7 @@ +{ + "cl": [ + "clang -emit-llvm -S -o demo.ll -c _code/demo.cpp", + "llgen .", + "rm llgo_autogen.lla; zip llgo_autogen.lla llgo_autogen.ll demo.ll", + ] +} diff --git a/c/setjmp/demo/llgo_autogen.lla b/c/setjmp/demo/llgo_autogen.lla new file mode 100644 index 00000000..700769e6 Binary files /dev/null and b/c/setjmp/demo/llgo_autogen.lla differ