mksyscall_aix.pl 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. #!/usr/bin/env perl
  2. # Copyright 2018 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_aix.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_aix.pl " . join(' ', @ARGV);
  20. my $errors = 0;
  21. my $_32bit = "";
  22. my $tags = ""; # build tags
  23. my $aix = 0;
  24. my $solaris = 0;
  25. binmode STDOUT;
  26. if($ARGV[0] eq "-b32") {
  27. $_32bit = "big-endian";
  28. shift;
  29. } elsif($ARGV[0] eq "-l32") {
  30. $_32bit = "little-endian";
  31. shift;
  32. }
  33. if($ARGV[0] eq "-aix") {
  34. $aix = 1;
  35. shift;
  36. }
  37. if($ARGV[0] eq "-tags") {
  38. shift;
  39. $tags = $ARGV[0];
  40. shift;
  41. }
  42. if($ARGV[0] =~ /^-/) {
  43. print STDERR "usage: mksyscall_aix.pl [-b32 | -l32] [-tags x,y] [file ...]\n";
  44. exit 1;
  45. }
  46. sub parseparamlist($) {
  47. my ($list) = @_;
  48. $list =~ s/^\s*//;
  49. $list =~ s/\s*$//;
  50. if($list eq "") {
  51. return ();
  52. }
  53. return split(/\s*,\s*/, $list);
  54. }
  55. sub parseparam($) {
  56. my ($p) = @_;
  57. if($p !~ /^(\S*) (\S*)$/) {
  58. print STDERR "$ARGV:$.: malformed parameter: $p\n";
  59. $errors = 1;
  60. return ("xx", "int");
  61. }
  62. return ($1, $2);
  63. }
  64. my $package = "";
  65. my $text = "";
  66. my $c_extern = "/*\n#include <stdint.h>\n";
  67. my @vars = ();
  68. while(<>) {
  69. chomp;
  70. s/\s+/ /g;
  71. s/^\s+//;
  72. s/\s+$//;
  73. $package = $1 if !$package && /^package (\S+)$/;
  74. my $nonblock = /^\/\/sysnb /;
  75. next if !/^\/\/sys / && !$nonblock;
  76. # Line must be of the form
  77. # func Open(path string, mode int, perm int) (fd int, err error)
  78. # Split into name, in params, out params.
  79. if(!/^\/\/sys(nb)? (\w+)\(([^()]*)\)\s*(?:\(([^()]+)\))?\s*(?:=\s*(?:(\w*)\.)?(\w*))?$/) {
  80. print STDERR "$ARGV:$.: malformed //sys declaration\n";
  81. $errors = 1;
  82. next;
  83. }
  84. my ($nb, $func, $in, $out, $modname, $sysname) = ($1, $2, $3, $4, $5, $6);
  85. # Split argument lists on comma.
  86. my @in = parseparamlist($in);
  87. my @out = parseparamlist($out);
  88. $in = join(', ', @in);
  89. $out = join(', ', @out);
  90. # Try in vain to keep people from editing this file.
  91. # The theory is that they jump into the middle of the file
  92. # without reading the header.
  93. $text .= "// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT\n\n";
  94. # Check if value return, err return available
  95. my $errvar = "";
  96. my $retvar = "";
  97. my $rettype = "";
  98. foreach my $p (@out) {
  99. my ($name, $type) = parseparam($p);
  100. if($type eq "error") {
  101. $errvar = $name;
  102. } else {
  103. $retvar = $name;
  104. $rettype = $type;
  105. }
  106. }
  107. # System call name.
  108. #if($func ne "fcntl") {
  109. if($sysname eq "") {
  110. $sysname = "$func";
  111. }
  112. $sysname =~ s/([a-z])([A-Z])/${1}_$2/g;
  113. $sysname =~ y/A-Z/a-z/; # All libc functions are lowercase.
  114. my $C_rettype = "";
  115. if($rettype eq "unsafe.Pointer") {
  116. $C_rettype = "uintptr_t";
  117. } elsif($rettype eq "uintptr") {
  118. $C_rettype = "uintptr_t";
  119. } elsif($rettype =~ /^_/) {
  120. $C_rettype = "uintptr_t";
  121. } elsif($rettype eq "int") {
  122. $C_rettype = "int";
  123. } elsif($rettype eq "int32") {
  124. $C_rettype = "int";
  125. } elsif($rettype eq "int64") {
  126. $C_rettype = "long long";
  127. } elsif($rettype eq "uint32") {
  128. $C_rettype = "unsigned int";
  129. } elsif($rettype eq "uint64") {
  130. $C_rettype = "unsigned long long";
  131. } else {
  132. $C_rettype = "int";
  133. }
  134. if($sysname eq "exit") {
  135. $C_rettype = "void";
  136. }
  137. # Change types to c
  138. my @c_in = ();
  139. foreach my $p (@in) {
  140. my ($name, $type) = parseparam($p);
  141. if($type =~ /^\*/) {
  142. push @c_in, "uintptr_t";
  143. } elsif($type eq "string") {
  144. push @c_in, "uintptr_t";
  145. } elsif($type =~ /^\[\](.*)/) {
  146. push @c_in, "uintptr_t", "size_t";
  147. } elsif($type eq "unsafe.Pointer") {
  148. push @c_in, "uintptr_t";
  149. } elsif($type eq "uintptr") {
  150. push @c_in, "uintptr_t";
  151. } elsif($type =~ /^_/) {
  152. push @c_in, "uintptr_t";
  153. } elsif($type eq "int") {
  154. push @c_in, "int";
  155. } elsif($type eq "int32") {
  156. push @c_in, "int";
  157. } elsif($type eq "int64") {
  158. push @c_in, "long long";
  159. } elsif($type eq "uint32") {
  160. push @c_in, "unsigned int";
  161. } elsif($type eq "uint64") {
  162. push @c_in, "unsigned long long";
  163. } else {
  164. push @c_in, "int";
  165. }
  166. }
  167. if ($func ne "fcntl" && $func ne "FcntlInt" && $func ne "readlen" && $func ne "writelen") {
  168. # Imports of system calls from libc
  169. $c_extern .= "$C_rettype $sysname";
  170. my $c_in = join(', ', @c_in);
  171. $c_extern .= "($c_in);\n";
  172. }
  173. # So file name.
  174. if($aix) {
  175. if($modname eq "") {
  176. $modname = "libc.a/shr_64.o";
  177. } else {
  178. print STDERR "$func: only syscall using libc are available\n";
  179. $errors = 1;
  180. next;
  181. }
  182. }
  183. my $strconvfunc = "C.CString";
  184. my $strconvtype = "*byte";
  185. # Go function header.
  186. if($out ne "") {
  187. $out = " ($out)";
  188. }
  189. if($text ne "") {
  190. $text .= "\n"
  191. }
  192. $text .= sprintf "func %s(%s)%s {\n", $func, join(', ', @in), $out ;
  193. # Prepare arguments to call.
  194. my @args = ();
  195. my $n = 0;
  196. my $arg_n = 0;
  197. foreach my $p (@in) {
  198. my ($name, $type) = parseparam($p);
  199. if($type =~ /^\*/) {
  200. push @args, "C.uintptr_t(uintptr(unsafe.Pointer($name)))";
  201. } elsif($type eq "string" && $errvar ne "") {
  202. $text .= "\t_p$n := uintptr(unsafe.Pointer($strconvfunc($name)))\n";
  203. push @args, "C.uintptr_t(_p$n)";
  204. $n++;
  205. } elsif($type eq "string") {
  206. print STDERR "$ARGV:$.: $func uses string arguments, but has no error return\n";
  207. $text .= "\t_p$n := uintptr(unsafe.Pointer($strconvfunc($name)))\n";
  208. push @args, "C.uintptr_t(_p$n)";
  209. $n++;
  210. } elsif($type =~ /^\[\](.*)/) {
  211. # Convert slice into pointer, length.
  212. # Have to be careful not to take address of &a[0] if len == 0:
  213. # pass nil in that case.
  214. $text .= "\tvar _p$n *$1\n";
  215. $text .= "\tif len($name) > 0 {\n\t\t_p$n = \&$name\[0]\n\t}\n";
  216. push @args, "C.uintptr_t(uintptr(unsafe.Pointer(_p$n)))";
  217. $n++;
  218. $text .= "\tvar _p$n int\n";
  219. $text .= "\t_p$n = len($name)\n";
  220. push @args, "C.size_t(_p$n)";
  221. $n++;
  222. } elsif($type eq "int64" && $_32bit ne "") {
  223. if($_32bit eq "big-endian") {
  224. push @args, "uintptr($name >> 32)", "uintptr($name)";
  225. } else {
  226. push @args, "uintptr($name)", "uintptr($name >> 32)";
  227. }
  228. $n++;
  229. } elsif($type eq "bool") {
  230. $text .= "\tvar _p$n uint32\n";
  231. $text .= "\tif $name {\n\t\t_p$n = 1\n\t} else {\n\t\t_p$n = 0\n\t}\n";
  232. push @args, "_p$n";
  233. $n++;
  234. } elsif($type =~ /^_/) {
  235. push @args, "C.uintptr_t(uintptr($name))";
  236. } elsif($type eq "unsafe.Pointer") {
  237. push @args, "C.uintptr_t(uintptr($name))";
  238. } elsif($type eq "int") {
  239. if (($arg_n == 2) && (($func eq "readlen") || ($func eq "writelen"))) {
  240. push @args, "C.size_t($name)";
  241. } elsif ($arg_n == 0 && $func eq "fcntl") {
  242. push @args, "C.uintptr_t($name)";
  243. } elsif (($arg_n == 2) && (($func eq "fcntl") || ($func eq "FcntlInt"))) {
  244. push @args, "C.uintptr_t($name)";
  245. } else {
  246. push @args, "C.int($name)";
  247. }
  248. } elsif($type eq "int32") {
  249. push @args, "C.int($name)";
  250. } elsif($type eq "int64") {
  251. push @args, "C.longlong($name)";
  252. } elsif($type eq "uint32") {
  253. push @args, "C.uint($name)";
  254. } elsif($type eq "uint64") {
  255. push @args, "C.ulonglong($name)";
  256. } elsif($type eq "uintptr") {
  257. push @args, "C.uintptr_t($name)";
  258. } else {
  259. push @args, "C.int($name)";
  260. }
  261. $arg_n++;
  262. }
  263. my $nargs = @args;
  264. # Determine which form to use; pad args with zeros.
  265. if ($nonblock) {
  266. }
  267. my $args = join(', ', @args);
  268. my $call = "";
  269. if ($sysname eq "exit") {
  270. if ($errvar ne "") {
  271. $call .= "er :=";
  272. } else {
  273. $call .= "";
  274. }
  275. } elsif ($errvar ne "") {
  276. $call .= "r0,er :=";
  277. } elsif ($retvar ne "") {
  278. $call .= "r0,_ :=";
  279. } else {
  280. $call .= ""
  281. }
  282. $call .= "C.$sysname($args)";
  283. # Assign return values.
  284. my $body = "";
  285. my $failexpr = "";
  286. for(my $i=0; $i<@out; $i++) {
  287. my $p = $out[$i];
  288. my ($name, $type) = parseparam($p);
  289. my $reg = "";
  290. if($name eq "err") {
  291. $reg = "e1";
  292. } else {
  293. $reg = "r0";
  294. }
  295. if($reg ne "e1" ) {
  296. $body .= "\t$name = $type($reg)\n";
  297. }
  298. }
  299. # verify return
  300. if ($sysname ne "exit" && $errvar ne "") {
  301. if ($C_rettype =~ /^uintptr/) {
  302. $body .= "\tif \(uintptr\(r0\) ==\^uintptr\(0\) && er != nil\) {\n";
  303. $body .= "\t\t$errvar = er\n";
  304. $body .= "\t}\n";
  305. } else {
  306. $body .= "\tif \(r0 ==-1 && er != nil\) {\n";
  307. $body .= "\t\t$errvar = er\n";
  308. $body .= "\t}\n";
  309. }
  310. } elsif ($errvar ne "") {
  311. $body .= "\tif \(er != nil\) {\n";
  312. $body .= "\t\t$errvar = er\n";
  313. $body .= "\t}\n";
  314. }
  315. $text .= "\t$call\n";
  316. $text .= $body;
  317. $text .= "\treturn\n";
  318. $text .= "}\n";
  319. }
  320. if($errors) {
  321. exit 1;
  322. }
  323. print <<EOF;
  324. // $cmdline
  325. // Code generated by the command above; see README.md. DO NOT EDIT.
  326. // +build $tags
  327. package $package
  328. $c_extern
  329. */
  330. import "C"
  331. import (
  332. "unsafe"
  333. "syscall"
  334. )
  335. EOF
  336. print "import \"golang.org/x/sys/unix\"\n" if $package ne "unix";
  337. chomp($_=<<EOF);
  338. $text
  339. EOF
  340. print $_;
  341. exit 0;