mksyscall.pl 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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_darwin.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 errno.
  14. # A line beginning with //sysnb is like //sys, except that the
  15. # goroutine will not be suspended during the execution of the system
  16. # call. This must only be used for system calls which can never
  17. # block, as otherwise the system call could cause all goroutines to
  18. # hang.
  19. use strict;
  20. my $cmdline = "mksyscall.pl " . join(' ', @ARGV);
  21. my $errors = 0;
  22. my $_32bit = "";
  23. my $plan9 = 0;
  24. my $openbsd = 0;
  25. my $netbsd = 0;
  26. my $dragonfly = 0;
  27. my $arm = 0; # 64-bit value should use (even, odd)-pair
  28. if($ARGV[0] eq "-b32") {
  29. $_32bit = "big-endian";
  30. shift;
  31. } elsif($ARGV[0] eq "-l32") {
  32. $_32bit = "little-endian";
  33. shift;
  34. }
  35. if($ARGV[0] eq "-plan9") {
  36. $plan9 = 1;
  37. shift;
  38. }
  39. if($ARGV[0] eq "-openbsd") {
  40. $openbsd = 1;
  41. shift;
  42. }
  43. if($ARGV[0] eq "-netbsd") {
  44. $netbsd = 1;
  45. shift;
  46. }
  47. if($ARGV[0] eq "-dragonfly") {
  48. $dragonfly = 1;
  49. shift;
  50. }
  51. if($ARGV[0] eq "-arm") {
  52. $arm = 1;
  53. shift;
  54. }
  55. if($ARGV[0] =~ /^-/) {
  56. print STDERR "usage: mksyscall.pl [-b32 | -l32] [file ...]\n";
  57. exit 1;
  58. }
  59. sub parseparamlist($) {
  60. my ($list) = @_;
  61. $list =~ s/^\s*//;
  62. $list =~ s/\s*$//;
  63. if($list eq "") {
  64. return ();
  65. }
  66. return split(/\s*,\s*/, $list);
  67. }
  68. sub parseparam($) {
  69. my ($p) = @_;
  70. if($p !~ /^(\S*) (\S*)$/) {
  71. print STDERR "$ARGV:$.: malformed parameter: $p\n";
  72. $errors = 1;
  73. return ("xx", "int");
  74. }
  75. return ($1, $2);
  76. }
  77. my $text = "";
  78. while(<>) {
  79. chomp;
  80. s/\s+/ /g;
  81. s/^\s+//;
  82. s/\s+$//;
  83. my $nonblock = /^\/\/sysnb /;
  84. next if !/^\/\/sys / && !$nonblock;
  85. # Line must be of the form
  86. # func Open(path string, mode int, perm int) (fd int, errno error)
  87. # Split into name, in params, out params.
  88. if(!/^\/\/sys(nb)? (\w+)\(([^()]*)\)\s*(?:\(([^()]+)\))?\s*(?:=\s*((?i)SYS_[A-Z0-9_]+))?$/) {
  89. print STDERR "$ARGV:$.: malformed //sys declaration\n";
  90. $errors = 1;
  91. next;
  92. }
  93. my ($func, $in, $out, $sysname) = ($2, $3, $4, $5);
  94. # Split argument lists on comma.
  95. my @in = parseparamlist($in);
  96. my @out = parseparamlist($out);
  97. # Try in vain to keep people from editing this file.
  98. # The theory is that they jump into the middle of the file
  99. # without reading the header.
  100. $text .= "// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT\n\n";
  101. # Go function header.
  102. my $out_decl = @out ? sprintf(" (%s)", join(', ', @out)) : "";
  103. $text .= sprintf "func %s(%s)%s {\n", $func, join(', ', @in), $out_decl;
  104. # Check if err return available
  105. my $errvar = "";
  106. foreach my $p (@out) {
  107. my ($name, $type) = parseparam($p);
  108. if($type eq "error") {
  109. $errvar = $name;
  110. last;
  111. }
  112. }
  113. # Prepare arguments to Syscall.
  114. my @args = ();
  115. my @uses = ();
  116. my $n = 0;
  117. foreach my $p (@in) {
  118. my ($name, $type) = parseparam($p);
  119. if($type =~ /^\*/) {
  120. push @args, "uintptr(unsafe.Pointer($name))";
  121. } elsif($type eq "string" && $errvar ne "") {
  122. $text .= "\tvar _p$n *byte\n";
  123. $text .= "\t_p$n, $errvar = BytePtrFromString($name)\n";
  124. $text .= "\tif $errvar != nil {\n\t\treturn\n\t}\n";
  125. push @args, "uintptr(unsafe.Pointer(_p$n))";
  126. push @uses, "use(unsafe.Pointer(_p$n))";
  127. $n++;
  128. } elsif($type eq "string") {
  129. print STDERR "$ARGV:$.: $func uses string arguments, but has no error return\n";
  130. $text .= "\tvar _p$n *byte\n";
  131. $text .= "\t_p$n, _ = BytePtrFromString($name)\n";
  132. push @args, "uintptr(unsafe.Pointer(_p$n))";
  133. push @uses, "use(unsafe.Pointer(_p$n))";
  134. $n++;
  135. } elsif($type =~ /^\[\](.*)/) {
  136. # Convert slice into pointer, length.
  137. # Have to be careful not to take address of &a[0] if len == 0:
  138. # pass dummy pointer in that case.
  139. # Used to pass nil, but some OSes or simulators reject write(fd, nil, 0).
  140. $text .= "\tvar _p$n unsafe.Pointer\n";
  141. $text .= "\tif len($name) > 0 {\n\t\t_p$n = unsafe.Pointer(\&${name}[0])\n\t}";
  142. $text .= " else {\n\t\t_p$n = unsafe.Pointer(&_zero)\n\t}";
  143. $text .= "\n";
  144. push @args, "uintptr(_p$n)", "uintptr(len($name))";
  145. $n++;
  146. } elsif($type eq "int64" && ($openbsd || $netbsd)) {
  147. push @args, "0";
  148. if($_32bit eq "big-endian") {
  149. push @args, "uintptr($name>>32)", "uintptr($name)";
  150. } elsif($_32bit eq "little-endian") {
  151. push @args, "uintptr($name)", "uintptr($name>>32)";
  152. } else {
  153. push @args, "uintptr($name)";
  154. }
  155. } elsif($type eq "int64" && $dragonfly) {
  156. if ($func !~ /^extp(read|write)/i) {
  157. push @args, "0";
  158. }
  159. if($_32bit eq "big-endian") {
  160. push @args, "uintptr($name>>32)", "uintptr($name)";
  161. } elsif($_32bit eq "little-endian") {
  162. push @args, "uintptr($name)", "uintptr($name>>32)";
  163. } else {
  164. push @args, "uintptr($name)";
  165. }
  166. } elsif($type eq "int64" && $_32bit ne "") {
  167. if(@args % 2 && $arm) {
  168. # arm abi specifies 64-bit argument uses
  169. # (even, odd) pair
  170. push @args, "0"
  171. }
  172. if($_32bit eq "big-endian") {
  173. push @args, "uintptr($name>>32)", "uintptr($name)";
  174. } else {
  175. push @args, "uintptr($name)", "uintptr($name>>32)";
  176. }
  177. } else {
  178. push @args, "uintptr($name)";
  179. }
  180. }
  181. # Determine which form to use; pad args with zeros.
  182. my $asm = "Syscall";
  183. if ($nonblock) {
  184. $asm = "RawSyscall";
  185. }
  186. if(@args <= 3) {
  187. while(@args < 3) {
  188. push @args, "0";
  189. }
  190. } elsif(@args <= 6) {
  191. $asm .= "6";
  192. while(@args < 6) {
  193. push @args, "0";
  194. }
  195. } elsif(@args <= 9) {
  196. $asm .= "9";
  197. while(@args < 9) {
  198. push @args, "0";
  199. }
  200. } else {
  201. print STDERR "$ARGV:$.: too many arguments to system call\n";
  202. }
  203. # System call number.
  204. if($sysname eq "") {
  205. $sysname = "SYS_$func";
  206. $sysname =~ s/([a-z])([A-Z])/${1}_$2/g; # turn FooBar into Foo_Bar
  207. $sysname =~ y/a-z/A-Z/;
  208. }
  209. # Actual call.
  210. my $args = join(', ', @args);
  211. my $call = "$asm($sysname, $args)";
  212. # Assign return values.
  213. my $body = "";
  214. my @ret = ("_", "_", "_");
  215. my $do_errno = 0;
  216. for(my $i=0; $i<@out; $i++) {
  217. my $p = $out[$i];
  218. my ($name, $type) = parseparam($p);
  219. my $reg = "";
  220. if($name eq "err" && !$plan9) {
  221. $reg = "e1";
  222. $ret[2] = $reg;
  223. $do_errno = 1;
  224. } elsif($name eq "err" && $plan9) {
  225. $ret[0] = "r0";
  226. $ret[2] = "e1";
  227. next;
  228. } else {
  229. $reg = sprintf("r%d", $i);
  230. $ret[$i] = $reg;
  231. }
  232. if($type eq "bool") {
  233. $reg = "$reg != 0";
  234. }
  235. if($type eq "int64" && $_32bit ne "") {
  236. # 64-bit number in r1:r0 or r0:r1.
  237. if($i+2 > @out) {
  238. print STDERR "$ARGV:$.: not enough registers for int64 return\n";
  239. }
  240. if($_32bit eq "big-endian") {
  241. $reg = sprintf("int64(r%d)<<32 | int64(r%d)", $i, $i+1);
  242. } else {
  243. $reg = sprintf("int64(r%d)<<32 | int64(r%d)", $i+1, $i);
  244. }
  245. $ret[$i] = sprintf("r%d", $i);
  246. $ret[$i+1] = sprintf("r%d", $i+1);
  247. }
  248. if($reg ne "e1" || $plan9) {
  249. $body .= "\t$name = $type($reg)\n";
  250. }
  251. }
  252. if ($ret[0] eq "_" && $ret[1] eq "_" && $ret[2] eq "_") {
  253. $text .= "\t$call\n";
  254. } else {
  255. $text .= "\t$ret[0], $ret[1], $ret[2] := $call\n";
  256. }
  257. foreach my $use (@uses) {
  258. $text .= "\t$use\n";
  259. }
  260. $text .= $body;
  261. if ($plan9 && $ret[2] eq "e1") {
  262. $text .= "\tif int32(r0) == -1 {\n";
  263. $text .= "\t\terr = e1\n";
  264. $text .= "\t}\n";
  265. } elsif ($do_errno) {
  266. $text .= "\tif e1 != 0 {\n";
  267. $text .= "\t\terr = e1\n";
  268. $text .= "\t}\n";
  269. }
  270. $text .= "\treturn\n";
  271. $text .= "}\n\n";
  272. }
  273. chomp $text;
  274. chomp $text;
  275. if($errors) {
  276. exit 1;
  277. }
  278. print <<EOF;
  279. // $cmdline
  280. // MACHINE GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
  281. package unix
  282. import (
  283. "syscall"
  284. "unsafe"
  285. )
  286. var _ syscall.Errno
  287. $text
  288. EOF
  289. exit 0;