如何使用go工具编译?

时间:2019-01-21 08:40:50

标签: go

简单代码recursion and tails

package main

import "fmt"

func TailRecursive(number int, product int) int {

    product = product + number

    if number == 1 {

        return product
    }

    return TailRecursive(number-1, product)
}

func main() {

    answer := TailRecursive(5, 0)
    fmt.Printf("Recursive: %d\n", answer)
}

当我尝试工具编译时

go tool compile 6g -S ./g9.go > assembly.asm

我明白了

 cat assembly.asm 
6g:0:0: open 6g: no such file or directory

我的内核架构

x86_64 x86_64 x86_64 GNU/Linux

如何使用go工具编译来获取正确的程序集输出?

1 个答案:

答案 0 :(得分:2)

丢弃6g(现在称为compile

go tool compile -S ./g9.go > assembly.asm

输出

"".TailRecursive STEXT size=107 args=0x18 locals=0x20
    0x0000 00000 (g9.go:5)  TEXT    "".TailRecursive(SB), $32-24
    0x0000 00000 (g9.go:5)  MOVQ    (TLS), CX
    0x0009 00009 (g9.go:5)  CMPQ    SP, 16(CX)
    0x000d 00013 (g9.go:5)  JLS 100
    0x000f 00015 (g9.go:5)  SUBQ    $32, SP
    0x0013 00019 (g9.go:5)  MOVQ    BP, 24(SP)
    0x0018 00024 (g9.go:5)  LEAQ    24(SP), BP
    0x001d 00029 (g9.go:5)  FUNCDATA    $0, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
    0x001d 00029 (g9.go:5)  FUNCDATA    $1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
    0x001d 00029 (g9.go:5)  FUNCDATA    $3, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
    0x001d 00029 (g9.go:7)  PCDATA  $2, $0
    0x001d 00029 (g9.go:7)  PCDATA  $0, $0
    0x001d 00029 (g9.go:7)  MOVQ    "".number+40(SP), AX
    0x0022 00034 (g9.go:7)  MOVQ    "".product+48(SP), CX
    0x0027 00039 (g9.go:7)  ADDQ    AX, CX
    0x002a 00042 (g9.go:9)  CMPQ    AX, $1
    0x002e 00046 (g9.go:9)  JNE 63
    0x0030 00048 (g9.go:11) MOVQ    CX, "".~r2+56(SP)
    0x0035 00053 (g9.go:11) MOVQ    24(SP), BP
    0x003a 00058 (g9.go:11) ADDQ    $32, SP
    0x003e 00062 (g9.go:11) RET
    ...