2013-12-05 Richard Biener <rguenther@suse.de>
[official-gcc.git] / libgo / go / syscall / mksyscall.awk
blobdaf6554a6cd58d7f3e8294635babccc532d4f6f5
1 # Copyright 2011 The Go Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style
3 # license that can be found in the LICENSE file.
5 # This AWK script reads a Go file with comments describing syscall
6 # functions and the C routines they map to. It generates the Go code
7 # which calls the C routines.
9 # The syscall functins are marked by lines beginning with "//sys" and
10 # read like func declarations if //sys is replaced by func, but:
11 # * The parameter lists must give a name for each argument.
12 # This includes return parameters.
13 # * The parameter lists must give a type for each argument:
14 # the (x, y, z int) shorthand is not allowed.
15 # * If the return parameter is an error, it must be named err.
17 # A line beginning with //sysnb is like //sys, except that the
18 # goroutine will not be suspended during the execution of the library
19 # call. This must only be used for library calls which can never
20 # block, as otherwise the library call could cause all goroutines to
21 # hang.
23 # After the //sys or //sysnb line comes a second line which describes
24 # the C function. The name must be the name of the function in the C
25 # library, and may be the same as the Go function. The limitations on
26 # the argument list are the same as for the //sys line, but there must
27 # be at most one result parameter, and it must be given as just a
28 # type, without a name.
30 BEGIN {
31 print "// This file was automatically generated by mksyscall.awk"
32 print ""
33 print "package syscall"
34 print ""
35 print "import \"unsafe\""
36 print ""
37 status = 0
40 /^\/\/sys/ {
41 if ($1 == "//sysnb") {
42 blocking = 0
43 } else {
44 blocking = 1
47 line = $0
49 if (match(line, "//sys(nb)?[ ]*[a-zA-Z0-9_]+\\([^()]*\\) *(\\(([^()]+)\\))?") == 0) {
50 print "unmatched line:", $0 | "cat 1>&2"
51 status = 1
52 next
55 # Sets a[1] = //sysnb, a[2] == function name.
56 split(line, a, "[ (]+")
57 gofnname = a[2]
59 off = match(line, "\\([^()]*\\)")
60 end = index(substr(line, off, length(line) - off + 1), ")")
61 gofnparams = substr(line, off + 1, end - 2)
63 line = substr(line, off + end, length(line) - (off + end) + 1)
64 off = match(line, "\\([^()]*\\)")
65 if (off == 0) {
66 gofnresults = ""
67 } else {
68 end = index(substr(line, off, length(line) - off + 1), ")")
69 gofnresults = substr(line, off + 1, end - 2)
72 getline
73 line = $0
75 if (match(line, "//[a-zA-Z0-9_]+\\([^()]*\\)") == 0) {
76 print "unmatched C line", $0, "after", gofnname | "cat 1>&2"
77 status = 1
78 next
81 split(line, a, "[ (]+")
82 cfnname = substr(a[1], 3, length(a[1]) - 2)
84 off = match(line, "\\([^()]*\\)")
85 end = index(substr(line, off, length(line) - off + 1), ")")
86 cfnparams = substr(line, off + 1, end - 2)
88 line = substr(line, off + end + 1, length(line) - (off + end) + 1)
89 while (substr(line, 1, 1) == " ") {
90 line = substr(line, 2, length(line) - 1)
92 end = index(line, " ")
93 if (end != 0) {
94 line = substr(line, 1, end)
96 cfnresult = line
98 printf("// Automatically generated wrapper for %s/%s\n", gofnname, cfnname)
99 printf("//extern %s\n", cfnname)
100 printf("func c_%s(%s) %s\n", cfnname, cfnparams, cfnresult)
101 printf("func %s(%s) %s%s%s%s{\n",
102 gofnname, gofnparams, gofnresults == "" ? "" : "(", gofnresults,
103 gofnresults == "" ? "" : ")", gofnresults == "" ? "" : " ")
105 loc = gofnname "/" cfnname ":"
107 haserr = 0
108 if (gofnresults != "") {
109 fields = split(gofnresults, goresults, ", *")
110 for (goresult = 1; goresults[goresult] != ""; goresult++) {
111 if (split(goresults[goresult], goparam) == 2) {
112 if (goparam[1] == "err") {
113 haserr = 1
114 break
120 split(gofnparams, goargs, ", *")
121 split(cfnparams, cargs, ", *")
122 args = ""
123 carg = 1
124 for (goarg = 1; goargs[goarg] != ""; goarg++) {
125 if (cargs[carg] == "") {
126 print loc, "not enough C parameters"
129 if (args != "") {
130 args = args ", "
133 if (split(goargs[goarg], a) != 2) {
134 print loc, "bad parameter:", goargs[goarg] | "cat 1>&2"
135 status = 1
136 next
139 goname = a[1]
140 gotype = a[2]
142 if (split(cargs[carg], a) != 2) {
143 print loc, "bad C parameter:", cargs[carg] | "cat 1>&2"
144 status = 1
145 next
148 ctype = a[2]
150 if (gotype ~ /^\*/) {
151 if (gotype != ctype) {
152 print loc, "Go/C pointer type mismatch:", gotype, ctype | "cat 1>&2"
153 status = 1
154 next
156 args = args goname
157 } else if (gotype == "string") {
158 if (ctype != "*byte") {
159 print loc, "Go string not matched to C *byte:", gotype, ctype | "cat 1>&2"
160 status = 1
161 next
163 printf("\tvar _p%d *byte\n", goarg)
164 if (haserr) {
165 printf("\t_p%d, err = BytePtrFromString(%s)\n", goarg, goname)
166 printf("\tif err != nil {\n\t\treturn\n\t}\n")
167 } else {
168 print loc, "uses string arguments but has no error return" | "cat 1>&2"
169 printf("\t_p%d, _ = BytePtrFromString(%s)\n", goarg, goname)
171 args = sprintf("%s_p%d", args, goarg)
172 } else if (gotype ~ /^\[\](.*)/) {
173 if (ctype !~ /^\*/ || cargs[carg + 1] == "") {
174 print loc, "bad C type for slice:", gotype, ctype | "cat 1>&2"
175 status = 1
176 next
179 # Convert a slice into a pair of pointer, length.
180 # Don't try to take the address of the zeroth element of a
181 # nil slice.
182 printf("\tvar _p%d %s\n", goarg, ctype)
183 printf("\tif len(%s) > 0 {\n", goname)
184 printf("\t\t_p%d = (%s)(unsafe.Pointer(&%s[0]))\n", goarg, ctype, goname)
185 printf("\t} else {\n")
186 printf("\t\t_p%d = (%s)(unsafe.Pointer(&_zero))\n", goarg, ctype)
187 printf("\t}\n")
189 ++carg
190 if (split(cargs[carg], cparam) != 2) {
191 print loc, "bad C parameter:", cargs[carg] | "cat 1>&2"
192 status = 1
193 next
196 args = sprintf("%s_p%d, %s(len(%s))", args, goarg, cparam[2], goname)
197 } else if (gotype == "uintptr" && ctype ~ /^\*/) {
198 args = sprintf("%s(%s)(unsafe.Pointer(%s))", args, ctype, goname)
199 } else {
200 args = sprintf("%s%s(%s)", args, ctype, goname)
203 carg++
206 if (cargs[carg] != "") {
207 print loc, "too many C parameters" | "cat 1>&2"
208 status = 1
209 next
212 if (blocking) {
213 print "\tEntersyscall()"
216 printf("\t")
217 if (gofnresults != "") {
218 printf("_r := ")
220 printf("c_%s(%s)\n", cfnname, args)
222 seterr = 0
223 if (gofnresults != "") {
224 fields = split(gofnresults, goresults, ", *")
225 if (fields > 2) {
226 print loc, "too many Go results" | "cat 1>&2"
227 status = 1
228 next
230 usedr = 0
231 for (goresult = 1; goresults[goresult] != ""; goresult++) {
232 if (split(goresults[goresult], goparam) != 2) {
233 print loc, "bad result:", goresults[goresult] | "cat 1>&2"
234 status = 1
235 next
238 goname = goparam[1]
239 gotype = goparam[2]
241 if (goname == "err") {
242 print "\tvar errno Errno"
243 print "\tsetErrno := false"
244 if (cfnresult ~ /^\*/) {
245 print "\tif _r == nil {"
246 } else {
247 print "\tif _r < 0 {"
249 print "\t\terrno = GetErrno()"
250 print "\t\tsetErrno = true"
251 print "\t}"
252 seterr = 1
253 } else if (gotype == "uintptr" && cfnresult ~ /^\*/) {
254 printf("\t%s = (%s)(unsafe.Pointer(_r))\n", goname, gotype)
255 } else {
256 if (usedr) {
257 print loc, "two parameters but no errno parameter" | "cat 1>&2"
258 status = 1
259 next
261 printf("\t%s = (%s)(_r)\n", goname, gotype)
262 usedr = 1
267 if (blocking) {
268 print "\tExitsyscall()"
271 if (seterr) {
272 print "\tif setErrno {"
273 print "\t\terr = errno"
274 print "\t}"
277 if (gofnresults != "") {
278 print "\treturn"
281 print "}"
283 print ""
285 next
288 { next }
290 END {
291 if (status != 0) {
292 print "*** mksyscall.awk failed" | "cat 1>&2"
293 exit status