Skip to content

Commit bc0c077

Browse files
committed
doc: update /doc/asm compiler output example
The compiler output shown in the doc is now quite old (most of the changes happened in Go 1.5). Update it to be more like what users will actually see. Also explain how to get literal machine code again. Prompted by #30968. Change-Id: I0ce139c3fe299ccc43e85b6aca81c6e0aac1a2df Reviewed-on: https://go-review.googlesource.com/c/go/+/175757 Run-TryBot: Russ Cox <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]> Reviewed-by: Rob Pike <[email protected]>
1 parent bc588d4 commit bc0c077

File tree

1 file changed

+50
-43
lines changed

1 file changed

+50
-43
lines changed

doc/asm.html

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -57,59 +57,66 @@ <h2 id="introduction">A Quick Guide to Go's Assembler</h2>
5757
println(3)
5858
}
5959
$ GOOS=linux GOARCH=amd64 go tool compile -S x.go # or: go build -gcflags -S x.go
60-
61-
--- prog list "main" ---
62-
0000 (x.go:3) TEXT main+0(SB),$8-0
63-
0001 (x.go:3) FUNCDATA $0,gcargs·0+0(SB)
64-
0002 (x.go:3) FUNCDATA $1,gclocals·0+0(SB)
65-
0003 (x.go:4) MOVQ $3,(SP)
66-
0004 (x.go:4) PCDATA $0,$8
67-
0005 (x.go:4) CALL ,runtime.printint+0(SB)
68-
0006 (x.go:4) PCDATA $0,$-1
69-
0007 (x.go:4) PCDATA $0,$0
70-
0008 (x.go:4) CALL ,runtime.printnl+0(SB)
71-
0009 (x.go:4) PCDATA $0,$-1
72-
0010 (x.go:5) RET ,
60+
"".main STEXT size=74 args=0x0 locals=0x10
61+
0x0000 00000 (x.go:3) TEXT "".main(SB), $16-0
62+
0x0000 00000 (x.go:3) MOVQ (TLS), CX
63+
0x0009 00009 (x.go:3) CMPQ SP, 16(CX)
64+
0x000d 00013 (x.go:3) JLS 67
65+
0x000f 00015 (x.go:3) SUBQ $16, SP
66+
0x0013 00019 (x.go:3) MOVQ BP, 8(SP)
67+
0x0018 00024 (x.go:3) LEAQ 8(SP), BP
68+
0x001d 00029 (x.go:3) FUNCDATA $0, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
69+
0x001d 00029 (x.go:3) FUNCDATA $1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
70+
0x001d 00029 (x.go:3) FUNCDATA $2, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
71+
0x001d 00029 (x.go:4) PCDATA $0, $0
72+
0x001d 00029 (x.go:4) PCDATA $1, $0
73+
0x001d 00029 (x.go:4) CALL runtime.printlock(SB)
74+
0x0022 00034 (x.go:4) MOVQ $3, (SP)
75+
0x002a 00042 (x.go:4) CALL runtime.printint(SB)
76+
0x002f 00047 (x.go:4) CALL runtime.printnl(SB)
77+
0x0034 00052 (x.go:4) CALL runtime.printunlock(SB)
78+
0x0039 00057 (x.go:5) MOVQ 8(SP), BP
79+
0x003e 00062 (x.go:5) ADDQ $16, SP
80+
0x0042 00066 (x.go:5) RET
81+
0x0043 00067 (x.go:5) NOP
82+
0x0043 00067 (x.go:3) PCDATA $1, $-1
83+
0x0043 00067 (x.go:3) PCDATA $0, $-1
84+
0x0043 00067 (x.go:3) CALL runtime.morestack_noctxt(SB)
85+
0x0048 00072 (x.go:3) JMP 0
7386
...
7487
</pre>
7588

7689
<p>
7790
The <code>FUNCDATA</code> and <code>PCDATA</code> directives contain information
7891
for use by the garbage collector; they are introduced by the compiler.
79-
</p>
80-
81-
<!-- Commenting out because the feature is gone but it's popular and may come back.
92+
</p>
8293

8394
<p>
84-
To see what gets put in the binary after linking, add the <code>-a</code> flag to the linker:
95+
To see what gets put in the binary after linking, use <code>go tool objdump</code>:
8596
</p>
8697

8798
<pre>
88-
$ go tool 6l -a x.6 # or: go build -ldflags -a x.go
89-
codeblk [0x2000,0x1d059) at offset 0x1000
90-
002000 main.main | (3) TEXT main.main+0(SB),$8
91-
002000 65488b0c25a0080000 | (3) MOVQ 2208(GS),CX
92-
002009 483b21 | (3) CMPQ SP,(CX)
93-
00200c 7707 | (3) JHI ,2015
94-
00200e e83da20100 | (3) CALL ,1c250+runtime.morestack00
95-
002013 ebeb | (3) JMP ,2000
96-
002015 4883ec08 | (3) SUBQ $8,SP
97-
002019 | (3) FUNCDATA $0,main.gcargs·0+0(SB)
98-
002019 | (3) FUNCDATA $1,main.gclocals·0+0(SB)
99-
002019 48c7042403000000 | (4) MOVQ $3,(SP)
100-
002021 | (4) PCDATA $0,$8
101-
002021 e8aad20000 | (4) CALL ,f2d0+runtime.printint
102-
002026 | (4) PCDATA $0,$-1
103-
002026 | (4) PCDATA $0,$0
104-
002026 e865d40000 | (4) CALL ,f490+runtime.printnl
105-
00202b | (4) PCDATA $0,$-1
106-
00202b 4883c408 | (5) ADDQ $8,SP
107-
00202f c3 | (5) RET ,
108-
...
99+
$ go build -o x.exe x.go
100+
$ go tool objdump -s main.main x.exe
101+
TEXT main.main(SB) /tmp/x.go
102+
x.go:3 0x10501c0 65488b0c2530000000 MOVQ GS:0x30, CX
103+
x.go:3 0x10501c9 483b6110 CMPQ 0x10(CX), SP
104+
x.go:3 0x10501cd 7634 JBE 0x1050203
105+
x.go:3 0x10501cf 4883ec10 SUBQ $0x10, SP
106+
x.go:3 0x10501d3 48896c2408 MOVQ BP, 0x8(SP)
107+
x.go:3 0x10501d8 488d6c2408 LEAQ 0x8(SP), BP
108+
x.go:4 0x10501dd e86e45fdff CALL runtime.printlock(SB)
109+
x.go:4 0x10501e2 48c7042403000000 MOVQ $0x3, 0(SP)
110+
x.go:4 0x10501ea e8e14cfdff CALL runtime.printint(SB)
111+
x.go:4 0x10501ef e8ec47fdff CALL runtime.printnl(SB)
112+
x.go:4 0x10501f4 e8d745fdff CALL runtime.printunlock(SB)
113+
x.go:5 0x10501f9 488b6c2408 MOVQ 0x8(SP), BP
114+
x.go:5 0x10501fe 4883c410 ADDQ $0x10, SP
115+
x.go:5 0x1050202 c3 RET
116+
x.go:3 0x1050203 e83882ffff CALL runtime.morestack_noctxt(SB)
117+
x.go:3 0x1050208 ebb6 JMP main.main(SB)
109118
</pre>
110119

111-
-->
112-
113120
<h3 id="constants">Constants</h3>
114121

115122
<p>
@@ -266,7 +273,7 @@ <h3 id="symbols">Symbols</h3>
266273
</p>
267274

268275
<p>
269-
In Go object files and binaries, the full name of a symbol is the
276+
In Go object files and binaries, the full name of a symbol is the
270277
package path followed by a period and the symbol name:
271278
<code>fmt.Printf</code> or <code>math/rand.Int</code>.
272279
Because the assembler's parser treats period and slash as punctuation,
@@ -485,7 +492,7 @@ <h3 id="runtime">Runtime Coordination</h3>
485492
At the start of the function, the arguments are assumed
486493
to be initialized but the results are assumed uninitialized.
487494
If the results will hold live pointers during a call instruction,
488-
the function should start by zeroing the results and then
495+
the function should start by zeroing the results and then
489496
executing the pseudo-instruction <code>GO_RESULTS_INITIALIZED</code>.
490497
This instruction records that the results are now initialized
491498
and should be scanned during stack movement and garbage collection.
@@ -503,7 +510,7 @@ <h3 id="runtime">Runtime Coordination</h3>
503510
The pointer information can also be omitted if the
504511
function contains no call instructions.
505512
Otherwise, the local stack frame must not contain pointers,
506-
and the assembly must confirm this fact by executing the
513+
and the assembly must confirm this fact by executing the
507514
pseudo-instruction <code>NO_LOCAL_POINTERS</code>.
508515
Because stack resizing is implemented by moving the stack,
509516
the stack pointer may change during any function call:

0 commit comments

Comments
 (0)