mksyscall_solaris.pl 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. # This program reads a file containing function prototypes
  6. # (like syscall_solaris.go) and generates system call bodies.
  7. # The prototypes are marked by lines beginning with "//sys"
  8. # and read like func declarations if //sys is replaced by func, but:
  9. # * The parameter lists must give a name for each argument.
  10. # This includes return parameters.
  11. # * The parameter lists must give a type for each argument:
  12. # the (x, y, z int) shorthand is not allowed.
  13. # * If the return parameter is an error number, it must be named err.
  14. # * If go func name needs to be different than its libc name,
  15. # * or the function is not in libc, name could be specified
  16. # * at the end, after "=" sign, like
  17. # //sys getsockopt(s int, level int, name int, val uintptr, vallen *_Socklen) (err error) = libsocket.getsockopt
  18. use strict;
  19. my $cmdline = "mksyscall_solaris.pl " . join(' ', @ARGV);
  20. my $errors = 0;
  21. my $_32bit = "";
  22. binmode STDOUT;
  23. if($ARGV[0] eq "-b32") {
  24. $_32bit = "big-endian";
  25. shift;
  26. } elsif($ARGV[0] eq "-l32") {
  27. $_32bit = "little-endian";
  28. shift;
  29. }
  30. if($ARGV[0] =~ /^-/) {
  31. print STDERR "usage: mksyscall_solaris.pl [-b32 | -l32] [file ...]\n";
  32. exit 1;
  33. }
  34. if($ENV{'GOARCH'} eq "" || $ENV{'GOOS'} eq "") {
  35. print STDERR "GOARCH or GOOS not defined in environment\n";
  36. exit 1;
  37. }
  38. sub parseparamlist($) {
  39. my ($list) = @_;
  40. $list =~ s/^\s*//;
  41. $list =~ s/\s*$//;
  42. if($list eq "") {
  43. return ();
  44. }
  45. return split(/\s*,\s*/, $list);
  46. }
  47. sub parseparam($) {
  48. my ($p) = @_;
  49. if($p !~ /^(\S*) (\S*)$/) {
  50. print STDERR "$ARGV:$.: malformed parameter: $p\n";
  51. $errors = 1;
  52. return ("xx", "int");
  53. }
  54. return ($1, $2);
  55. }
  56. my $package = "";
  57. my $text = "";
  58. my $vars = "";
  59. my $mods = "";
  60. my $modnames = "";
  61. while(<>) {
  62. chomp;
  63. s/\s+/ /g;
  64. s/^\s+//;
  65. s/\s+$//;
  66. $package = $1 if !$package && /^package (\S+)$/;
  67. my $nonblock = /^\/\/sysnb /;
  68. next if !/^\/\/sys / && !$nonblock;
  69. # Line must be of the form
  70. # func Open(path string, mode int, perm int) (fd int, err error)
  71. # Split into name, in params, out params.
  72. if(!/^\/\/sys(nb)? (\w+)\(([^()]*)\)\s*(?:\(([^()]+)\))?\s*(?:=\s*(?:(\w*)\.)?(\w*))?$/) {
  73. print STDERR "$ARGV:$.: malformed //sys declaration\n";
  74. $errors = 1;
  75. next;
  76. }
  77. my ($nb, $func, $in, $out, $modname, $sysname) = ($1, $2, $3, $4, $5, $6);
  78. # Split argument lists on comma.
  79. my @in = parseparamlist($in);
  80. my @out = parseparamlist($out);
  81. # So file name.
  82. if($modname eq "") {
  83. $modname = "libc";
  84. }
  85. my $modvname = "mod$modname";
  86. if($modnames !~ /$modname/) {
  87. $modnames .= ".$modname";
  88. $mods .= "\t$modvname = newLazySO(\"$modname.so\")\n";
  89. }
  90. # System call name.
  91. if($sysname eq "") {
  92. $sysname = "$func";
  93. }
  94. # System call pointer variable name.
  95. my $sysvarname = "proc$sysname";
  96. my $strconvfunc = "BytePtrFromString";
  97. my $strconvtype = "*byte";
  98. # Library proc address variable.
  99. $sysname =~ y/A-Z/a-z/; # All libc functions are lowercase.
  100. $vars .= "\t$sysvarname = $modvname.NewProc(\"$sysname\")\n";
  101. # Go function header.
  102. $out = join(', ', @out);
  103. if($out ne "") {
  104. $out = " ($out)";
  105. }
  106. if($text ne "") {
  107. $text .= "\n"
  108. }
  109. $text .= sprintf "func %s(%s)%s {\n", $func, join(', ', @in), $out;
  110. # Check if err return available
  111. my $errvar = "";
  112. foreach my $p (@out) {
  113. my ($name, $type) = parseparam($p);
  114. if($type eq "error") {
  115. $errvar = $name;
  116. last;
  117. }
  118. }
  119. # Prepare arguments to Syscall.
  120. my @args = ();
  121. my @uses = ();
  122. my $n = 0;
  123. foreach my $p (@in) {
  124. my ($name, $type) = parseparam($p);
  125. if($type =~ /^\*/) {
  126. push @args, "uintptr(unsafe.Pointer($name))";
  127. } elsif($type eq "string" && $errvar ne "") {
  128. $text .= "\tvar _p$n $strconvtype\n";
  129. $text .= "\t_p$n, $errvar = $strconvfunc($name)\n";
  130. $text .= "\tif $errvar != nil {\n\t\treturn\n\t}\n";
  131. push @args, "uintptr(unsafe.Pointer(_p$n))";
  132. push @uses, "use(unsafe.Pointer(_p$n))";
  133. $n++;
  134. } elsif($type eq "string") {
  135. print STDERR "$ARGV:$.: $func uses string arguments, but has no error return\n";
  136. $text .= "\tvar _p$n $strconvtype\n";
  137. $text .= "\t_p$n, _ = $strconvfunc($name)\n";
  138. push @args, "uintptr(unsafe.Pointer(_p$n))";
  139. push @uses, "use(unsafe.Pointer(_p$n))";
  140. $n++;
  141. } elsif($type =~ /^\[\](.*)/) {
  142. # Convert slice into pointer, length.
  143. # Have to be careful not to take address of &a[0] if len == 0:
  144. # pass nil in that case.
  145. $text .= "\tvar _p$n *$1\n";
  146. $text .= "\tif len($name) > 0 {\n\t\t_p$n = \&$name\[0]\n\t}\n";
  147. push @args, "uintptr(unsafe.Pointer(_p$n))", "uintptr(len($name))";
  148. $n++;
  149. } elsif($type eq "int64" && $_32bit ne "") {
  150. if($_32bit eq "big-endian") {
  151. push @args, "uintptr($name >> 32)", "uintptr($name)";
  152. } else {
  153. push @args, "uintptr($name)", "uintptr($name >> 32)";
  154. }
  155. } elsif($type eq "bool") {
  156. $text .= "\tvar _p$n uint32\n";
  157. $text .= "\tif $name {\n\t\t_p$n = 1\n\t} else {\n\t\t_p$n = 0\n\t}\n";
  158. push @args, "uintptr(_p$n)";
  159. $n++;
  160. } else {
  161. push @args, "uintptr($name)";
  162. }
  163. }
  164. my $nargs = @args;
  165. # Determine which form to use; pad args with zeros.
  166. my $asm = "sysvicall6";
  167. if ($nonblock) {
  168. $asm = "rawSysvicall6";
  169. }
  170. if(@args <= 6) {
  171. while(@args < 6) {
  172. push @args, "0";
  173. }
  174. } else {
  175. print STDERR "$ARGV:$.: too many arguments to system call\n";
  176. }
  177. # Actual call.
  178. my $args = join(', ', @args);
  179. my $call = "$asm($sysvarname.Addr(), $nargs, $args)";
  180. # Assign return values.
  181. my $body = "";
  182. my $failexpr = "";
  183. my @ret = ("_", "_", "_");
  184. my @pout= ();
  185. my $do_errno = 0;
  186. for(my $i=0; $i<@out; $i++) {
  187. my $p = $out[$i];
  188. my ($name, $type) = parseparam($p);
  189. my $reg = "";
  190. if($name eq "err") {
  191. $reg = "e1";
  192. $ret[2] = $reg;
  193. $do_errno = 1;
  194. } else {
  195. $reg = sprintf("r%d", $i);
  196. $ret[$i] = $reg;
  197. }
  198. if($type eq "bool") {
  199. $reg = "$reg != 0";
  200. }
  201. if($type eq "int64" && $_32bit ne "") {
  202. # 64-bit number in r1:r0 or r0:r1.
  203. if($i+2 > @out) {
  204. print STDERR "$ARGV:$.: not enough registers for int64 return\n";
  205. }
  206. if($_32bit eq "big-endian") {
  207. $reg = sprintf("int64(r%d)<<32 | int64(r%d)", $i, $i+1);
  208. } else {
  209. $reg = sprintf("int64(r%d)<<32 | int64(r%d)", $i+1, $i);
  210. }
  211. $ret[$i] = sprintf("r%d", $i);
  212. $ret[$i+1] = sprintf("r%d", $i+1);
  213. }
  214. if($reg ne "e1") {
  215. $body .= "\t$name = $type($reg)\n";
  216. }
  217. }
  218. if ($ret[0] eq "_" && $ret[1] eq "_" && $ret[2] eq "_") {
  219. $text .= "\t$call\n";
  220. } else {
  221. $text .= "\t$ret[0], $ret[1], $ret[2] := $call\n";
  222. }
  223. foreach my $use (@uses) {
  224. $text .= "\t$use\n";
  225. }
  226. $text .= $body;
  227. if ($do_errno) {
  228. $text .= "\tif e1 != 0 {\n";
  229. $text .= "\t\terr = e1\n";
  230. $text .= "\t}\n";
  231. }
  232. $text .= "\treturn\n";
  233. $text .= "}\n";
  234. }
  235. if($errors) {
  236. exit 1;
  237. }
  238. print <<EOF;
  239. // $cmdline
  240. // MACHINE GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
  241. // +build $ENV{'GOARCH'},$ENV{'GOOS'}
  242. package $package
  243. import "unsafe"
  244. EOF
  245. print "import \"golang.org/x/sys/unix\"\n" if $package ne "unix";
  246. print <<EOF;
  247. var (
  248. $mods
  249. $vars
  250. )
  251. $text
  252. EOF
  253. exit 0;