mksyscall_solaris.pl 6.8 KB

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