github.com/ader1990/go@v0.0.0-20140630135419-8c24447fa791/src/pkg/syscall/mksyscall.pl (about) 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 $openbsd = 0; 29 my $netbsd = 0; 30 my $dragonfly = 0; 31 my $nacl = 0; 32 my $arm = 0; # 64-bit value should use (even, odd)-pair 33 34 if($ARGV[0] eq "-b32") { 35 $_32bit = "big-endian"; 36 shift; 37 } elsif($ARGV[0] eq "-l32") { 38 $_32bit = "little-endian"; 39 shift; 40 } 41 if($ARGV[0] eq "-plan9") { 42 $plan9 = 1; 43 shift; 44 } 45 if($ARGV[0] eq "-openbsd") { 46 $openbsd = 1; 47 shift; 48 } 49 if($ARGV[0] eq "-netbsd") { 50 $netbsd = 1; 51 shift; 52 } 53 if($ARGV[0] eq "-dragonfly") { 54 $dragonfly = 1; 55 shift; 56 } 57 if($ARGV[0] eq "-nacl") { 58 $nacl = 1; 59 shift; 60 } 61 if($ARGV[0] eq "-arm") { 62 $arm = 1; 63 shift; 64 } 65 66 if($ARGV[0] =~ /^-/) { 67 print STDERR "usage: mksyscall.pl [-b32 | -l32] [file ...]\n"; 68 exit 1; 69 } 70 71 sub parseparamlist($) { 72 my ($list) = @_; 73 $list =~ s/^\s*//; 74 $list =~ s/\s*$//; 75 if($list eq "") { 76 return (); 77 } 78 return split(/\s*,\s*/, $list); 79 } 80 81 sub parseparam($) { 82 my ($p) = @_; 83 if($p !~ /^(\S*) (\S*)$/) { 84 print STDERR "$ARGV:$.: malformed parameter: $p\n"; 85 $errors = 1; 86 return ("xx", "int"); 87 } 88 return ($1, $2); 89 } 90 91 my $text = ""; 92 while(<>) { 93 chomp; 94 s/\s+/ /g; 95 s/^\s+//; 96 s/\s+$//; 97 my $nonblock = /^\/\/sysnb /; 98 next if !/^\/\/sys / && !$nonblock; 99 100 # Line must be of the form 101 # func Open(path string, mode int, perm int) (fd int, errno error) 102 # Split into name, in params, out params. 103 if(!/^\/\/sys(nb)? (\w+)\(([^()]*)\)\s*(?:\(([^()]+)\))?\s*(?:=\s*((?i)SYS_[A-Z0-9_]+))?$/) { 104 print STDERR "$ARGV:$.: malformed //sys declaration\n"; 105 $errors = 1; 106 next; 107 } 108 my ($func, $in, $out, $sysname) = ($2, $3, $4, $5); 109 110 # Split argument lists on comma. 111 my @in = parseparamlist($in); 112 my @out = parseparamlist($out); 113 114 # Try in vain to keep people from editing this file. 115 # The theory is that they jump into the middle of the file 116 # without reading the header. 117 $text .= "// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT\n\n"; 118 119 # Go function header. 120 my $out_decl = @out ? sprintf(" (%s)", join(', ', @out)) : ""; 121 $text .= sprintf "func %s(%s)%s {\n", $func, join(', ', @in), $out_decl; 122 123 # Check if err return available 124 my $errvar = ""; 125 foreach my $p (@out) { 126 my ($name, $type) = parseparam($p); 127 if($type eq "error") { 128 $errvar = $name; 129 last; 130 } 131 } 132 133 # Prepare arguments to Syscall. 134 my @args = (); 135 my $n = 0; 136 foreach my $p (@in) { 137 my ($name, $type) = parseparam($p); 138 if($type =~ /^\*/) { 139 push @args, "uintptr(unsafe.Pointer($name))"; 140 } elsif($type eq "string" && $errvar ne "") { 141 $text .= "\tvar _p$n *byte\n"; 142 $text .= "\t_p$n, $errvar = BytePtrFromString($name)\n"; 143 $text .= "\tif $errvar != nil {\n\t\treturn\n\t}\n"; 144 push @args, "uintptr(unsafe.Pointer(_p$n))"; 145 $n++; 146 } elsif($type eq "string") { 147 print STDERR "$ARGV:$.: $func uses string arguments, but has no error return\n"; 148 $text .= "\tvar _p$n *byte\n"; 149 $text .= "\t_p$n, _ = BytePtrFromString($name)\n"; 150 push @args, "uintptr(unsafe.Pointer(_p$n))"; 151 $n++; 152 } elsif($type =~ /^\[\](.*)/) { 153 # Convert slice into pointer, length. 154 # Have to be careful not to take address of &a[0] if len == 0: 155 # pass dummy pointer in that case. 156 # Used to pass nil, but some OSes or simulators reject write(fd, nil, 0). 157 $text .= "\tvar _p$n unsafe.Pointer\n"; 158 $text .= "\tif len($name) > 0 {\n\t\t_p$n = unsafe.Pointer(\&${name}[0])\n\t}"; 159 $text .= " else {\n\t\t_p$n = unsafe.Pointer(&_zero)\n\t}"; 160 $text .= "\n"; 161 push @args, "uintptr(_p$n)", "uintptr(len($name))"; 162 $n++; 163 } elsif($type eq "int64" && ($openbsd || $netbsd)) { 164 push @args, "0"; 165 if($_32bit eq "big-endian") { 166 push @args, "uintptr($name>>32)", "uintptr($name)"; 167 } elsif($_32bit eq "little-endian") { 168 push @args, "uintptr($name)", "uintptr($name>>32)"; 169 } else { 170 push @args, "uintptr($name)"; 171 } 172 } elsif($type eq "int64" && $dragonfly) { 173 if ($func !~ /^extp(read|write)/i) { 174 push @args, "0"; 175 } 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)"; 180 } else { 181 push @args, "uintptr($name)"; 182 } 183 } elsif($type eq "int64" && $_32bit ne "") { 184 if(@args % 2 && $arm) { 185 # arm abi specifies 64-bit argument uses 186 # (even, odd) pair 187 push @args, "0" 188 } 189 if($_32bit eq "big-endian") { 190 push @args, "uintptr($name>>32)", "uintptr($name)"; 191 } else { 192 push @args, "uintptr($name)", "uintptr($name>>32)"; 193 } 194 } else { 195 push @args, "uintptr($name)"; 196 } 197 } 198 199 # Determine which form to use; pad args with zeros. 200 my $asm = "Syscall"; 201 if ($nonblock) { 202 $asm = "RawSyscall"; 203 } 204 if(@args <= 3) { 205 while(@args < 3) { 206 push @args, "0"; 207 } 208 } elsif(@args <= 6) { 209 $asm .= "6"; 210 while(@args < 6) { 211 push @args, "0"; 212 } 213 } elsif(@args <= 9) { 214 $asm .= "9"; 215 while(@args < 9) { 216 push @args, "0"; 217 } 218 } else { 219 print STDERR "$ARGV:$.: too many arguments to system call\n"; 220 } 221 222 # System call number. 223 if($sysname eq "") { 224 $sysname = "SYS_$func"; 225 $sysname =~ s/([a-z])([A-Z])/${1}_$2/g; # turn FooBar into Foo_Bar 226 $sysname =~ y/a-z/A-Z/; 227 if($nacl) { 228 $sysname =~ y/A-Z/a-z/; 229 } 230 } 231 232 # Actual call. 233 my $args = join(', ', @args); 234 my $call = "$asm($sysname, $args)"; 235 236 # Assign return values. 237 my $body = ""; 238 my @ret = ("_", "_", "_"); 239 my $do_errno = 0; 240 for(my $i=0; $i<@out; $i++) { 241 my $p = $out[$i]; 242 my ($name, $type) = parseparam($p); 243 my $reg = ""; 244 if($name eq "err" && !$plan9) { 245 $reg = "e1"; 246 $ret[2] = $reg; 247 $do_errno = 1; 248 } elsif($name eq "err" && $plan9) { 249 $ret[0] = "r0"; 250 $ret[2] = "e1"; 251 next; 252 } else { 253 $reg = sprintf("r%d", $i); 254 $ret[$i] = $reg; 255 } 256 if($type eq "bool") { 257 $reg = "$reg != 0"; 258 } 259 if($type eq "int64" && $_32bit ne "") { 260 # 64-bit number in r1:r0 or r0:r1. 261 if($i+2 > @out) { 262 print STDERR "$ARGV:$.: not enough registers for int64 return\n"; 263 } 264 if($_32bit eq "big-endian") { 265 $reg = sprintf("int64(r%d)<<32 | int64(r%d)", $i, $i+1); 266 } else { 267 $reg = sprintf("int64(r%d)<<32 | int64(r%d)", $i+1, $i); 268 } 269 $ret[$i] = sprintf("r%d", $i); 270 $ret[$i+1] = sprintf("r%d", $i+1); 271 } 272 if($reg ne "e1" || $plan9) { 273 $body .= "\t$name = $type($reg)\n"; 274 } 275 } 276 if ($ret[0] eq "_" && $ret[1] eq "_" && $ret[2] eq "_") { 277 $text .= "\t$call\n"; 278 } else { 279 $text .= "\t$ret[0], $ret[1], $ret[2] := $call\n"; 280 } 281 $text .= $body; 282 283 if ($plan9 && $ret[2] eq "e1") { 284 $text .= "\tif int32(r0) == -1 {\n"; 285 $text .= "\t\terr = e1\n"; 286 $text .= "\t}\n"; 287 } elsif ($do_errno) { 288 $text .= "\tif e1 != 0 {\n"; 289 $text .= "\t\terr = e1\n"; 290 $text .= "\t}\n"; 291 } 292 $text .= "\treturn\n"; 293 $text .= "}\n\n"; 294 } 295 296 chomp $text; 297 chomp $text; 298 299 if($errors) { 300 exit 1; 301 } 302 303 print <<EOF; 304 // $cmdline 305 // MACHINE GENERATED BY THE COMMAND ABOVE; DO NOT EDIT 306 307 package syscall 308 309 import "unsafe" 310 311 $text 312 EOF 313 exit 0;