mksyscall_solaris.pl 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 $dynimports = "";
  59. my $linknames = "";
  60. my @vars = ();
  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. # System call name.
  86. if($sysname eq "") {
  87. $sysname = "$func";
  88. }
  89. # System call pointer variable name.
  90. my $sysvarname = "proc$sysname";
  91. my $strconvfunc = "BytePtrFromString";
  92. my $strconvtype = "*byte";
  93. $sysname =~ y/A-Z/a-z/; # All libc functions are lowercase.
  94. # Runtime import of function to allow cross-platform builds.
  95. $dynimports .= "//go:cgo_import_dynamic libc_${sysname} ${sysname} \"$modname.so\"\n";
  96. # Link symbol to proc address variable.
  97. $linknames .= "//go:linkname ${sysvarname} libc_${sysname}\n";
  98. # Library proc address variable.
  99. push @vars, $sysvarname;
  100. # Go function header.
  101. $out = join(', ', @out);
  102. if($out ne "") {
  103. $out = " ($out)";
  104. }
  105. if($text ne "") {
  106. $text .= "\n"
  107. }
  108. $text .= sprintf "func %s(%s)%s {\n", $func, join(', ', @in), $out;
  109. # Check if err return available
  110. my $errvar = "";
  111. foreach my $p (@out) {
  112. my ($name, $type) = parseparam($p);
  113. if($type eq "error") {
  114. $errvar = $name;
  115. last;
  116. }
  117. }
  118. # Prepare arguments to Syscall.
  119. my @args = ();
  120. my @uses = ();
  121. my $n = 0;
  122. foreach my $p (@in) {
  123. my ($name, $type) = parseparam($p);
  124. if($type =~ /^\*/) {
  125. push @args, "uintptr(unsafe.Pointer($name))";
  126. } elsif($type eq "string" && $errvar ne "") {
  127. $text .= "\tvar _p$n $strconvtype\n";
  128. $text .= "\t_p$n, $errvar = $strconvfunc($name)\n";
  129. $text .= "\tif $errvar != nil {\n\t\treturn\n\t}\n";
  130. push @args, "uintptr(unsafe.Pointer(_p$n))";
  131. push @uses, "use(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. push @uses, "use(unsafe.Pointer(_p$n))";
  139. $n++;
  140. } elsif($type =~ /^\[\](.*)/) {
  141. # Convert slice into pointer, length.
  142. # Have to be careful not to take address of &a[0] if len == 0:
  143. # pass nil in that case.
  144. $text .= "\tvar _p$n *$1\n";
  145. $text .= "\tif len($name) > 0 {\n\t\t_p$n = \&$name\[0]\n\t}\n";
  146. push @args, "uintptr(unsafe.Pointer(_p$n))", "uintptr(len($name))";
  147. $n++;
  148. } elsif($type eq "int64" && $_32bit ne "") {
  149. if($_32bit eq "big-endian") {
  150. push @args, "uintptr($name >> 32)", "uintptr($name)";
  151. } else {
  152. push @args, "uintptr($name)", "uintptr($name >> 32)";
  153. }
  154. } elsif($type eq "bool") {
  155. $text .= "\tvar _p$n uint32\n";
  156. $text .= "\tif $name {\n\t\t_p$n = 1\n\t} else {\n\t\t_p$n = 0\n\t}\n";
  157. push @args, "uintptr(_p$n)";
  158. $n++;
  159. } else {
  160. push @args, "uintptr($name)";
  161. }
  162. }
  163. my $nargs = @args;
  164. # Determine which form to use; pad args with zeros.
  165. my $asm = "sysvicall6";
  166. if ($nonblock) {
  167. $asm = "rawSysvicall6";
  168. }
  169. if(@args <= 6) {
  170. while(@args < 6) {
  171. push @args, "0";
  172. }
  173. } else {
  174. print STDERR "$ARGV:$.: too many arguments to system call\n";
  175. }
  176. # Actual call.
  177. my $args = join(', ', @args);
  178. my $call = "$asm(uintptr(unsafe.Pointer(&$sysvarname)), $nargs, $args)";
  179. # Assign return values.
  180. my $body = "";
  181. my $failexpr = "";
  182. my @ret = ("_", "_", "_");
  183. my @pout= ();
  184. my $do_errno = 0;
  185. for(my $i=0; $i<@out; $i++) {
  186. my $p = $out[$i];
  187. my ($name, $type) = parseparam($p);
  188. my $reg = "";
  189. if($name eq "err") {
  190. $reg = "e1";
  191. $ret[2] = $reg;
  192. $do_errno = 1;
  193. } else {
  194. $reg = sprintf("r%d", $i);
  195. $ret[$i] = $reg;
  196. }
  197. if($type eq "bool") {
  198. $reg = "$reg != 0";
  199. }
  200. if($type eq "int64" && $_32bit ne "") {
  201. # 64-bit number in r1:r0 or r0:r1.
  202. if($i+2 > @out) {
  203. print STDERR "$ARGV:$.: not enough registers for int64 return\n";
  204. }
  205. if($_32bit eq "big-endian") {
  206. $reg = sprintf("int64(r%d)<<32 | int64(r%d)", $i, $i+1);
  207. } else {
  208. $reg = sprintf("int64(r%d)<<32 | int64(r%d)", $i+1, $i);
  209. }
  210. $ret[$i] = sprintf("r%d", $i);
  211. $ret[$i+1] = sprintf("r%d", $i+1);
  212. }
  213. if($reg ne "e1") {
  214. $body .= "\t$name = $type($reg)\n";
  215. }
  216. }
  217. if ($ret[0] eq "_" && $ret[1] eq "_" && $ret[2] eq "_") {
  218. $text .= "\t$call\n";
  219. } else {
  220. $text .= "\t$ret[0], $ret[1], $ret[2] := $call\n";
  221. }
  222. foreach my $use (@uses) {
  223. $text .= "\t$use\n";
  224. }
  225. $text .= $body;
  226. if ($do_errno) {
  227. $text .= "\tif e1 != 0 {\n";
  228. $text .= "\t\terr = e1\n";
  229. $text .= "\t}\n";
  230. }
  231. $text .= "\treturn\n";
  232. $text .= "}\n";
  233. }
  234. if($errors) {
  235. exit 1;
  236. }
  237. print <<EOF;
  238. // $cmdline
  239. // MACHINE GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
  240. // +build $ENV{'GOARCH'},$ENV{'GOOS'}
  241. package $package
  242. import (
  243. "syscall"
  244. "unsafe"
  245. )
  246. EOF
  247. print "import \"golang.org/x/sys/unix\"\n" if $package ne "unix";
  248. my $vardecls = "\t" . join(",\n\t", @vars);
  249. $vardecls .= " syscallFunc";
  250. chomp($_=<<EOF);
  251. $dynimports
  252. $linknames
  253. var (
  254. $vardecls
  255. )
  256. $text
  257. EOF
  258. print $_;
  259. exit 0;