Skip to content

Commit 7c03fe7

Browse files
aclementsgopherbot
authored andcommitted
cmd/compile: improve compiler directive docs
This section has gotten long enough that it deserves to be multiple sections. This also allows us to better structure information shared by subsets of directives. In particular, this enables a self-contained section on the wasm directives. Updates #66984. Change-Id: I062081d46c6b0aef7887fdaf9efae80f32ad4b21 Reviewed-on: https://go-review.googlesource.com/c/go/+/638881 LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Austin Clements <[email protected]> Reviewed-by: Cherry Mui <[email protected]>
1 parent d7c3e93 commit 7c03fe7

File tree

1 file changed

+40
-29
lines changed

1 file changed

+40
-29
lines changed

src/cmd/compile/doc.go

+40-29
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ the package and about types used by symbols imported by the package from
1515
other packages. It is therefore not necessary when compiling client C of
1616
package P to read the files of P's dependencies, only the compiled output of P.
1717
18-
Command Line
18+
# Command Line
1919
2020
Usage:
2121
@@ -150,14 +150,21 @@ Flags to debug the compiler itself:
150150
-w
151151
Debug type checking.
152152
153-
Compiler Directives
153+
# Compiler Directives
154154
155155
The compiler accepts directives in the form of comments.
156-
To distinguish them from non-directive comments, directives
157-
require no space between the comment opening and the name of the directive. However, since
158-
they are comments, tools unaware of the directive convention or of a particular
156+
Each directive must be placed its own line, with only leading spaces and tabs
157+
allowed before the comment, and there must be no space between the comment
158+
opening and the name of the directive, to distinguish it from a regular comment.
159+
Tools unaware of the directive convention or of a particular
159160
directive can skip over a directive like any other comment.
161+
162+
Other than the line directive, which is a historical special case;
163+
all other compiler directives are of the form
164+
//go:name, indicating that they are defined by the Go toolchain.
160165
*/
166+
// # Line Directives
167+
//
161168
// Line directives come in several forms:
162169
//
163170
// //line :line
@@ -197,12 +204,9 @@ directive can skip over a directive like any other comment.
197204
// Line directives typically appear in machine-generated code, so that compilers and debuggers
198205
// will report positions in the original input to the generator.
199206
/*
200-
The line directive is a historical special case; all other directives are of the form
201-
//go:name, indicating that they are defined by the Go toolchain.
202-
Each directive must be placed its own line, with only leading spaces and tabs
203-
allowed before the comment.
204-
Each directive applies to the Go code that immediately follows it,
205-
which typically must be a declaration.
207+
# Function Directives
208+
209+
A function directive applies to the Go function that immediately follows it.
206210
207211
//go:noescape
208212
@@ -245,6 +249,8 @@ It specifies that the function must omit its usual stack overflow check.
245249
This is most commonly used by low-level runtime code invoked
246250
at times when it is unsafe for the calling goroutine to be preempted.
247251
252+
# Linkname Directive
253+
248254
//go:linkname localname [importpath.name]
249255
250256
The //go:linkname directive conventionally precedes the var or func
@@ -295,17 +301,34 @@ The declaration of lower.f may also have a linkname directive with a
295301
single argument, f. This is optional, but helps alert the reader that
296302
the function is accessed from outside the package.
297303
304+
# WebAssembly Directives
305+
298306
//go:wasmimport importmodule importname
299307
300308
The //go:wasmimport directive is wasm-only and must be followed by a
301-
function declaration.
309+
function declaration with no body.
302310
It specifies that the function is provided by a wasm module identified
303-
by ``importmodule`` and ``importname``.
311+
by ``importmodule'' and ``importname''. For example,
304312
305313
//go:wasmimport a_module f
306314
func g()
307315
308-
The types of parameters and return values to the Go function are translated to
316+
causes g to refer to the WebAssembly function f from module a_module.
317+
318+
//go:wasmexport exportname
319+
320+
The //go:wasmexport directive is wasm-only and must be followed by a
321+
function definition.
322+
It specifies that the function is exported to the wasm host as ``exportname''.
323+
For example,
324+
325+
//go:wasmexport h
326+
func hWasm() { ... }
327+
328+
make Go function hWasm available outside this WebAssembly module as h.
329+
330+
For both go:wasmimport and go:wasmexport,
331+
the types of parameters and return values to the Go function are translated to
309332
Wasm according to the following table:
310333
311334
Go types Wasm types
@@ -318,24 +341,12 @@ Wasm according to the following table:
318341
pointer i32 (more restrictions below)
319342
string (i32, i32) (only permitted as a parameters, not a result)
320343
344+
Any other parameter types are disallowed by the compiler.
345+
321346
For a pointer type, its element type must be a bool, int8, uint8, int16, uint16,
322347
int32, uint32, int64, uint64, float32, float64, an array whose element type is
323348
a permitted pointer element type, or a struct, which, if non-empty, embeds
324-
structs.HostLayout, and contains only fields whose types are permitted pointer
349+
[structs.HostLayout], and contains only fields whose types are permitted pointer
325350
element types.
326-
327-
Any other parameter types are disallowed by the compiler.
328-
329-
//go:wasmexport exportname
330-
331-
The //go:wasmexport directive is wasm-only and must be followed by a
332-
function definition.
333-
It specifies that the function is exported to the wasm host as ``exportname``.
334-
335-
//go:wasmexport f
336-
func g()
337-
338-
The types of parameters and return values to the Go function are permitted and
339-
translated to Wasm in the same way as //go:wasmimport functions.
340351
*/
341352
package main

0 commit comments

Comments
 (0)