mksyscall_solaris.pl 6.9 KB

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