Skip to content

Instantly share code, notes, and snippets.

@epy0n0ff
Created December 21, 2015 01:38
Show Gist options
  • Select an option

  • Save epy0n0ff/39abfbd5df41238914bc to your computer and use it in GitHub Desktop.

Select an option

Save epy0n0ff/39abfbd5df41238914bc to your computer and use it in GitHub Desktop.

func(_ *hoge) A()と省略しているコードがあったので気になった。

package main
import "fmt"


type Func struct {
	value int
}

func(_ *Func) A() {
	fmt.Println("A")
}

func (f *Func) B() {
	fmt.Println("B")
}


func main() {
	f := &Func{}
	f.A()
	f.B()
}

まずは省略する関数と省略しないが使わない関数の比較

:"".(*Func).A t=1 size=256 value=0 args=0x8 locals=0x78
        0x0000 00000 (receiver.go:9)    TEXT    "".(*Func).A(SB), $120-8
        0x0000 00000 (receiver.go:9)    MOVQ    (TLS), CX
        0x0009 00009 (receiver.go:9)    CMPQ    SP, 16(CX)
        0x000d 00013 (receiver.go:9)    JLS     236
        0x0013 00019 (receiver.go:9)    SUBQ    $120, SP
        0x0017 00023 (receiver.go:9)    FUNCDATA        $0, gclocals·37a2283f5c69c342946cad8073b58fca(SB)
        0x0017 00023 (receiver.go:9)    FUNCDATA        $1, gclocals·e686e33109a1bc2792301626c7b401c5(SB)
        0x0017 00023 (receiver.go:10)   LEAQ    go.string."A"(SB), BX
        0x001e 00030 (receiver.go:10)   MOVQ    BX, "".autotmp_0000+80(SP)
        0x0023 00035 (receiver.go:10)   MOVQ    $1, "".autotmp_0000+88(SP)
        0x002c 00044 (receiver.go:10)   MOVQ    $0, BX
        0x002e 00046 (receiver.go:10)   MOVQ    BX, "".autotmp_0004+64(SP)
        0x0033 00051 (receiver.go:10)   MOVQ    BX, "".autotmp_0004+72(SP)
        0x0038 00056 (receiver.go:10)   LEAQ    "".autotmp_0004+64(SP), BX
        0x003d 00061 (receiver.go:10)   CMPQ    BX, $0
        0x0041 00065 (receiver.go:10)   JEQ     $1, 229
        0x0047 00071 (receiver.go:10)   MOVQ    $1, "".autotmp_0001+104(SP)
        0x0050 00080 (receiver.go:10)   MOVQ    $1, "".autotmp_0001+112(SP)
        0x0059 00089 (receiver.go:10)   MOVQ    BX, "".autotmp_0001+96(SP)
        0x005e 00094 (receiver.go:10)   LEAQ    type.string(SB), BX
        0x0065 00101 (receiver.go:10)   MOVQ    BX, (SP)
        0x0069 00105 (receiver.go:10)   LEAQ    "".autotmp_0000+80(SP), BX
        0x006e 00110 (receiver.go:10)   MOVQ    BX, 8(SP)
        0x0073 00115 (receiver.go:10)   MOVQ    $0, 16(SP)
        0x007c 00124 (receiver.go:10)   PCDATA  $0, $1
        0x007c 00124 (receiver.go:10)   CALL    runtime.convT2E(SB)
        0x0081 00129 (receiver.go:10)   MOVQ    24(SP), CX
        0x0086 00134 (receiver.go:10)   MOVQ    32(SP), AX
        0x008b 00139 (receiver.go:10)   MOVQ    "".autotmp_0001+96(SP), BX
        0x0090 00144 (receiver.go:10)   MOVQ    CX, "".autotmp_0005+48(SP)
        0x0095 00149 (receiver.go:10)   MOVQ    CX, (BX)
        0x0098 00152 (receiver.go:10)   MOVQ    AX, "".autotmp_0005+56(SP)
        0x009d 00157 (receiver.go:10)   CMPB    runtime.writeBarrierEnabled(SB), $0
        0x00a4 00164 (receiver.go:10)   JNE     $0, 209
        0x00a6 00166 (receiver.go:10)   MOVQ    AX, 8(BX)
        0x00aa 00170 (receiver.go:10)   MOVQ    "".autotmp_0001+96(SP), BX
        0x00af 00175 (receiver.go:10)   MOVQ    BX, (SP)
        0x00b3 00179 (receiver.go:10)   MOVQ    "".autotmp_0001+104(SP), BX
        0x00b8 00184 (receiver.go:10)   MOVQ    BX, 8(SP)
        0x00bd 00189 (receiver.go:10)   MOVQ    "".autotmp_0001+112(SP), BX
        0x00c2 00194 (receiver.go:10)   MOVQ    BX, 16(SP)
        0x00c7 00199 (receiver.go:10)   PCDATA  $0, $2
        0x00c7 00199 (receiver.go:10)   CALL    fmt.Println(SB)
        0x00cc 00204 (receiver.go:11)   ADDQ    $120, SP
        0x00d0 00208 (receiver.go:11)   RET
        0x00d1 00209 (receiver.go:10)   LEAQ    8(BX), R8
        0x00d5 00213 (receiver.go:10)   MOVQ    R8, (SP)
        0x00d9 00217 (receiver.go:10)   MOVQ    AX, 8(SP)
        0x00de 00222 (receiver.go:10)   PCDATA  $0, $1
        0x00de 00222 (receiver.go:10)   CALL    runtime.writebarrierptr(SB)
        0x00e3 00227 (receiver.go:10)   JMP     170
        0x00e5 00229 (receiver.go:10)   MOVL    AX, (BX)
        0x00e7 00231 (receiver.go:10)   JMP     71
        0x00ec 00236 (receiver.go:9)    CALL    runtime.morestack_noctxt(SB)
        0x00f1 00241 (receiver.go:9)    JMP     0

"".(*Func).B t=1 size=256 value=0 args=0x8 locals=0x78
        0x0000 00000 (receiver.go:13)   TEXT    "".(*Func).B(SB), $120-8
        0x0000 00000 (receiver.go:13)   MOVQ    (TLS), CX
        0x0009 00009 (receiver.go:13)   CMPQ    SP, 16(CX)
        0x000d 00013 (receiver.go:13)   JLS     236
        0x0013 00019 (receiver.go:13)   SUBQ    $120, SP
        0x0017 00023 (receiver.go:13)   FUNCDATA        $0, gclocals·37a2283f5c69c342946cad8073b58fca(SB)
        0x0017 00023 (receiver.go:13)   FUNCDATA        $1, gclocals·e686e33109a1bc2792301626c7b401c5(SB)
        0x0017 00023 (receiver.go:14)   LEAQ    go.string."B"(SB), BX
        0x001e 00030 (receiver.go:14)   MOVQ    BX, "".autotmp_0006+80(SP)
        0x0023 00035 (receiver.go:14)   MOVQ    $1, "".autotmp_0006+88(SP)
        0x002c 00044 (receiver.go:14)   MOVQ    $0, BX
        0x002e 00046 (receiver.go:14)   MOVQ    BX, "".autotmp_0010+64(SP)
        0x0033 00051 (receiver.go:14)   MOVQ    BX, "".autotmp_0010+72(SP)
        0x0038 00056 (receiver.go:14)   LEAQ    "".autotmp_0010+64(SP), BX
        0x003d 00061 (receiver.go:14)   CMPQ    BX, $0
        0x0041 00065 (receiver.go:14)   JEQ     $1, 229
        0x0047 00071 (receiver.go:14)   MOVQ    $1, "".autotmp_0007+104(SP)
        0x0050 00080 (receiver.go:14)   MOVQ    $1, "".autotmp_0007+112(SP)
        0x0059 00089 (receiver.go:14)   MOVQ    BX, "".autotmp_0007+96(SP)
        0x005e 00094 (receiver.go:14)   LEAQ    type.string(SB), BX
        0x0065 00101 (receiver.go:14)   MOVQ    BX, (SP)
        0x0069 00105 (receiver.go:14)   LEAQ    "".autotmp_0006+80(SP), BX
        0x006e 00110 (receiver.go:14)   MOVQ    BX, 8(SP)
        0x0073 00115 (receiver.go:14)   MOVQ    $0, 16(SP)
        0x007c 00124 (receiver.go:14)   PCDATA  $0, $1
        0x007c 00124 (receiver.go:14)   CALL    runtime.convT2E(SB)
        0x0081 00129 (receiver.go:14)   MOVQ    24(SP), CX
        0x0086 00134 (receiver.go:14)   MOVQ    32(SP), AX
        0x008b 00139 (receiver.go:14)   MOVQ    "".autotmp_0007+96(SP), BX
        0x0090 00144 (receiver.go:14)   MOVQ    CX, "".autotmp_0011+48(SP)
        0x0095 00149 (receiver.go:14)   MOVQ    CX, (BX)
        0x0098 00152 (receiver.go:14)   MOVQ    AX, "".autotmp_0011+56(SP)
        0x009d 00157 (receiver.go:14)   CMPB    runtime.writeBarrierEnabled(SB), $0
        0x00a4 00164 (receiver.go:14)   JNE     $0, 209
        0x00a6 00166 (receiver.go:14)   MOVQ    AX, 8(BX)
        0x00aa 00170 (receiver.go:14)   MOVQ    "".autotmp_0007+96(SP), BX
        0x00af 00175 (receiver.go:14)   MOVQ    BX, (SP)
        0x00b3 00179 (receiver.go:14)   MOVQ    "".autotmp_0007+104(SP), BX
        0x00b8 00184 (receiver.go:14)   MOVQ    BX, 8(SP)
        0x00bd 00189 (receiver.go:14)   MOVQ    "".autotmp_0007+112(SP), BX
        0x00c2 00194 (receiver.go:14)   MOVQ    BX, 16(SP)
        0x00c7 00199 (receiver.go:14)   PCDATA  $0, $2
        0x00c7 00199 (receiver.go:14)   CALL    fmt.Println(SB)
        0x00cc 00204 (receiver.go:15)   ADDQ    $120, SP
        0x00d0 00208 (receiver.go:15)   RET
        0x00d1 00209 (receiver.go:14)   LEAQ    8(BX), R8
        0x00d5 00213 (receiver.go:14)   MOVQ    R8, (SP)
        0x00d9 00217 (receiver.go:14)   MOVQ    AX, 8(SP)
        0x00de 00222 (receiver.go:14)   PCDATA  $0, $1
        0x00de 00222 (receiver.go:14)   CALL    runtime.writebarrierptr(SB)
        0x00e3 00227 (receiver.go:14)   JMP     170
        0x00e5 00229 (receiver.go:14)   MOVL    AX, (BX)
        0x00e7 00231 (receiver.go:14)   JMP     71
        0x00ec 00236 (receiver.go:13)   CALL    runtime.morestack_noctxt(SB)
        0x00f1 00241 (receiver.go:13)   JMP     0

次に省略せずポインタを使うパターン。

package main
import "fmt"


type Func struct {
	value int
}

func(_ *Func) A() {
	fmt.Println("A")
}

func (f *Func) B() {
	fmt.Println(f.value)
}


func main() {
	f := &Func{}
	f.A()
	f.B()
}

Aは同じなのでBのみ記載。

"".(*Func).B t=1 size=256 value=0 args=0x8 locals=0x68
        0x0000 00000 (receiver.go:13)   TEXT    "".(*Func).B(SB), $104-8
        0x0000 00000 (receiver.go:13)   MOVQ    (TLS), CX
        0x0009 00009 (receiver.go:13)   CMPQ    SP, 16(CX)
        0x000d 00013 (receiver.go:13)   JLS     232
        0x0013 00019 (receiver.go:13)   SUBQ    $104, SP
        0x0017 00023 (receiver.go:13)   FUNCDATA        $0, gclocals·37a2283f5c69c342946cad8073b58fca(SB)
        0x0017 00023 (receiver.go:13)   FUNCDATA        $1, gclocals·5ef976c2593056b9243adf402ae9d952(SB)
        0x0017 00023 (receiver.go:14)   MOVQ    $0, BX
        0x0019 00025 (receiver.go:14)   MOVQ    BX, "".autotmp_0009+64(SP)
        0x001e 00030 (receiver.go:14)   MOVQ    BX, "".autotmp_0009+72(SP)
        0x0023 00035 (receiver.go:14)   LEAQ    "".autotmp_0009+64(SP), BX
        0x0028 00040 (receiver.go:14)   CMPQ    BX, $0
        0x002c 00044 (receiver.go:14)   JEQ     $1, 225
        0x0032 00050 (receiver.go:14)   MOVQ    $1, "".autotmp_0006+88(SP)
        0x003b 00059 (receiver.go:14)   MOVQ    $1, "".autotmp_0006+96(SP)
        0x0044 00068 (receiver.go:14)   MOVQ    BX, "".autotmp_0006+80(SP)
        0x0049 00073 (receiver.go:14)   LEAQ    type.int(SB), BX
        0x0050 00080 (receiver.go:14)   MOVQ    BX, (SP)
        0x0054 00084 (receiver.go:14)   MOVQ    "".f+112(FP), BX
        0x0059 00089 (receiver.go:14)   MOVQ    BX, 8(SP)
        0x005e 00094 (receiver.go:14)   CMPQ    8(SP), $0
        0x0064 00100 (receiver.go:14)   JEQ     $1, 216
        0x0066 00102 (receiver.go:14)   MOVQ    $0, 16(SP)
        0x006f 00111 (receiver.go:14)   PCDATA  $0, $1
        0x006f 00111 (receiver.go:14)   CALL    runtime.convT2E(SB)
        0x0074 00116 (receiver.go:14)   MOVQ    24(SP), CX
        0x0079 00121 (receiver.go:14)   MOVQ    32(SP), AX
        0x007e 00126 (receiver.go:14)   MOVQ    "".autotmp_0006+80(SP), BX
        0x0083 00131 (receiver.go:14)   MOVQ    CX, "".autotmp_0010+48(SP)
        0x0088 00136 (receiver.go:14)   MOVQ    CX, (BX)
        0x008b 00139 (receiver.go:14)   MOVQ    AX, "".autotmp_0010+56(SP)
        0x0090 00144 (receiver.go:14)   CMPB    runtime.writeBarrierEnabled(SB), $0
        0x0097 00151 (receiver.go:14)   JNE     $0, 196
        0x0099 00153 (receiver.go:14)   MOVQ    AX, 8(BX)
        0x009d 00157 (receiver.go:14)   MOVQ    "".autotmp_0006+80(SP), BX
        0x00a2 00162 (receiver.go:14)   MOVQ    BX, (SP)
        0x00a6 00166 (receiver.go:14)   MOVQ    "".autotmp_0006+88(SP), BX
        0x00ab 00171 (receiver.go:14)   MOVQ    BX, 8(SP)
        0x00b0 00176 (receiver.go:14)   MOVQ    "".autotmp_0006+96(SP), BX
        0x00b5 00181 (receiver.go:14)   MOVQ    BX, 16(SP)
        0x00ba 00186 (receiver.go:14)   PCDATA  $0, $2
        0x00ba 00186 (receiver.go:14)   CALL    fmt.Println(SB)
        0x00bf 00191 (receiver.go:15)   ADDQ    $104, SP
        0x00c3 00195 (receiver.go:15)   RET
        0x00c4 00196 (receiver.go:14)   LEAQ    8(BX), R8
        0x00c8 00200 (receiver.go:14)   MOVQ    R8, (SP)
        0x00cc 00204 (receiver.go:14)   MOVQ    AX, 8(SP)
        0x00d1 00209 (receiver.go:14)   PCDATA  $0, $1
        0x00d1 00209 (receiver.go:14)   CALL    runtime.writebarrierptr(SB)
        0x00d6 00214 (receiver.go:14)   JMP     157
        0x00d8 00216 (receiver.go:14)   MOVL    AX, 0
        0x00df 00223 (receiver.go:14)   JMP     102
        0x00e1 00225 (receiver.go:14)   MOVL    AX, (BX)
        0x00e3 00227 (receiver.go:14)   JMP     50
        0x00e8 00232 (receiver.go:13)   CALL    runtime.morestack_noctxt(SB)
        0x00ed 00237 (receiver.go:13)   JMP     0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment