2 # Copyright 2009 The Go Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style
4 # license that can be found in the LICENSE file.
6 # This program reads a file containing function prototypes
7 # (like syscall_solaris.go) and generates system call bodies.
8 # The prototypes are marked by lines beginning with "//sys"
9 # and read like func declarations if //sys is replaced by func, but:
10 # * The parameter lists must give a name for each argument.
11 # This includes return parameters.
12 # * The parameter lists must give a type for each argument:
13 # the (x, y, z int) shorthand is not allowed.
14 # * If the return parameter is an error number, it must be named err.
15 # * If go func name needs to be different than its libc name,
16 # * or the function is not in libc, name could be specified
17 # * at the end, after "=" sign, like
18 # //sys getsockopt(s int, level int, name int, val uintptr, vallen *_Socklen) (err error) = libsocket.getsockopt
22 my $cmdline = "mksyscall_libc.pl " . join(' ', @ARGV);
25 my $tags = ""; # build tags
26 my $newtags = ""; # new style build tags
32 if($ARGV[0] eq "-b32") {
33 $_32bit = "big-endian";
35 } elsif($ARGV[0] eq "-l32") {
36 $_32bit = "little-endian";
39 if($ARGV[0] eq "-aix") {
43 if($ARGV[0] eq "-solaris") {
47 if($ARGV[0] eq "-tags") {
54 if($ARGV[0] =~ /^-/) {
55 print STDERR "usage: mksyscall_libc.pl [-b32 | -l32] [-aix | -solaris] [-tags x,y] [file ...]\n";
59 sub parseparamlist($) {
66 return split(/\s*,\s*/, $list);
71 if($p !~ /^(\S*) (\S*)$/) {
72 print STDERR "$ARGV:$.: malformed parameter: $p\n";
89 $package = $1 if !$package && /^package (\S+)$/;
90 my $nonblock = /^\/\/sysnb /;
91 next if !/^\/\/sys / && !$nonblock;
94 $syscalldot = "syscall." if $package ne "syscall";
96 # Line must be of the form
97 # func Open(path string, mode int, perm int) (fd int, err error)
98 # Split into name, in params, out params.
99 if(!/^\/\/sys(nb)? (\w+)\(([^()]*)\)\s*(?:\(([^()]+)\))?\s*(?:=\s*(?:(\w*)\.)?(\w*))?$/) {
100 print STDERR "$ARGV:$.: malformed //sys declaration\n";
104 my ($nb, $func, $in, $out, $modname, $sysname) = ($1, $2, $3, $4, $5, $6);
106 # Split argument lists on comma.
107 my @in = parseparamlist($in);
108 my @out = parseparamlist($out);
110 # Try in vain to keep people from editing this file.
111 # The theory is that they jump into the middle of the file
112 # without reading the header.
113 $text .= "// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT\n\n";
118 $modname = "libc.a/shr_64.o";
120 print STDERR "$func: only syscall using libc are available\n";
139 # System call pointer variable name.
140 my $sysvarname = "libc_${sysname}";
142 my $strconvfunc = "BytePtrFromString";
143 my $strconvtype = "*byte";
145 $sysname =~ y/A-Z/a-z/; # All libc functions are lowercase.
147 # Runtime import of function to allow cross-platform builds.
148 if($dynimports !~ /\s+${sysvarname}\s+/) {
149 $dynimports .= "//go:cgo_import_dynamic ${sysvarname} ${sysname} \"$modname\"\n";
150 # Link symbol to proc address variable.
151 $linknames .= "//go:linkname ${sysvarname} ${sysvarname}\n";
152 # Library proc address variable.
153 push @vars, $sysvarname;
156 # Go function header.
157 $out = join(', ', @out);
164 $text .= sprintf "func %s(%s)%s {\n", $func, join(', ', @in), $out;
166 # Check if err return available
168 foreach my $p (@out) {
169 my ($name, $type) = parseparam($p);
170 if($type eq "error") {
176 # Prepare arguments to Syscall.
179 foreach my $p (@in) {
180 my ($name, $type) = parseparam($p);
182 push @args, "uintptr(unsafe.Pointer($name))";
183 } elsif($type eq "string" && $errvar ne "") {
184 $text .= "\tvar _p$n $strconvtype\n";
185 $text .= "\t_p$n, $errvar = $strconvfunc($name)\n";
186 $text .= "\tif $errvar != nil {\n\t\treturn\n\t}\n";
187 push @args, "uintptr(unsafe.Pointer(_p$n))";
189 } elsif($type eq "string") {
190 print STDERR "$ARGV:$.: $func uses string arguments, but has no error return\n";
191 $text .= "\tvar _p$n $strconvtype\n";
192 $text .= "\t_p$n, _ = $strconvfunc($name)\n";
193 push @args, "uintptr(unsafe.Pointer(_p$n))";
195 } elsif($type =~ /^\[\](.*)/) {
196 # Convert slice into pointer, length.
197 # Have to be careful not to take address of &a[0] if len == 0:
198 # pass nil in that case.
199 $text .= "\tvar _p$n *$1\n";
200 $text .= "\tif len($name) > 0 {\n\t\t_p$n = \&$name\[0]\n\t}\n";
201 push @args, "uintptr(unsafe.Pointer(_p$n))", "uintptr(len($name))";
203 } elsif($type eq "int64" && $_32bit ne "") {
204 if($_32bit eq "big-endian") {
205 push @args, "uintptr($name >> 32)", "uintptr($name)";
207 push @args, "uintptr($name)", "uintptr($name >> 32)";
209 } elsif($type eq "bool") {
210 $text .= "\tvar _p$n uint32\n";
211 $text .= "\tif $name {\n\t\t_p$n = 1\n\t} else {\n\t\t_p$n = 0\n\t}\n";
212 push @args, "uintptr(_p$n)";
215 push @args, "uintptr($name)";
221 my $asmrawfuncname="";
224 $asmfuncname="syscall6";
225 $asmrawfuncname="rawSyscall6";
227 $asmfuncname="sysvicall6";
228 $asmrawfuncname="rawSysvicall6";
231 # Determine which form to use; pad args with zeros.
232 my $asm = "${syscalldot}${asmfuncname}";
234 $asm = "${syscalldot}${asmrawfuncname}";
241 print STDERR "$ARGV:$.: too many arguments to system call\n";
245 my $args = join(', ', @args);
246 my $call = "$asm(uintptr(unsafe.Pointer(&$sysvarname)), $nargs, $args)";
248 # Assign return values.
251 my @ret = ("_", "_", "_");
254 for(my $i=0; $i<@out; $i++) {
256 my ($name, $type) = parseparam($p);
263 $reg = sprintf("r%d", $i);
266 if($type eq "bool") {
269 if($type eq "int64" && $_32bit ne "") {
270 # 64-bit number in r1:r0 or r0:r1.
272 print STDERR "$ARGV:$.: not enough registers for int64 return\n";
274 if($_32bit eq "big-endian") {
275 $reg = sprintf("int64(r%d)<<32 | int64(r%d)", $i, $i+1);
277 $reg = sprintf("int64(r%d)<<32 | int64(r%d)", $i+1, $i);
279 $ret[$i] = sprintf("r%d", $i);
280 $ret[$i+1] = sprintf("r%d", $i+1);
283 $body .= "\t$name = $type($reg)\n";
286 if ($ret[0] eq "_" && $ret[1] eq "_" && $ret[2] eq "_") {
287 $text .= "\t$call\n";
289 $text .= "\t$ret[0], $ret[1], $ret[2] := $call\n";
294 $text .= "\tif e1 != 0 {\n";
295 $text .= "\t\terr = errnoErr(e1)\n";
298 $text .= "\treturn\n";
306 # TODO: this assumes tags are just simply comma separated. For now this is all the uses.
307 $newtags = $tags =~ s/,/ && /r;
311 // Code generated by the command above; DO NOT EDIT.
320 print "import \"syscall\"\n" if $package ne "syscall";
322 my $vardecls = "\t" . join(",\n\t", @vars);
323 $vardecls .= " libcFunc";
329 type libcFunc uintptr