Go X/Tools SSA init$guard

47 views
Skip to first unread message

Xie Yuchen

unread,
Apr 3, 2024, 6:07:28 AM4/3/24
to golang-nuts
I tried to write the SSA information out and found the init$guard which is likely to decide the execution of the sub-packages init functions.

For example, give the following go code, I will get the SSA output.

package main

import "fmt"
import _ "unsafe"

const message = "Hello, World!"

func init(){}

func main() {
fmt.Println(message)
}

func empty(){}

The output is shown below. Based on the code, I have a rough idea  that the variable init$guard might be set by someone to control the execution of init. However, i failed to find the documentation about it when I search "init$guard", "package initializer" and so on. Could someone help to explain how it works and which component sets up the variable. 

# Name: hello.init
# Package: hello
# Synthetic: package initializer
func init():
0:                                                                entry P:0 S:2
        t0 = *init$guard                                                   bool
        if t0 goto 2 else 1
1:                                                           init.start P:1 S:1
        *init$guard = true:bool
        t1 = fmt.init()                                                      ()
        t2 = unsafe.init()                                                   ()
        t3 = init#1()                                                        ()
        jump 2
2:                                                            init.done P:2 S:0
        return

Reply all
Reply to author
Forward
0 new messages