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