annotate gcc/go/gccgo.texi @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 \input texinfo @c -*-texinfo-*-
kono
parents:
diff changeset
2 @setfilename gccgo.info
kono
parents:
diff changeset
3 @settitle The GNU Go Compiler
kono
parents:
diff changeset
4
kono
parents:
diff changeset
5 @c Merge the standard indexes into a single one.
kono
parents:
diff changeset
6 @syncodeindex fn cp
kono
parents:
diff changeset
7 @syncodeindex vr cp
kono
parents:
diff changeset
8 @syncodeindex ky cp
kono
parents:
diff changeset
9 @syncodeindex pg cp
kono
parents:
diff changeset
10 @syncodeindex tp cp
kono
parents:
diff changeset
11
kono
parents:
diff changeset
12 @include gcc-common.texi
kono
parents:
diff changeset
13
kono
parents:
diff changeset
14 @c Copyright years for this manual.
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
15 @set copyrights-go 2010-2018
111
kono
parents:
diff changeset
16
kono
parents:
diff changeset
17 @copying
kono
parents:
diff changeset
18 @c man begin COPYRIGHT
kono
parents:
diff changeset
19 Copyright @copyright{} @value{copyrights-go} Free Software Foundation, Inc.
kono
parents:
diff changeset
20
kono
parents:
diff changeset
21 Permission is granted to copy, distribute and/or modify this document
kono
parents:
diff changeset
22 under the terms of the GNU Free Documentation License, Version 1.3 or
kono
parents:
diff changeset
23 any later version published by the Free Software Foundation; with no
kono
parents:
diff changeset
24 Invariant Sections, the Front-Cover Texts being (a) (see below), and
kono
parents:
diff changeset
25 with the Back-Cover Texts being (b) (see below).
kono
parents:
diff changeset
26 A copy of the license is included in the
kono
parents:
diff changeset
27 @c man end
kono
parents:
diff changeset
28 section entitled ``GNU Free Documentation License''.
kono
parents:
diff changeset
29 @ignore
kono
parents:
diff changeset
30 @c man begin COPYRIGHT
kono
parents:
diff changeset
31 man page gfdl(7).
kono
parents:
diff changeset
32 @c man end
kono
parents:
diff changeset
33 @end ignore
kono
parents:
diff changeset
34
kono
parents:
diff changeset
35 @c man begin COPYRIGHT
kono
parents:
diff changeset
36
kono
parents:
diff changeset
37 (a) The FSF's Front-Cover Text is:
kono
parents:
diff changeset
38
kono
parents:
diff changeset
39 A GNU Manual
kono
parents:
diff changeset
40
kono
parents:
diff changeset
41 (b) The FSF's Back-Cover Text is:
kono
parents:
diff changeset
42
kono
parents:
diff changeset
43 You have freedom to copy and modify this GNU Manual, like GNU
kono
parents:
diff changeset
44 software. Copies published by the Free Software Foundation raise
kono
parents:
diff changeset
45 funds for GNU development.
kono
parents:
diff changeset
46 @c man end
kono
parents:
diff changeset
47 @end copying
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49 @ifinfo
kono
parents:
diff changeset
50 @format
kono
parents:
diff changeset
51 @dircategory Software development
kono
parents:
diff changeset
52 @direntry
kono
parents:
diff changeset
53 * Gccgo: (gccgo). A GCC-based compiler for the Go language
kono
parents:
diff changeset
54 @end direntry
kono
parents:
diff changeset
55 @end format
kono
parents:
diff changeset
56
kono
parents:
diff changeset
57 @insertcopying
kono
parents:
diff changeset
58 @end ifinfo
kono
parents:
diff changeset
59
kono
parents:
diff changeset
60 @titlepage
kono
parents:
diff changeset
61 @title The GNU Go Compiler
kono
parents:
diff changeset
62 @versionsubtitle
kono
parents:
diff changeset
63 @author Ian Lance Taylor
kono
parents:
diff changeset
64
kono
parents:
diff changeset
65 @page
kono
parents:
diff changeset
66 @vskip 0pt plus 1filll
kono
parents:
diff changeset
67 Published by the Free Software Foundation @*
kono
parents:
diff changeset
68 51 Franklin Street, Fifth Floor@*
kono
parents:
diff changeset
69 Boston, MA 02110-1301, USA@*
kono
parents:
diff changeset
70 @sp 1
kono
parents:
diff changeset
71 @insertcopying
kono
parents:
diff changeset
72 @end titlepage
kono
parents:
diff changeset
73 @contents
kono
parents:
diff changeset
74 @page
kono
parents:
diff changeset
75
kono
parents:
diff changeset
76 @node Top
kono
parents:
diff changeset
77 @top Introduction
kono
parents:
diff changeset
78
kono
parents:
diff changeset
79 This manual describes how to use @command{gccgo}, the GNU compiler for
kono
parents:
diff changeset
80 the Go programming language. This manual is specifically about
kono
parents:
diff changeset
81 @command{gccgo}. For more information about the Go programming
kono
parents:
diff changeset
82 language in general, including language specifications and standard
kono
parents:
diff changeset
83 package documentation, see @uref{http://golang.org/}.
kono
parents:
diff changeset
84
kono
parents:
diff changeset
85 @menu
kono
parents:
diff changeset
86 * Copying:: The GNU General Public License.
kono
parents:
diff changeset
87 * GNU Free Documentation License::
kono
parents:
diff changeset
88 How you can share and copy this manual.
kono
parents:
diff changeset
89 * Invoking gccgo:: How to run gccgo.
kono
parents:
diff changeset
90 * Import and Export:: Importing and exporting package data.
kono
parents:
diff changeset
91 * Compiler Directives:: Comments to control compilation.
kono
parents:
diff changeset
92 * C Interoperability:: Calling C from Go and vice-versa.
kono
parents:
diff changeset
93 * Index:: Index.
kono
parents:
diff changeset
94 @end menu
kono
parents:
diff changeset
95
kono
parents:
diff changeset
96
kono
parents:
diff changeset
97 @include gpl_v3.texi
kono
parents:
diff changeset
98
kono
parents:
diff changeset
99 @include fdl.texi
kono
parents:
diff changeset
100
kono
parents:
diff changeset
101
kono
parents:
diff changeset
102 @node Invoking gccgo
kono
parents:
diff changeset
103 @chapter Invoking gccgo
kono
parents:
diff changeset
104
kono
parents:
diff changeset
105 @c man title gccgo A GCC-based compiler for the Go language
kono
parents:
diff changeset
106
kono
parents:
diff changeset
107 @ignore
kono
parents:
diff changeset
108 @c man begin SYNOPSIS gccgo
kono
parents:
diff changeset
109 gccgo [@option{-c}|@option{-S}]
kono
parents:
diff changeset
110 [@option{-g}] [@option{-pg}] [@option{-O}@var{level}]
kono
parents:
diff changeset
111 [@option{-I}@var{dir}@dots{}] [@option{-L}@var{dir}@dots{}]
kono
parents:
diff changeset
112 [@option{-o} @var{outfile}] @var{infile}@dots{}
kono
parents:
diff changeset
113
kono
parents:
diff changeset
114 Only the most useful options are listed here; see below for the
kono
parents:
diff changeset
115 remainder.
kono
parents:
diff changeset
116 @c man end
kono
parents:
diff changeset
117 @c man begin SEEALSO
kono
parents:
diff changeset
118 gpl(7), gfdl(7), fsf-funding(7), gcc(1)
kono
parents:
diff changeset
119 and the Info entries for @file{gccgo} and @file{gcc}.
kono
parents:
diff changeset
120 @c man end
kono
parents:
diff changeset
121 @end ignore
kono
parents:
diff changeset
122
kono
parents:
diff changeset
123 @c man begin DESCRIPTION gccgo
kono
parents:
diff changeset
124
kono
parents:
diff changeset
125 The @command{gccgo} command is a frontend to @command{gcc} and
kono
parents:
diff changeset
126 supports many of the same options. @xref{Option Summary, , Option
kono
parents:
diff changeset
127 Summary, gcc, Using the GNU Compiler Collection (GCC)}. This manual
kono
parents:
diff changeset
128 only documents the options specific to @command{gccgo}.
kono
parents:
diff changeset
129
kono
parents:
diff changeset
130 The @command{gccgo} command may be used to compile Go source code into
kono
parents:
diff changeset
131 an object file, link a collection of object files together, or do both
kono
parents:
diff changeset
132 in sequence.
kono
parents:
diff changeset
133
kono
parents:
diff changeset
134 Go source code is compiled as packages. A package consists of one or
kono
parents:
diff changeset
135 more Go source files. All the files in a single package must be
kono
parents:
diff changeset
136 compiled together, by passing all the files as arguments to
kono
parents:
diff changeset
137 @command{gccgo}. A single invocation of @command{gccgo} may only
kono
parents:
diff changeset
138 compile a single package.
kono
parents:
diff changeset
139
kono
parents:
diff changeset
140 One Go package may @code{import} a different Go package. The imported
kono
parents:
diff changeset
141 package must have already been compiled; @command{gccgo} will read
kono
parents:
diff changeset
142 the import data directly from the compiled package. When this package
kono
parents:
diff changeset
143 is later linked, the compiled form of the package must be included in
kono
parents:
diff changeset
144 the link command.
kono
parents:
diff changeset
145
kono
parents:
diff changeset
146 Go programs must generally be compiled with debugging information, and
kono
parents:
diff changeset
147 @option{-g1} is the default as described below. Stripping a Go
kono
parents:
diff changeset
148 program will generally cause it to misbehave or fail.
kono
parents:
diff changeset
149
kono
parents:
diff changeset
150 @c man end
kono
parents:
diff changeset
151
kono
parents:
diff changeset
152 @c man begin OPTIONS gccgo
kono
parents:
diff changeset
153
kono
parents:
diff changeset
154 @table @gcctabopt
kono
parents:
diff changeset
155 @item -I@var{dir}
kono
parents:
diff changeset
156 @cindex @option{-I}
kono
parents:
diff changeset
157 Specify a directory to use when searching for an import package at
kono
parents:
diff changeset
158 compile time.
kono
parents:
diff changeset
159
kono
parents:
diff changeset
160 @item -L@var{dir}
kono
parents:
diff changeset
161 @cindex @option{-L}
kono
parents:
diff changeset
162 When linking, specify a library search directory, as with
kono
parents:
diff changeset
163 @command{gcc}.
kono
parents:
diff changeset
164
kono
parents:
diff changeset
165 @item -fgo-pkgpath=@var{string}
kono
parents:
diff changeset
166 @cindex @option{-fgo-pkgpath}
kono
parents:
diff changeset
167 Set the package path to use. This sets the value returned by the
kono
parents:
diff changeset
168 PkgPath method of reflect.Type objects. It is also used for the names
kono
parents:
diff changeset
169 of globally visible symbols. The argument to this option should
kono
parents:
diff changeset
170 normally be the string that will be used to import this package after
kono
parents:
diff changeset
171 it has been installed; in other words, a pathname within the
kono
parents:
diff changeset
172 directories specified by the @option{-I} option.
kono
parents:
diff changeset
173
kono
parents:
diff changeset
174 @item -fgo-prefix=@var{string}
kono
parents:
diff changeset
175 @cindex @option{-fgo-prefix}
kono
parents:
diff changeset
176 An alternative to @option{-fgo-pkgpath}. The argument will be
kono
parents:
diff changeset
177 combined with the package name from the source file to produce the
kono
parents:
diff changeset
178 package path. If @option{-fgo-pkgpath} is used, @option{-fgo-prefix}
kono
parents:
diff changeset
179 will be ignored.
kono
parents:
diff changeset
180
kono
parents:
diff changeset
181 Go permits a single program to include more than one package with the
kono
parents:
diff changeset
182 same name in the @code{package} clause in the source file, though
kono
parents:
diff changeset
183 obviously the two packages must be imported using different pathnames.
kono
parents:
diff changeset
184 In order for this to work with @command{gccgo}, either
kono
parents:
diff changeset
185 @option{-fgo-pkgpath} or @option{-fgo-prefix} must be specified when
kono
parents:
diff changeset
186 compiling a package.
kono
parents:
diff changeset
187
kono
parents:
diff changeset
188 Using either @option{-fgo-pkgpath} or @option{-fgo-prefix} disables
kono
parents:
diff changeset
189 the special treatment of the @code{main} package and permits that
kono
parents:
diff changeset
190 package to be imported like any other.
kono
parents:
diff changeset
191
kono
parents:
diff changeset
192 @item -fgo-relative-import-path=@var{dir}
kono
parents:
diff changeset
193 @cindex @option{-fgo-relative-import-path}
kono
parents:
diff changeset
194 A relative import is an import that starts with @file{./} or
kono
parents:
diff changeset
195 @file{../}. If this option is used, @command{gccgo} will use
kono
parents:
diff changeset
196 @var{dir} as a prefix for the relative import when searching for it.
kono
parents:
diff changeset
197
kono
parents:
diff changeset
198 @item -frequire-return-statement
kono
parents:
diff changeset
199 @itemx -fno-require-return-statement
kono
parents:
diff changeset
200 @cindex @option{-frequire-return-statement}
kono
parents:
diff changeset
201 @cindex @option{-fno-require-return-statement}
kono
parents:
diff changeset
202 By default @command{gccgo} will warn about functions which have one or
kono
parents:
diff changeset
203 more return parameters but lack an explicit @code{return} statement.
kono
parents:
diff changeset
204 This warning may be disabled using
kono
parents:
diff changeset
205 @option{-fno-require-return-statement}.
kono
parents:
diff changeset
206
kono
parents:
diff changeset
207 @item -fgo-check-divide-zero
kono
parents:
diff changeset
208 @cindex @option{-fgo-check-divide-zero}
kono
parents:
diff changeset
209 @cindex @option{-fno-go-check-divide-zero}
kono
parents:
diff changeset
210 Add explicit checks for division by zero. In Go a division (or
kono
parents:
diff changeset
211 modulos) by zero causes a panic. On Unix systems this is detected in
kono
parents:
diff changeset
212 the runtime by catching the @code{SIGFPE} signal. Some processors,
kono
parents:
diff changeset
213 such as PowerPC, do not generate a SIGFPE on division by zero. Some
kono
parents:
diff changeset
214 runtimes do not generate a signal that can be caught. On those
kono
parents:
diff changeset
215 systems, this option may be used. Or the checks may be removed via
kono
parents:
diff changeset
216 @option{-fno-go-check-divide-zero}. This option is currently on by
kono
parents:
diff changeset
217 default, but in the future may be off by default on systems that do
kono
parents:
diff changeset
218 not require it.
kono
parents:
diff changeset
219
kono
parents:
diff changeset
220 @item -fgo-check-divide-overflow
kono
parents:
diff changeset
221 @cindex @option{-fgo-check-divide-overflow}
kono
parents:
diff changeset
222 @cindex @option{-fno-go-check-divide-overflow}
kono
parents:
diff changeset
223 Add explicit checks for division overflow. For example, division
kono
parents:
diff changeset
224 overflow occurs when computing @code{INT_MIN / -1}. In Go this should
kono
parents:
diff changeset
225 be wrapped, to produce @code{INT_MIN}. Some processors, such as x86,
kono
parents:
diff changeset
226 generate a trap on division overflow. On those systems, this option
kono
parents:
diff changeset
227 may be used. Or the checks may be removed via
kono
parents:
diff changeset
228 @option{-fno-go-check-divide-overflow}. This option is currently on
kono
parents:
diff changeset
229 by default, but in the future may be off by default on systems that do
kono
parents:
diff changeset
230 not require it.
kono
parents:
diff changeset
231
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
232 @item -fno-go-optimize-allocs
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
233 @cindex @option{-fno-go-optimize-allocs}
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
234 Disable escape analysis, which tries to allocate objects on the stack
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
235 rather than the heap.
111
kono
parents:
diff changeset
236
kono
parents:
diff changeset
237 @item -fgo-debug-escape@var{n}
kono
parents:
diff changeset
238 @cindex @option{-fgo-debug-escape}
kono
parents:
diff changeset
239 Output escape analysis debugging information. Larger values of
kono
parents:
diff changeset
240 @var{n} generate more information.
kono
parents:
diff changeset
241
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
242 @item -fgo-debug-escape-hash=@var{n}
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
243 @cindex @option{-fgo-debug-escape-hash}
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
244 A hash value to debug escape analysis. @var{n} is a binary string.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
245 This runs escape analysis only on functions whose names hash to values
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
246 that match the given suffix @var{n}. This can be used to binary
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
247 search across functions to uncover escape analysis bugs.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
248
111
kono
parents:
diff changeset
249 @item -fgo-c-header=@var{file}
kono
parents:
diff changeset
250 @cindex @option{-fgo-c-header}
kono
parents:
diff changeset
251 Write top-level named Go struct definitions to @var{file} as C code.
kono
parents:
diff changeset
252 This is used when compiling the runtime package.
kono
parents:
diff changeset
253
kono
parents:
diff changeset
254 @item -fgo-compiling-runtime
kono
parents:
diff changeset
255 @cindex @option{-fgo-compiling-runtime}
kono
parents:
diff changeset
256 Apply special rules for compiling the runtime package. Implicit
kono
parents:
diff changeset
257 memory allocation is forbidden. Some additional compiler directives
kono
parents:
diff changeset
258 are supported.
kono
parents:
diff changeset
259
kono
parents:
diff changeset
260 @item -g
kono
parents:
diff changeset
261 @cindex @option{-g for gccgo}
kono
parents:
diff changeset
262 This is the standard @command{gcc} option (@pxref{Debugging Options, ,
kono
parents:
diff changeset
263 Debugging Options, gcc, Using the GNU Compiler Collection (GCC)}). It
kono
parents:
diff changeset
264 is mentioned here because by default @command{gccgo} turns on
kono
parents:
diff changeset
265 debugging information generation with the equivalent of the standard
kono
parents:
diff changeset
266 option @option{-g1}. This is because Go programs require debugging
kono
parents:
diff changeset
267 information to be available in order to get backtrace information. An
kono
parents:
diff changeset
268 explicit @option{-g0} may be used to disable the generation of
kono
parents:
diff changeset
269 debugging information, in which case certain standard library
kono
parents:
diff changeset
270 functions, such as @code{runtime.Callers}, will not operate correctly.
kono
parents:
diff changeset
271 @end table
kono
parents:
diff changeset
272
kono
parents:
diff changeset
273 @c man end
kono
parents:
diff changeset
274
kono
parents:
diff changeset
275 @node Import and Export
kono
parents:
diff changeset
276 @chapter Import and Export
kono
parents:
diff changeset
277
kono
parents:
diff changeset
278 When @command{gccgo} compiles a package which exports anything, the
kono
parents:
diff changeset
279 export information will be stored directly in the object file. When a
kono
parents:
diff changeset
280 package is imported, @command{gccgo} must be able to find the file.
kono
parents:
diff changeset
281
kono
parents:
diff changeset
282 @cindex @file{.gox}
kono
parents:
diff changeset
283 When Go code imports the package @file{@var{gopackage}}, @command{gccgo}
kono
parents:
diff changeset
284 will look for the import data using the following filenames, using the
kono
parents:
diff changeset
285 first one that it finds.
kono
parents:
diff changeset
286
kono
parents:
diff changeset
287 @table @file
kono
parents:
diff changeset
288 @item @var{gopackage}.gox
kono
parents:
diff changeset
289 @item lib@var{gopackage}.so
kono
parents:
diff changeset
290 @item lib@var{gopackage}.a
kono
parents:
diff changeset
291 @item @var{gopackage}.o
kono
parents:
diff changeset
292 @end table
kono
parents:
diff changeset
293
kono
parents:
diff changeset
294 The compiler will search for these files in the directories named by
kono
parents:
diff changeset
295 any @option{-I} options, in order in which the directories appear on
kono
parents:
diff changeset
296 the command line. The compiler will then search several standard
kono
parents:
diff changeset
297 system directories. Finally the compiler will search the current
kono
parents:
diff changeset
298 directory (to search the current directory earlier, use @samp{-I.}).
kono
parents:
diff changeset
299
kono
parents:
diff changeset
300 The compiler will extract the export information directly from the
kono
parents:
diff changeset
301 compiled object file. The file @file{@var{gopackage}.gox} will
kono
parents:
diff changeset
302 typically contain nothing but export data. This can be generated from
kono
parents:
diff changeset
303 @file{@var{gopackage}.o} via
kono
parents:
diff changeset
304
kono
parents:
diff changeset
305 @smallexample
kono
parents:
diff changeset
306 objcopy -j .go_export @var{gopackage}.o @var{gopackage}.gox
kono
parents:
diff changeset
307 @end smallexample
kono
parents:
diff changeset
308
kono
parents:
diff changeset
309 For example, it may be desirable to extract the export information
kono
parents:
diff changeset
310 from several different packages into their independent
kono
parents:
diff changeset
311 @file{@var{gopackage}.gox} files, and then to combine the different
kono
parents:
diff changeset
312 package object files together into a single shared library or archive.
kono
parents:
diff changeset
313
kono
parents:
diff changeset
314 At link time you must explicitly tell @command{gccgo} which files to
kono
parents:
diff changeset
315 link together into the executable, as is usual with @command{gcc}.
kono
parents:
diff changeset
316 This is different from the behavior of other Go compilers.
kono
parents:
diff changeset
317
kono
parents:
diff changeset
318 @node Compiler Directives
kono
parents:
diff changeset
319 @chapter Compiler Directives
kono
parents:
diff changeset
320
kono
parents:
diff changeset
321 The Go compiler supports a few compiler directives. A compiler
kono
parents:
diff changeset
322 directive uses a @code{//} comment at the start of a line. There must
kono
parents:
diff changeset
323 be no space between the @code{//} and the name of the directive.
kono
parents:
diff changeset
324
kono
parents:
diff changeset
325 @table @code
kono
parents:
diff changeset
326 @item //line @var{file}:@var{line}
kono
parents:
diff changeset
327 The @code{//line} directive specifies that the source line that
kono
parents:
diff changeset
328 follows should be recorded as having come from the given file path and
kono
parents:
diff changeset
329 line number. Successive lines are recorded using increasing line
kono
parents:
diff changeset
330 numbers, until the next directive. This directive typically appears
kono
parents:
diff changeset
331 in machine-generated code, so that compilers and debuggers will show
kono
parents:
diff changeset
332 lines in the original input to the generator.
kono
parents:
diff changeset
333
kono
parents:
diff changeset
334 @item //extern @var{extern_name}
kono
parents:
diff changeset
335 The @code{extern} directive sets the externally visible name of the
kono
parents:
diff changeset
336 next function declaration. See @ref{Function Names}.
kono
parents:
diff changeset
337
kono
parents:
diff changeset
338 @item //go:compile @var{go_name} @var{extern_name}
kono
parents:
diff changeset
339 The @code{go:compile} directives sets the externally visible name of a
kono
parents:
diff changeset
340 function definition or declaration. See @ref{Function Names}.
kono
parents:
diff changeset
341
kono
parents:
diff changeset
342 @item //go:noescape
kono
parents:
diff changeset
343 The @code{//go:noescape} directive specifies that the next declaration
kono
parents:
diff changeset
344 in the file, which must be a func without a body (meaning that it has
kono
parents:
diff changeset
345 an implementation not written in Go) does not allow any of the
kono
parents:
diff changeset
346 pointers passed as arguments to escape into the heap or into the
kono
parents:
diff changeset
347 values returned from the function. This information can be used during
kono
parents:
diff changeset
348 the compiler's escape analysis of Go code calling the function.
kono
parents:
diff changeset
349
kono
parents:
diff changeset
350 @item //go:nosplit
kono
parents:
diff changeset
351 The @code{//go:nosplit} directive specifies that the next function
kono
parents:
diff changeset
352 declared in the file must not include a stack overflow check. This is
kono
parents:
diff changeset
353 most commonly used by low-level runtime sources invoked at times when
kono
parents:
diff changeset
354 it is unsafe for the calling goroutine to be preempted.
kono
parents:
diff changeset
355
kono
parents:
diff changeset
356 @item //go:noinline
kono
parents:
diff changeset
357 The @code{//go:noinline} directive specifies that the next function
kono
parents:
diff changeset
358 defined in the file may not be inlined.
kono
parents:
diff changeset
359
kono
parents:
diff changeset
360 @end table
kono
parents:
diff changeset
361
kono
parents:
diff changeset
362 @node C Interoperability
kono
parents:
diff changeset
363 @chapter C Interoperability
kono
parents:
diff changeset
364
kono
parents:
diff changeset
365 When using @command{gccgo} there is limited interoperability with C,
kono
parents:
diff changeset
366 or with C++ code compiled using @code{extern "C"}.
kono
parents:
diff changeset
367
kono
parents:
diff changeset
368 This information is provided largely for documentation purposes. For
kono
parents:
diff changeset
369 ordinary use it is best to build programs with the go tool and then
kono
parents:
diff changeset
370 use @code{import "C"}, as described at
kono
parents:
diff changeset
371 @url{http://golang.org/cmd/cgo}.
kono
parents:
diff changeset
372
kono
parents:
diff changeset
373 @menu
kono
parents:
diff changeset
374 * C Type Interoperability:: How C and Go types match up.
kono
parents:
diff changeset
375 * Function Names:: How Go functions are named.
kono
parents:
diff changeset
376 @end menu
kono
parents:
diff changeset
377
kono
parents:
diff changeset
378 @node C Type Interoperability
kono
parents:
diff changeset
379 @section C Type Interoperability
kono
parents:
diff changeset
380
kono
parents:
diff changeset
381 Basic types map directly: an @code{int} in Go is an @code{int} in C,
kono
parents:
diff changeset
382 etc. Go @code{byte} is equivalent to C @code{unsigned char}.
kono
parents:
diff changeset
383 Pointers in Go are pointers in C. A Go @code{struct} is the same as C
kono
parents:
diff changeset
384 @code{struct} with the same field names and types.
kono
parents:
diff changeset
385
kono
parents:
diff changeset
386 @cindex @code{string} in C
kono
parents:
diff changeset
387 The Go @code{string} type is currently defined as a two-element
kono
parents:
diff changeset
388 structure:
kono
parents:
diff changeset
389
kono
parents:
diff changeset
390 @smallexample
kono
parents:
diff changeset
391 struct __go_string @{
kono
parents:
diff changeset
392 const unsigned char *__data;
kono
parents:
diff changeset
393 int __length;
kono
parents:
diff changeset
394 @};
kono
parents:
diff changeset
395 @end smallexample
kono
parents:
diff changeset
396
kono
parents:
diff changeset
397 You can't pass arrays between C and Go. However, a pointer to an
kono
parents:
diff changeset
398 array in Go is equivalent to a C pointer to the equivalent of the
kono
parents:
diff changeset
399 element type. For example, Go @code{*[10]int} is equivalent to C
kono
parents:
diff changeset
400 @code{int*}, assuming that the C pointer does point to 10 elements.
kono
parents:
diff changeset
401
kono
parents:
diff changeset
402 @cindex @code{slice} in C
kono
parents:
diff changeset
403 A slice in Go is a structure. The current definition is:
kono
parents:
diff changeset
404
kono
parents:
diff changeset
405 @smallexample
kono
parents:
diff changeset
406 struct __go_slice @{
kono
parents:
diff changeset
407 void *__values;
kono
parents:
diff changeset
408 int __count;
kono
parents:
diff changeset
409 int __capacity;
kono
parents:
diff changeset
410 @};
kono
parents:
diff changeset
411 @end smallexample
kono
parents:
diff changeset
412
kono
parents:
diff changeset
413 The type of a Go function with no receiver is equivalent to a C
kono
parents:
diff changeset
414 function whose parameter types are equivalent. When a Go function
kono
parents:
diff changeset
415 returns more than one value, the C function returns a struct. For
kono
parents:
diff changeset
416 example, these functions have equivalent types:
kono
parents:
diff changeset
417
kono
parents:
diff changeset
418 @smallexample
kono
parents:
diff changeset
419 func GoFunction(int) (int, float)
kono
parents:
diff changeset
420 struct @{ int i; float f; @} CFunction(int)
kono
parents:
diff changeset
421 @end smallexample
kono
parents:
diff changeset
422
kono
parents:
diff changeset
423 A pointer to a Go function is equivalent to a pointer to a C function
kono
parents:
diff changeset
424 when the functions have equivalent types.
kono
parents:
diff changeset
425
kono
parents:
diff changeset
426 Go @code{interface}, @code{channel}, and @code{map} types have no
kono
parents:
diff changeset
427 corresponding C type (@code{interface} is a two-element struct and
kono
parents:
diff changeset
428 @code{channel} and @code{map} are pointers to structs in C, but the
kono
parents:
diff changeset
429 structs are deliberately undocumented). C @code{enum} types
kono
parents:
diff changeset
430 correspond to some integer type, but precisely which one is difficult
kono
parents:
diff changeset
431 to predict in general; use a cast. C @code{union} types have no
kono
parents:
diff changeset
432 corresponding Go type. C @code{struct} types containing bitfields
kono
parents:
diff changeset
433 have no corresponding Go type. C++ @code{class} types have no
kono
parents:
diff changeset
434 corresponding Go type.
kono
parents:
diff changeset
435
kono
parents:
diff changeset
436 Memory allocation is completely different between C and Go, as Go uses
kono
parents:
diff changeset
437 garbage collection. The exact guidelines in this area are
kono
parents:
diff changeset
438 undetermined, but it is likely that it will be permitted to pass a
kono
parents:
diff changeset
439 pointer to allocated memory from C to Go. The responsibility of
kono
parents:
diff changeset
440 eventually freeing the pointer will remain with C side, and of course
kono
parents:
diff changeset
441 if the C side frees the pointer while the Go side still has a copy the
kono
parents:
diff changeset
442 program will fail. When passing a pointer from Go to C, the Go
kono
parents:
diff changeset
443 function must retain a visible copy of it in some Go variable.
kono
parents:
diff changeset
444 Otherwise the Go garbage collector may delete the pointer while the C
kono
parents:
diff changeset
445 function is still using it.
kono
parents:
diff changeset
446
kono
parents:
diff changeset
447 @node Function Names
kono
parents:
diff changeset
448 @section Function Names
kono
parents:
diff changeset
449
kono
parents:
diff changeset
450 @cindex @code{extern}
kono
parents:
diff changeset
451 @cindex external names
kono
parents:
diff changeset
452 Go code can call C functions directly using the @code{//extern} or
kono
parents:
diff changeset
453 @code{//go:linkname} compiler directives. An @code{//extern}
kono
parents:
diff changeset
454 directive must be at the beginning of the line and must start with
kono
parents:
diff changeset
455 @code{//extern}. This must be followed by a space and then the
kono
parents:
diff changeset
456 external name of the function. The function declaration must be on
kono
parents:
diff changeset
457 the line immediately after the comment. For example, here is how the
kono
parents:
diff changeset
458 C function @code{open} can be declared in Go:
kono
parents:
diff changeset
459
kono
parents:
diff changeset
460 @smallexample
kono
parents:
diff changeset
461 //extern open
kono
parents:
diff changeset
462 func c_open(name *byte, mode int, perm int) int
kono
parents:
diff changeset
463 @end smallexample
kono
parents:
diff changeset
464
kono
parents:
diff changeset
465 You can do the same thing using the @code{//go:linkname} compiler
kono
parents:
diff changeset
466 directive. The @code{//go:linkname} directive must be at the start of
kono
parents:
diff changeset
467 the line. It is followed by whitespace, the name of the Go function,
kono
parents:
diff changeset
468 more whitespace, and the external name of the function. Unlike
kono
parents:
diff changeset
469 @code{//extern}, @code{//go:linkname} does not need to appear
kono
parents:
diff changeset
470 immediately adjacent to the function definition or declaration.
kono
parents:
diff changeset
471
kono
parents:
diff changeset
472 @smallexample
kono
parents:
diff changeset
473 //go:linkname c_open open
kono
parents:
diff changeset
474 func c_open(name *byte, mode int, perm int) int
kono
parents:
diff changeset
475 @end smallexample
kono
parents:
diff changeset
476
kono
parents:
diff changeset
477 The C function naturally expects a nul terminated string, which in Go
kono
parents:
diff changeset
478 is equivalent to a pointer to an array (not a slice!) of @code{byte}
kono
parents:
diff changeset
479 with a terminating zero byte. So a sample call from Go would look
kono
parents:
diff changeset
480 like (after importing the @code{os} package):
kono
parents:
diff changeset
481
kono
parents:
diff changeset
482 @smallexample
kono
parents:
diff changeset
483 var name = [4]byte@{'f', 'o', 'o', 0@};
kono
parents:
diff changeset
484 i := c_open(&name[0], os.O_RDONLY, 0);
kono
parents:
diff changeset
485 @end smallexample
kono
parents:
diff changeset
486
kono
parents:
diff changeset
487 Note that this serves as an example only. To open a file in Go please
kono
parents:
diff changeset
488 use Go's @code{os.Open} function instead.
kono
parents:
diff changeset
489
kono
parents:
diff changeset
490 The name of Go functions accessed from C is subject to change. At
kono
parents:
diff changeset
491 present the name of a Go function that does not have a receiver is
kono
parents:
diff changeset
492 @code{pkgpath.Functionname}. The @var{pkgpath} is set by the
kono
parents:
diff changeset
493 @option{-fgo-pkgpath} option used when the package is compiled; if the
kono
parents:
diff changeset
494 option is not used, the default is @code{go.@var{packagename}}. To
kono
parents:
diff changeset
495 call the function from C you must set the name using the @command{gcc}
kono
parents:
diff changeset
496 @code{__asm__} extension.
kono
parents:
diff changeset
497
kono
parents:
diff changeset
498 @smallexample
kono
parents:
diff changeset
499 extern int go_function(int) __asm__ ("mypkgpath.Function");
kono
parents:
diff changeset
500 @end smallexample
kono
parents:
diff changeset
501
kono
parents:
diff changeset
502 @node Index
kono
parents:
diff changeset
503 @unnumbered Index
kono
parents:
diff changeset
504
kono
parents:
diff changeset
505 @printindex cp
kono
parents:
diff changeset
506
kono
parents:
diff changeset
507 @bye