mksyscall.pl 7.9 KB

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