From 95c1886df5d151e82c8a98d92fd4e46b9d28200e Mon Sep 17 00:00:00 2001 From: xushiwei Date: Fri, 7 Jun 2024 14:12:35 +0800 Subject: [PATCH] c/setjmp/demo --- _demo/setjmp/setjmp.go | 2 + c/setjmp/demo/_code/demo.cpp | 5 +++ c/setjmp/demo/demo.go | 28 +++++++++++++ c/setjmp/demo/demo.ll | 73 +++++++++++++++++++++++++++++++++ c/setjmp/demo/llgo.cfg | 7 ++++ c/setjmp/demo/llgo_autogen.lla | Bin 0 -> 1809 bytes 6 files changed, 115 insertions(+) create mode 100644 c/setjmp/demo/_code/demo.cpp create mode 100644 c/setjmp/demo/demo.go create mode 100644 c/setjmp/demo/demo.ll create mode 100644 c/setjmp/demo/llgo.cfg create mode 100644 c/setjmp/demo/llgo_autogen.lla 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 0000000000000000000000000000000000000000..700769e6ce6cce32d807e43d7299f3a824468425 GIT binary patch literal 1809 zcmZ{ldobJS8pnS~P{q2_Rzvr+8unDQu8Y#LjJQ-JBvSWUf{MrzB&6<_CGDKDw(8PW zlW5i@SV=_D9u4gx#p-F@7EO9Ym1WhPsFUu_+1WXrGw;0fKF>ejcjoiQ_k-|K0O)jJ}t?xRZ=-=*!xX)4(#(H?D0ZCQM z#=aBQ!Lg{{8#c^;#w0CRZ!KPQkBSl->bB`xvpg28E)9%8y^2Z&7p8wcOFoZ`ZIoD{ z0pB(VyC!v^C;GT|!0YdsV>Fb?TT9EVTKZZZL99A+u@QpVR-c~ZUFBu*V^nI~jwR!% zbF(WLdNl4lFMq61-t1pTv0J_TZQ-P}K8O$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