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