英文:
Where is g struct define in golang source code?
问题
在golang的源代码中,有一些代码如下:
MOVL (g_sched+gobuf_sp)(SI), SP // sp = m->g0->sched.sp
在runtime/stubs.go
中,我找到了以下代码:
func getg() *g
但是我找不到getg
函数或g
结构体,它们在哪里?
英文:
In golang source code, there is some code like:
MOVL (g_sched+gobuf_sp)(SI), SP // sp = m->g0->sched.sp
And in runtime/stubs.go
I found
func getg() *g
But i can not find getg
function or g
struct, where is it?
答案1
得分: 3
这个结构体在runtime2.go
中。
> type g struct {
> // Stack parameters.
> // stack描述了实际的堆栈内存:[stack.lo, stack.hi)。
> // stackguard0是在Go堆栈增长序言中与堆栈指针进行比较的。
你可以使用godef
快速回答这类问题。
函数的注释描述了它的工作原理:
> 编译器将对此函数的调用重写为直接获取g的指令(从TLS或专用寄存器获取)。
要了解编译器是如何实现这一点的,请查看cmd/compile/internal/gc/typecheck.go
:
> if ..... && n.Left.Sym.Name == "getg" { ...
英文:
The struct is in runtime2.go
.
> type g struct {
> // Stack parameters.
> // stack describes the actual stack memory: [stack.lo, stack.hi).
> // stackguard0 is the stack pointer compared in the Go stack growth prologue.
You can use godef
to quickly answer questions like that on your own.
The function's comment describes how that works:
> The compiler rewrites calls to this function into instructions
> that fetch the g directly (from TLS or from the dedicated register).
To see how the compiler does this, check cmd/compile/internal/gc/typecheck.go
:
> if ..... && n.Left.Sym.Name == "getg" { ...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论