Home | History | Annotate | Download | only in syscall
      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 @uses = ();
    136 	my $n = 0;
    137 	foreach my $p (@in) {
    138 		my ($name, $type) = parseparam($p);
    139 		if($type =~ /^\*/) {
    140 			push @args, "uintptr(unsafe.Pointer($name))";
    141 		} elsif($type eq "string" && $errvar ne "") {
    142 			$text .= "\tvar _p$n *byte\n";
    143 			$text .= "\t_p$n, $errvar = BytePtrFromString($name)\n";
    144 			$text .= "\tif $errvar != nil {\n\t\treturn\n\t}\n";
    145 			push @args, "uintptr(unsafe.Pointer(_p$n))";
    146 			push @uses, "use(unsafe.Pointer(_p$n))";
    147 			$n++;
    148 		} elsif($type eq "string") {
    149 			print STDERR "$ARGV:$.: $func uses string arguments, but has no error return\n";
    150 			$text .= "\tvar _p$n *byte\n";
    151 			$text .= "\t_p$n, _ = BytePtrFromString($name)\n";
    152 			push @args, "uintptr(unsafe.Pointer(_p$n))";
    153 			push @uses, "use(unsafe.Pointer(_p$n))";
    154 			$n++;
    155 		} elsif($type =~ /^\[\](.*)/) {
    156 			# Convert slice into pointer, length.
    157 			# Have to be careful not to take address of &a[0] if len == 0:
    158 			# pass dummy pointer in that case.
    159 			# Used to pass nil, but some OSes or simulators reject write(fd, nil, 0).
    160 			$text .= "\tvar _p$n unsafe.Pointer\n";
    161 			$text .= "\tif len($name) > 0 {\n\t\t_p$n = unsafe.Pointer(\&${name}[0])\n\t}";
    162 			$text .= " else {\n\t\t_p$n = unsafe.Pointer(&_zero)\n\t}";
    163 			$text .= "\n";
    164 			push @args, "uintptr(_p$n)", "uintptr(len($name))";
    165 			$n++;
    166 		} elsif($type eq "int64" && ($openbsd || $netbsd)) {
    167 			push @args, "0";
    168 			if($_32bit eq "big-endian") {
    169 				push @args, "uintptr($name>>32)", "uintptr($name)";
    170 			} elsif($_32bit eq "little-endian") {
    171 				push @args, "uintptr($name)", "uintptr($name>>32)";
    172 			} else {
    173 				push @args, "uintptr($name)";
    174 			}
    175 		} elsif($type eq "int64" && $dragonfly) {
    176 			if ($func !~ /^extp(read|write)/i) {
    177 				push @args, "0";
    178 			}
    179 			if($_32bit eq "big-endian") {
    180 				push @args, "uintptr($name>>32)", "uintptr($name)";
    181 			} elsif($_32bit eq "little-endian") {
    182 				push @args, "uintptr($name)", "uintptr($name>>32)";
    183 			} else {
    184 				push @args, "uintptr($name)";
    185 			}
    186 		} elsif($type eq "int64" && $_32bit ne "") {
    187 			if(@args % 2 && $arm) {
    188 				# arm abi specifies 64-bit argument uses 
    189 				# (even, odd) pair
    190 				push @args, "0"
    191 			}
    192 			if($_32bit eq "big-endian") {
    193 				push @args, "uintptr($name>>32)", "uintptr($name)";
    194 			} else {
    195 				push @args, "uintptr($name)", "uintptr($name>>32)";
    196 			}
    197 		} else {
    198 			push @args, "uintptr($name)";
    199 		}
    200 	}
    201 
    202 	# Determine which form to use; pad args with zeros.
    203 	my $asm = "Syscall";
    204 	if ($nonblock) {
    205 		$asm = "RawSyscall";
    206 	}
    207 	if(@args <= 3) {
    208 		while(@args < 3) {
    209 			push @args, "0";
    210 		}
    211 	} elsif(@args <= 6) {
    212 		$asm .= "6";
    213 		while(@args < 6) {
    214 			push @args, "0";
    215 		}
    216 	} elsif(@args <= 9) {
    217 		$asm .= "9";
    218 		while(@args < 9) {
    219 			push @args, "0";
    220 		}
    221 	} else {
    222 		print STDERR "$ARGV:$.: too many arguments to system call\n";
    223 	}
    224 
    225 	# System call number.
    226 	if($sysname eq "") {
    227 		$sysname = "SYS_$func";
    228 		$sysname =~ s/([a-z])([A-Z])/${1}_$2/g;	# turn FooBar into Foo_Bar
    229 		$sysname =~ y/a-z/A-Z/;
    230 		if($nacl) {
    231 			$sysname =~ y/A-Z/a-z/;
    232 		}
    233 	}
    234 
    235 	# Actual call.
    236 	my $args = join(', ', @args);
    237 	my $call = "$asm($sysname, $args)";
    238 
    239 	# Assign return values.
    240 	my $body = "";
    241 	my @ret = ("_", "_", "_");
    242 	my $do_errno = 0;
    243 	for(my $i=0; $i<@out; $i++) {
    244 		my $p = $out[$i];
    245 		my ($name, $type) = parseparam($p);
    246 		my $reg = "";
    247 		if($name eq "err" && !$plan9) {
    248 			$reg = "e1";
    249 			$ret[2] = $reg;
    250 			$do_errno = 1;
    251 		} elsif($name eq "err" && $plan9) {
    252 			$ret[0] = "r0";
    253 			$ret[2] = "e1";
    254 			next;
    255 		} else {
    256 			$reg = sprintf("r%d", $i);
    257 			$ret[$i] = $reg;
    258 		}
    259 		if($type eq "bool") {
    260 			$reg = "$reg != 0";
    261 		}
    262 		if($type eq "int64" && $_32bit ne "") {
    263 			# 64-bit number in r1:r0 or r0:r1.
    264 			if($i+2 > @out) {
    265 				print STDERR "$ARGV:$.: not enough registers for int64 return\n";
    266 			}
    267 			if($_32bit eq "big-endian") {
    268 				$reg = sprintf("int64(r%d)<<32 | int64(r%d)", $i, $i+1);
    269 			} else {
    270 				$reg = sprintf("int64(r%d)<<32 | int64(r%d)", $i+1, $i);
    271 			}
    272 			$ret[$i] = sprintf("r%d", $i);
    273 			$ret[$i+1] = sprintf("r%d", $i+1);
    274 		}
    275 		if($reg ne "e1" || $plan9) {
    276 			$body .= "\t$name = $type($reg)\n";
    277 		}
    278 	}
    279 	if ($ret[0] eq "_" && $ret[1] eq "_" && $ret[2] eq "_") {
    280 		$text .= "\t$call\n";
    281 	} else {
    282 		$text .= "\t$ret[0], $ret[1], $ret[2] := $call\n";
    283 	}
    284 	foreach my $use (@uses) {
    285 		$text .= "\t$use\n";
    286 	}
    287 	$text .= $body;
    288 	
    289 	if ($plan9 && $ret[2] eq "e1") {
    290 		$text .= "\tif int32(r0) == -1 {\n";
    291 		$text .= "\t\terr = e1\n";
    292 		$text .= "\t}\n";
    293 	} elsif ($do_errno) {
    294 		$text .= "\tif e1 != 0 {\n";
    295 		$text .= "\t\terr = errnoErr(e1)\n";
    296 		$text .= "\t}\n";
    297 	}
    298 	$text .= "\treturn\n";
    299 	$text .= "}\n\n";
    300 }
    301 
    302 chomp $text;
    303 chomp $text;
    304 
    305 if($errors) {
    306 	exit 1;
    307 }
    308 
    309 print <<EOF;
    310 // $cmdline
    311 // MACHINE GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
    312 
    313 package syscall
    314 
    315 import "unsafe"
    316 
    317 $text
    318 EOF
    319 exit 0;
    320