github.com/juelite/golang.org-x-sys@v0.0.0-20181121071242-7b69e1c5db33/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  if($ENV{'GOARCH'} eq "" || $ENV{'GOOS'} eq "") {
    67  	print STDERR "GOARCH or GOOS not defined in environment\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  	}
   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  	foreach my $use (@uses) {
   282  		$text .= "\t$use\n";
   283  	}
   284  	$text .= $body;
   285  	
   286  	if ($plan9 && $ret[2] eq "e1") {
   287  		$text .= "\tif int32(r0) == -1 {\n";
   288  		$text .= "\t\terr = e1\n";
   289  		$text .= "\t}\n";
   290  	} elsif ($do_errno) {
   291  		$text .= "\tif e1 != 0 {\n";
   292  		$text .= "\t\terr = errnoErr(e1)\n";
   293  		$text .= "\t}\n";
   294  	}
   295  	$text .= "\treturn\n";
   296  	$text .= "}\n\n";
   297  }
   298  
   299  chomp $text;
   300  chomp $text;
   301  
   302  if($errors) {
   303  	exit 1;
   304  }
   305  
   306  print <<EOF;
   307  // $cmdline
   308  // MACHINE GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
   309  
   310  // +build $ENV{'GOARCH'},$ENV{'GOOS'}
   311  
   312  package unix
   313  
   314  import (
   315  	"syscall"
   316  	"unsafe"
   317  )
   318  
   319  var _ syscall.Errno
   320  
   321  $text
   322  EOF
   323  exit 0;