A lightweight mechanism to provide an *instant kickstart* to a Go web server instance upon changing any Go source files under the project directory (and its subdirectories).

mksyscall.pl 7.8KB

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