where is the implementation of `getg`?

462 views
Skip to first unread message

Jiajun Huang

unread,
Jan 16, 2018, 8:40:48 PM1/16/18
to golang-nuts
Hi, all:

     I'm reading golang runtime implementation, I've got a function definition:

// getg returns the pointer to the current g.
// The compiler rewrites calls to this function into instructions
// that fetch the g directly (from TLS or from the dedicated register).
func getg
() *g


    So, is there any solution that I can reading what implementation does the compiler rewrite? It seems that it's Assembly code.

Dave Cheney

unread,
Jan 16, 2018, 8:51:28 PM1/16/18
to golang-nuts
A long time ago getg was written in assembly in the runtime package. These days it is implemented directly as pseudo instruction in the compiler. Search for OpGetG in $GOROOT/src/cmd/compile/internal

Ian Lance Taylor

unread,
Jan 16, 2018, 8:53:25 PM1/16/18
to Dave Cheney, golang-nuts
On Tue, Jan 16, 2018 at 5:51 PM, Dave Cheney <da...@cheney.net> wrote:
> A long time ago getg was written in assembly in the runtime package. These
> days it is implemented directly as pseudo instruction in the compiler.
> Search for OpGetG in $GOROOT/src/cmd/compile/internal

Yes. The simplest way to see the generated instructions is to
disassemble the runtime package and look for a place that you know is
a call to getg.

Ian


> On Wednesday, 17 January 2018 12:40:48 UTC+11, Jiajun Huang wrote:
>>
>> Hi, all:
>>
>> I'm reading golang runtime implementation, I've got a function
>> definition:
>>
>> // getg returns the pointer to the current g.
>> // The compiler rewrites calls to this function into instructions
>> // that fetch the g directly (from TLS or from the dedicated register).
>> func getg() *g
>>
>>
>> So, is there any solution that I can reading what implementation does
>> the compiler rewrite? It seems that it's Assembly code.
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

gansteed

unread,
Jan 17, 2018, 1:02:30 AM1/17/18
to Ian Lance Taylor, Dave Cheney, golang-nuts
thanks

Ian Lance Taylor <ia...@golang.org>于2018年1月17日周三 上午9:53写道:
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/KgPOzaMylHo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages