]> Cypherpunks.ru repositories - gostls13.git/blob - src/syscall/mksyscall.pl
[dev.regabi] test: add a test for inlining closures
[gostls13.git] / src / syscall / mksyscall.pl
1 #!/usr/bin/env perl
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.
5
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.
15
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
20 # hang.
21
22 use strict;
23
24 my $cmdline = "mksyscall.pl " . join(' ', @ARGV);
25 my $errors = 0;
26 my $_32bit = "";
27 my $plan9 = 0;
28 my $darwin = 0;
29 my $openbsd = 0;
30 my $netbsd = 0;
31 my $dragonfly = 0;
32 my $arm = 0; # 64-bit value should use (even, odd)-pair
33 my $tags = "";  # build tags
34
35 if($ARGV[0] eq "-b32") {
36         $_32bit = "big-endian";
37         shift;
38 } elsif($ARGV[0] eq "-l32") {
39         $_32bit = "little-endian";
40         shift;
41 }
42 if($ARGV[0] eq "-plan9") {
43         $plan9 = 1;
44         shift;
45 }
46 if($ARGV[0] eq "-darwin") {
47         $darwin = 1;
48         shift;
49 }
50 if($ARGV[0] eq "-openbsd") {
51         $openbsd = 1;
52         shift;
53 }
54 if($ARGV[0] eq "-netbsd") {
55         $netbsd = 1;
56         shift;
57 }
58 if($ARGV[0] eq "-dragonfly") {
59         $dragonfly = 1;
60         shift;
61 }
62 if($ARGV[0] eq "-arm") {
63         $arm = 1;
64         shift;
65 }
66 if($ARGV[0] eq "-tags") {
67         shift;
68         $tags = $ARGV[0];
69         shift;
70 }
71
72 if($ARGV[0] =~ /^-/) {
73         print STDERR "usage: mksyscall.pl [-b32 | -l32] [-tags x,y] [file ...]\n";
74         exit 1;
75 }
76
77 sub parseparamlist($) {
78         my ($list) = @_;
79         $list =~ s/^\s*//;
80         $list =~ s/\s*$//;
81         if($list eq "") {
82                 return ();
83         }
84         return split(/\s*,\s*/, $list);
85 }
86
87 sub parseparam($) {
88         my ($p) = @_;
89         if($p !~ /^(\S*) (\S*)$/) {
90                 print STDERR "$ARGV:$.: malformed parameter: $p\n";
91                 $errors = 1;
92                 return ("xx", "int");
93         }
94         return ($1, $2);
95 }
96
97 # set of trampolines we've already generated
98 my %trampolines;
99
100 my $text = "";
101 while(<>) {
102         chomp;
103         s/\s+/ /g;
104         s/^\s+//;
105         s/\s+$//;
106         my $nonblock = /^\/\/sysnb /;
107         next if !/^\/\/sys / && !$nonblock;
108
109         # Line must be of the form
110         #       func Open(path string, mode int, perm int) (fd int, errno error)
111         # Split into name, in params, out params.
112         if(!/^\/\/sys(nb)? (\w+)\(([^()]*)\)\s*(?:\(([^()]+)\))?\s*(?:=\s*((?i)_?SYS_[A-Z0-9_]+))?$/) {
113                 print STDERR "$ARGV:$.: malformed //sys declaration\n";
114                 $errors = 1;
115                 next;
116         }
117         my ($func, $in, $out, $sysname) = ($2, $3, $4, $5);
118
119         # Split argument lists on comma.
120         my @in = parseparamlist($in);
121         my @out = parseparamlist($out);
122
123         # Try in vain to keep people from editing this file.
124         # The theory is that they jump into the middle of the file
125         # without reading the header.
126         $text .= "// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT\n\n";
127
128         if ($darwin && $func eq "ptrace1") {
129                 # The ptrace function is called from forkAndExecInChild where stack
130                 # growth is forbidden.
131                 $text .= "//go:nosplit\n"
132         }
133
134         # Go function header.
135         my $out_decl = @out ? sprintf(" (%s)", join(', ', @out)) : "";
136         $text .= sprintf "func %s(%s)%s {\n", $func, join(', ', @in), $out_decl;
137
138         # Check if err return available
139         my $errvar = "";
140         foreach my $p (@out) {
141                 my ($name, $type) = parseparam($p);
142                 if($type eq "error") {
143                         $errvar = $name;
144                         last;
145                 }
146         }
147
148         # Prepare arguments to Syscall.
149         my @args = ();
150         my $n = 0;
151         foreach my $p (@in) {
152                 my ($name, $type) = parseparam($p);
153                 if($type =~ /^\*/) {
154                         push @args, "uintptr(unsafe.Pointer($name))";
155                 } elsif($type eq "string" && $errvar ne "") {
156                         $text .= "\tvar _p$n *byte\n";
157                         $text .= "\t_p$n, $errvar = BytePtrFromString($name)\n";
158                         $text .= "\tif $errvar != nil {\n\t\treturn\n\t}\n";
159                         push @args, "uintptr(unsafe.Pointer(_p$n))";
160                         $n++;
161                 } elsif($type eq "string") {
162                         print STDERR "$ARGV:$.: $func uses string arguments, but has no error return\n";
163                         $text .= "\tvar _p$n *byte\n";
164                         $text .= "\t_p$n, _ = BytePtrFromString($name)\n";
165                         push @args, "uintptr(unsafe.Pointer(_p$n))";
166                         $n++;
167                 } elsif($type =~ /^\[\](.*)/) {
168                         # Convert slice into pointer, length.
169                         # Have to be careful not to take address of &a[0] if len == 0:
170                         # pass dummy pointer in that case.
171                         # Used to pass nil, but some OSes or simulators reject write(fd, nil, 0).
172                         $text .= "\tvar _p$n unsafe.Pointer\n";
173                         $text .= "\tif len($name) > 0 {\n\t\t_p$n = unsafe.Pointer(\&${name}[0])\n\t}";
174                         $text .= " else {\n\t\t_p$n = unsafe.Pointer(&_zero)\n\t}";
175                         $text .= "\n";
176                         push @args, "uintptr(_p$n)", "uintptr(len($name))";
177                         $n++;
178                 } elsif($type eq "int64" && ($openbsd || $netbsd)) {
179                         push @args, "0";
180                         if($_32bit eq "big-endian") {
181                                 push @args, "uintptr($name>>32)", "uintptr($name)";
182                         } elsif($_32bit eq "little-endian") {
183                                 push @args, "uintptr($name)", "uintptr($name>>32)";
184                         } else {
185                                 push @args, "uintptr($name)";
186                         }
187                 } elsif($type eq "int64" && $dragonfly) {
188                         if ($func !~ /^extp(read|write)/i) {
189                                 push @args, "0";
190                         }
191                         if($_32bit eq "big-endian") {
192                                 push @args, "uintptr($name>>32)", "uintptr($name)";
193                         } elsif($_32bit eq "little-endian") {
194                                 push @args, "uintptr($name)", "uintptr($name>>32)";
195                         } else {
196                                 push @args, "uintptr($name)";
197                         }
198                 } elsif($type eq "int64" && $_32bit ne "") {
199                         if(@args % 2 && $arm) {
200                                 # arm abi specifies 64-bit argument uses
201                                 # (even, odd) pair
202                                 push @args, "0"
203                         }
204                         if($_32bit eq "big-endian") {
205                                 push @args, "uintptr($name>>32)", "uintptr($name)";
206                         } else {
207                                 push @args, "uintptr($name)", "uintptr($name>>32)";
208                         }
209                 } else {
210                         push @args, "uintptr($name)";
211                 }
212         }
213
214         # Determine which form to use; pad args with zeros.
215         my $asm = "Syscall";
216         if ($nonblock) {
217                 if ($errvar eq "" && $ENV{'GOOS'} eq "linux") {
218                         $asm = "rawSyscallNoError";
219                 } else {
220                         $asm = "RawSyscall";
221                 }
222         }
223         if ($darwin) {
224                 # Call unexported syscall functions (which take
225                 # libc functions instead of syscall numbers).
226                 $asm = lcfirst($asm);
227         }
228         if(@args <= 3) {
229                 while(@args < 3) {
230                         push @args, "0";
231                 }
232         } elsif(@args <= 6) {
233                 $asm .= "6";
234                 while(@args < 6) {
235                         push @args, "0";
236                 }
237         } elsif(@args <= 9) {
238                 $asm .= "9";
239                 while(@args < 9) {
240                         push @args, "0";
241                 }
242         } else {
243                 print STDERR "$ARGV:$.: too many arguments to system call\n";
244         }
245
246         if ($darwin) {
247                 # Use extended versions for calls that generate a 64-bit result.
248                 my ($name, $type) = parseparam($out[0]);
249                 if ($type eq "int64" || ($type eq "uintptr" && $_32bit eq "")) {
250                         $asm .= "X";
251                 }
252         }
253
254         # System call number.
255         my $funcname = "";
256         if($sysname eq "") {
257                 $sysname = "SYS_$func";
258                 $sysname =~ s/([a-z])([A-Z])/${1}_$2/g; # turn FooBar into Foo_Bar
259                 $sysname =~ y/a-z/A-Z/;
260                 if($darwin) {
261                         $sysname =~ y/A-Z/a-z/;
262                         $sysname = substr $sysname, 4;
263                         $funcname = "libc_$sysname";
264                 }
265         }
266         if($darwin) {
267                 if($funcname eq "") {
268                         $sysname = substr $sysname, 4;
269                         $funcname = "libc_$sysname";
270                 }
271                 $sysname = "funcPC(${funcname}_trampoline)";
272         }
273
274         # Actual call.
275         my $args = join(', ', @args);
276         my $call = "$asm($sysname, $args)";
277
278         # Assign return values.
279         my $body = "";
280         my @ret = ("_", "_", "_");
281         my $do_errno = 0;
282         for(my $i=0; $i<@out; $i++) {
283                 my $p = $out[$i];
284                 my ($name, $type) = parseparam($p);
285                 my $reg = "";
286                 if($name eq "err" && !$plan9) {
287                         $reg = "e1";
288                         $ret[2] = $reg;
289                         $do_errno = 1;
290                 } elsif($name eq "err" && $plan9) {
291                         $ret[0] = "r0";
292                         $ret[2] = "e1";
293                         next;
294                 } else {
295                         $reg = sprintf("r%d", $i);
296                         $ret[$i] = $reg;
297                 }
298                 if($type eq "bool") {
299                         $reg = "$reg != 0";
300                 }
301                 if($type eq "int64" && $_32bit ne "") {
302                         # 64-bit number in r1:r0 or r0:r1.
303                         if($i+2 > @out) {
304                                 print STDERR "$ARGV:$.: not enough registers for int64 return\n";
305                         }
306                         if($_32bit eq "big-endian") {
307                                 $reg = sprintf("int64(r%d)<<32 | int64(r%d)", $i, $i+1);
308                         } else {
309                                 $reg = sprintf("int64(r%d)<<32 | int64(r%d)", $i+1, $i);
310                         }
311                         $ret[$i] = sprintf("r%d", $i);
312                         $ret[$i+1] = sprintf("r%d", $i+1);
313                 }
314                 if($reg ne "e1" || $plan9) {
315                         $body .= "\t$name = $type($reg)\n";
316                 }
317         }
318         if ($ret[0] eq "_" && $ret[1] eq "_" && $ret[2] eq "_") {
319                 $text .= "\t$call\n";
320         } else {
321                 if ($errvar eq "" && $ENV{'GOOS'} eq "linux") {
322                         # raw syscall without error on Linux, see golang.org/issue/22924
323                         $text .= "\t$ret[0], $ret[1] := $call\n";
324                 } else {
325                         $text .= "\t$ret[0], $ret[1], $ret[2] := $call\n";
326                 }
327         }
328         $text .= $body;
329
330         if ($plan9 && $ret[2] eq "e1") {
331                 $text .= "\tif int32(r0) == -1 {\n";
332                 $text .= "\t\terr = e1\n";
333                 $text .= "\t}\n";
334         } elsif ($do_errno) {
335                 $text .= "\tif e1 != 0 {\n";
336                 $text .= "\t\terr = errnoErr(e1)\n";
337                 $text .= "\t}\n";
338         }
339         $text .= "\treturn\n";
340         $text .= "}\n\n";
341         if($darwin) {
342                 if (not exists $trampolines{$funcname}) {
343                         $trampolines{$funcname} = 1;
344                         # The assembly trampoline that jumps to the libc routine.
345                         $text .= "func ${funcname}_trampoline()\n";
346                         # Tell the linker that funcname can be found in libSystem using varname without the libc_ prefix.
347                         my $basename = substr $funcname, 5;
348                         $text .= "//go:cgo_import_dynamic $funcname $basename \"/usr/lib/libSystem.B.dylib\"\n\n";
349                 }
350         }
351 }
352
353 chomp $text;
354 chomp $text;
355
356 if($errors) {
357         exit 1;
358 }
359
360 print <<EOF;
361 // $cmdline
362 // Code generated by the command above; DO NOT EDIT.
363
364 // +build $tags
365
366 package syscall
367
368 import "unsafe"
369
370 $text
371 EOF
372 exit 0;