简体   繁体   English

如何使用go工具编译?

[英]How to use go tool compile?

Simple code 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)
}

When I try tool compile 当我尝试工具编译时

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

I got this 我懂了

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

My kernel architecture 我的内核架构

x86_64 x86_64 x86_64 GNU/Linux

How to use go tool compile to get the proper assembly output? 如何使用go工具编译以获得正确的程序集输出?

Drop the 6g (it's now known as compile ) 删除6g(现在称为compile

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

Output 输出量

"".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
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM