about golang defer?

158 views
Skip to first unread message

xie cui

unread,
Jun 27, 2021, 10:57:17 PM6/27/21
to golang-nuts
a defer call generate some code like:
    • v42 (8) = StaticLECall <mem> {AuxCall{runtime.deferprocStack}} [8] v36 v41
    • v43 (8) = SelectN <mem> [0] v42
  • Defer v42 → b2 b3 (likely) (8)
  • b2: ← b1-
    • v48 (7) = Copy <mem> v43
    • v49 (7) = VarKill <mem> {.autotmp_0} v48
    • v50 (9) = StaticLECall <mem> {AuxCall{runtime.deferreturn}} v49
    • v51 (9) = SelectN <mem> [0] v50
    • v52 (9) = MakeResult <mem> v51
  • Ret v52 (9)
  • b3: ← b1-
    • v44 (8) = Copy <mem> v43
    • v45 (8) = StaticLECall <mem> {AuxCall{runtime.deferreturn}} v44
    • v46 (8) = SelectN <mem> [0] v45
    • v47 (8) = MakeResult <mem> v46
  • Ret v47 (8)
my question is Under what circumstances the deferprocStack return 1?
some useful info: 

https://github.com/golang/go/blob/master/src/runtime/panic.go#L290-L328, in these lines it seems deferprocStack always return 0. i think i have miss some thing, please help me to find when it get to branch return 1.

jake...@gmail.com

unread,
Jun 28, 2021, 11:57:57 AM6/28/21
to golang-nuts
I can't help you with your specific question, but two procedural points. First, while such a question is welcome in this group (golang-nuts), you may reach an audience better able to answer it on the golang-dev group, since it deals with the internal details of the compiler. But I would also note that golang-dev is in "Quiet Week", so will be less active than normal.

However you proceed, I hope you get an answer.

Ian Lance Taylor

unread,
Jun 28, 2021, 12:58:10 PM6/28/21
to xie cui, golang-nuts
deferprocStack will appear to return 1 if the deferred function calls
recover. This is implemented by the function "recovery" in
runtime/panic.go, which resets the program counter and stack pointer
and sets the return register to 1.

Ian
Reply all
Reply to author
Forward
0 new messages