Created
September 28, 2018 21:08
-
-
Save sklam/b637304bfef12f945db9066fae686e8b to your computer and use it in GitHub Desktop.
How C++ RAII work?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Compile with clang -S -emit-llvm -O1 main.cpp | |
| #include <cstdio> | |
| struct Apple { | |
| Apple() { | |
| puts("CTOR"); | |
| } | |
| ~Apple() { | |
| puts("DTOR"); | |
| } | |
| }; | |
| int foo() { | |
| Apple apple; | |
| } | |
| int main() { | |
| Apple apple; | |
| foo(); | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ; ModuleID = 'main.cpp' | |
| source_filename = "main.cpp" | |
| target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" | |
| target triple = "x86_64-apple-macosx10.13.0" | |
| %struct.Apple = type { i8 } | |
| @.str = private unnamed_addr constant [5 x i8] c"CTOR\00", align 1 | |
| @.str.1 = private unnamed_addr constant [5 x i8] c"DTOR\00", align 1 | |
| ; Function Attrs: ssp uwtable | |
| define i32 @_Z3foov() local_unnamed_addr #0 { | |
| %1 = alloca %struct.Apple, align 1 | |
| %2 = getelementptr inbounds %struct.Apple, %struct.Apple* %1, i64 0, i32 0 | |
| call void @llvm.lifetime.start(i64 1, i8* nonnull %2) #5 | |
| call void @_ZN5AppleC1Ev(%struct.Apple* nonnull %1) | |
| call void @_ZN5AppleD1Ev(%struct.Apple* nonnull %1) | |
| call void @llvm.lifetime.end(i64 1, i8* nonnull %2) #5 | |
| ret i32 undef | |
| } | |
| ; Function Attrs: argmemonly nounwind | |
| declare void @llvm.lifetime.start(i64, i8* nocapture) #1 | |
| ; Function Attrs: ssp uwtable | |
| define linkonce_odr void @_ZN5AppleC1Ev(%struct.Apple*) unnamed_addr #0 align 2 { | |
| tail call void @_ZN5AppleC2Ev(%struct.Apple* %0) | |
| ret void | |
| } | |
| ; Function Attrs: ssp uwtable | |
| define linkonce_odr void @_ZN5AppleD1Ev(%struct.Apple*) unnamed_addr #0 align 2 { | |
| tail call void @_ZN5AppleD2Ev(%struct.Apple* %0) | |
| ret void | |
| } | |
| ; Function Attrs: argmemonly nounwind | |
| declare void @llvm.lifetime.end(i64, i8* nocapture) #1 | |
| ; Function Attrs: norecurse ssp uwtable | |
| define i32 @main() local_unnamed_addr #2 personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) { | |
| %1 = alloca %struct.Apple, align 1 | |
| %2 = getelementptr inbounds %struct.Apple, %struct.Apple* %1, i64 0, i32 0 | |
| call void @llvm.lifetime.start(i64 1, i8* nonnull %2) #5 | |
| call void @_ZN5AppleC1Ev(%struct.Apple* nonnull %1) | |
| %3 = invoke i32 @_Z3foov() | |
| to label %4 unwind label %5 | |
| ; <label>:4: ; preds = %0 | |
| call void @_ZN5AppleD1Ev(%struct.Apple* nonnull %1) | |
| call void @llvm.lifetime.end(i64 1, i8* nonnull %2) #5 | |
| ret i32 0 | |
| ; <label>:5: ; preds = %0 | |
| %6 = landingpad { i8*, i32 } | |
| cleanup | |
| invoke void @_ZN5AppleD1Ev(%struct.Apple* nonnull %1) | |
| to label %7 unwind label %8 | |
| ; <label>:7: ; preds = %5 | |
| call void @llvm.lifetime.end(i64 1, i8* nonnull %2) #5 | |
| resume { i8*, i32 } %6 | |
| ; <label>:8: ; preds = %5 | |
| %9 = landingpad { i8*, i32 } | |
| catch i8* null | |
| %10 = extractvalue { i8*, i32 } %9, 0 | |
| call void @__clang_call_terminate(i8* %10) #6 | |
| unreachable | |
| } | |
| declare i32 @__gxx_personality_v0(...) | |
| ; Function Attrs: noinline noreturn nounwind | |
| define linkonce_odr hidden void @__clang_call_terminate(i8*) local_unnamed_addr #3 { | |
| %2 = tail call i8* @__cxa_begin_catch(i8* %0) #5 | |
| tail call void @_ZSt9terminatev() #6 | |
| unreachable | |
| } | |
| declare i8* @__cxa_begin_catch(i8*) local_unnamed_addr | |
| declare void @_ZSt9terminatev() local_unnamed_addr | |
| ; Function Attrs: ssp uwtable | |
| define linkonce_odr void @_ZN5AppleC2Ev(%struct.Apple*) unnamed_addr #0 align 2 { | |
| %2 = tail call i32 @puts(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0)) | |
| ret void | |
| } | |
| ; Function Attrs: nounwind | |
| declare i32 @puts(i8* nocapture readonly) local_unnamed_addr #4 | |
| ; Function Attrs: ssp uwtable | |
| define linkonce_odr void @_ZN5AppleD2Ev(%struct.Apple*) unnamed_addr #0 align 2 { | |
| %2 = tail call i32 @puts(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0)) | |
| ret void | |
| } | |
| attributes #0 = { ssp uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="penryn" "target-features"="+cx16,+fxsr,+mmx,+sse,+sse2,+sse3,+sse4.1,+ssse3,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" } | |
| attributes #1 = { argmemonly nounwind } | |
| attributes #2 = { norecurse ssp uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="penryn" "target-features"="+cx16,+fxsr,+mmx,+sse,+sse2,+sse3,+sse4.1,+ssse3,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" } | |
| attributes #3 = { noinline noreturn nounwind } | |
| attributes #4 = { nounwind "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="penryn" "target-features"="+cx16,+fxsr,+mmx,+sse,+sse2,+sse3,+sse4.1,+ssse3,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" } | |
| attributes #5 = { nounwind } | |
| attributes #6 = { noreturn nounwind } | |
| !llvm.module.flags = !{!0} | |
| !llvm.ident = !{!1} | |
| !0 = !{i32 1, !"PIC Level", i32 2} | |
| !1 = !{!"Apple LLVM version 9.0.0 (clang-900.0.39.2)"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment