You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

319 lines
7.7 KiB

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