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