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_darwin.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 errno.
16 # A line beginning with //sysnb is like //sys, except that the
17 # goroutine will not be suspended during the execution of the system
18 # call. This must only be used for system calls which can never
19 # block, as otherwise the system call could cause all goroutines to
24 my $cmdline = "mksyscall.pl " . join(' ', @ARGV);
31 my $arm = 0; # 64-bit value should use (even, odd)-pair
32 my $tags = ""; # build tags
34 if($ARGV[0] eq "-b32") {
35 $_32bit = "big-endian";
37 } elsif($ARGV[0] eq "-l32") {
38 $_32bit = "little-endian";
41 if($ARGV[0] eq "-plan9") {
45 if($ARGV[0] eq "-openbsd") {
49 if($ARGV[0] eq "-netbsd") {
53 if($ARGV[0] eq "-dragonfly") {
57 if($ARGV[0] eq "-arm") {
61 if($ARGV[0] eq "-tags") {
67 if($ARGV[0] =~ /^-/) {
68 print STDERR "usage: mksyscall.pl [-b32 | -l32] [-tags x,y] [file ...]\n";
72 # Check that we are using the new build system if we should
73 if($ENV{'GOOS'} eq "linux" && $ENV{'GOARCH'} ne "sparc64") {
74 if($ENV{'GOLANG_SYS_BUILD'} ne "docker") {
75 print STDERR "In the new build system, mksyscall should not be called directly.\n";
76 print STDERR "See README.md\n";
82 sub parseparamlist($) {
89 return split(/\s*,\s*/, $list);
94 if($p !~ /^(\S*) (\S*)$/) {
95 print STDERR "$ARGV:$.: malformed parameter: $p\n";
108 my $nonblock = /^\/\/sysnb /;
109 next if !/^\/\/sys / && !$nonblock;
111 # Line must be of the form
112 # func Open(path string, mode int, perm int) (fd int, errno error)
113 # Split into name, in params, out params.
114 if(!/^\/\/sys(nb)? (\w+)\(([^()]*)\)\s*(?:\(([^()]+)\))?\s*(?:=\s*((?i)SYS_[A-Z0-9_]+))?$/) {
115 print STDERR "$ARGV:$.: malformed //sys declaration\n";
119 my ($func, $in, $out, $sysname) = ($2, $3, $4, $5);
121 # Split argument lists on comma.
122 my @in = parseparamlist($in);
123 my @out = parseparamlist($out);
125 # Try in vain to keep people from editing this file.
126 # The theory is that they jump into the middle of the file
127 # without reading the header.
128 $text .= "// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT\n\n";
130 # Go function header.
131 my $out_decl = @out ? sprintf(" (%s)", join(', ', @out)) : "";
132 $text .= sprintf "func %s(%s)%s {\n", $func, join(', ', @in), $out_decl;
134 # Check if err return available
136 foreach my $p (@out) {
137 my ($name, $type) = parseparam($p);
138 if($type eq "error") {
144 # Prepare arguments to Syscall.
147 foreach my $p (@in) {
148 my ($name, $type) = parseparam($p);
150 push @args, "uintptr(unsafe.Pointer($name))";
151 } elsif($type eq "string" && $errvar ne "") {
152 $text .= "\tvar _p$n *byte\n";
153 $text .= "\t_p$n, $errvar = BytePtrFromString($name)\n";
154 $text .= "\tif $errvar != nil {\n\t\treturn\n\t}\n";
155 push @args, "uintptr(unsafe.Pointer(_p$n))";
157 } elsif($type eq "string") {
158 print STDERR "$ARGV:$.: $func uses string arguments, but has no error return\n";
159 $text .= "\tvar _p$n *byte\n";
160 $text .= "\t_p$n, _ = BytePtrFromString($name)\n";
161 push @args, "uintptr(unsafe.Pointer(_p$n))";
163 } elsif($type =~ /^\[\](.*)/) {
164 # Convert slice into pointer, length.
165 # Have to be careful not to take address of &a[0] if len == 0:
166 # pass dummy pointer in that case.
167 # Used to pass nil, but some OSes or simulators reject write(fd, nil, 0).
168 $text .= "\tvar _p$n unsafe.Pointer\n";
169 $text .= "\tif len($name) > 0 {\n\t\t_p$n = unsafe.Pointer(\&${name}[0])\n\t}";
170 $text .= " else {\n\t\t_p$n = unsafe.Pointer(&_zero)\n\t}";
172 push @args, "uintptr(_p$n)", "uintptr(len($name))";
174 } elsif($type eq "int64" && ($openbsd || $netbsd)) {
176 if($_32bit eq "big-endian") {
177 push @args, "uintptr($name>>32)", "uintptr($name)";
178 } elsif($_32bit eq "little-endian") {
179 push @args, "uintptr($name)", "uintptr($name>>32)";
181 push @args, "uintptr($name)";
183 } elsif($type eq "int64" && $dragonfly) {
184 if ($func !~ /^extp(read|write)/i) {
187 if($_32bit eq "big-endian") {
188 push @args, "uintptr($name>>32)", "uintptr($name)";
189 } elsif($_32bit eq "little-endian") {
190 push @args, "uintptr($name)", "uintptr($name>>32)";
192 push @args, "uintptr($name)";
194 } elsif($type eq "int64" && $_32bit ne "") {
195 if(@args % 2 && $arm) {
196 # arm abi specifies 64-bit argument uses
200 if($_32bit eq "big-endian") {
201 push @args, "uintptr($name>>32)", "uintptr($name)";
203 push @args, "uintptr($name)", "uintptr($name>>32)";
206 push @args, "uintptr($name)";
210 # Determine which form to use; pad args with zeros.
219 } elsif(@args <= 6) {
224 } elsif(@args <= 9) {
230 print STDERR "$ARGV:$.: too many arguments to system call\n";
233 # System call number.
235 $sysname = "SYS_$func";
236 $sysname =~ s/([a-z])([A-Z])/${1}_$2/g; # turn FooBar into Foo_Bar
237 $sysname =~ y/a-z/A-Z/;
241 my $args = join(', ', @args);
242 my $call = "$asm($sysname, $args)";
244 # Assign return values.
246 my @ret = ("_", "_", "_");
248 for(my $i=0; $i<@out; $i++) {
250 my ($name, $type) = parseparam($p);
252 if($name eq "err" && !$plan9) {
256 } elsif($name eq "err" && $plan9) {
261 $reg = sprintf("r%d", $i);
264 if($type eq "bool") {
267 if($type eq "int64" && $_32bit ne "") {
268 # 64-bit number in r1:r0 or r0:r1.
270 print STDERR "$ARGV:$.: not enough registers for int64 return\n";
272 if($_32bit eq "big-endian") {
273 $reg = sprintf("int64(r%d)<<32 | int64(r%d)", $i, $i+1);
275 $reg = sprintf("int64(r%d)<<32 | int64(r%d)", $i+1, $i);
277 $ret[$i] = sprintf("r%d", $i);
278 $ret[$i+1] = sprintf("r%d", $i+1);
280 if($reg ne "e1" || $plan9) {
281 $body .= "\t$name = $type($reg)\n";
284 if ($ret[0] eq "_" && $ret[1] eq "_" && $ret[2] eq "_") {
285 $text .= "\t$call\n";
287 $text .= "\t$ret[0], $ret[1], $ret[2] := $call\n";
291 if ($plan9 && $ret[2] eq "e1") {
292 $text .= "\tif int32(r0) == -1 {\n";
293 $text .= "\t\terr = e1\n";
295 } elsif ($do_errno) {
296 $text .= "\tif e1 != 0 {\n";
297 $text .= "\t\terr = errnoErr(e1)\n";
300 $text .= "\treturn\n";
313 // Code generated by the command above; see README.md. DO NOT EDIT.