1 // Copyright 2019 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.
8 This program reads a file containing function prototypes
9 (like syscall_aix.go) and generates system call bodies.
10 The prototypes are marked by lines beginning with "//sys"
11 and read like func declarations if //sys is replaced by func, but:
12 * The parameter lists must give a name for each argument.
13 This includes return parameters.
14 * The parameter lists must give a type for each argument:
15 the (x, y, z int) shorthand is not allowed.
16 * If the return parameter is an error number, it must be named err.
17 * If go func name needs to be different than its libc name,
18 * or the function is not in libc, name could be specified
19 * at the end, after "=" sign, like
20 //sys getsockopt(s int, level int, name int, val uintptr, vallen *_Socklen) (err error) = libsocket.getsockopt
23 This program will generate three files and handle both gc and gccgo implementation:
24 - zsyscall_aix_ppc64.go: the common part of each implementation (error handler, pointer creation)
25 - zsyscall_aix_ppc64_gc.go: gc part with //go_cgo_import_dynamic and a call to syscall6
26 - zsyscall_aix_ppc64_gccgo.go: gccgo part with C function and conversion to C type.
28 The generated code looks like this
31 func asyscall(...) (n int, err error) {
33 r1, e1 := callasyscall(...)
39 zsyscall_aix_ppc64_gc.go
40 //go:cgo_import_dynamic libc_asyscall asyscall "libc.a/shr_64.o"
41 //go:linkname libc_asyscall libc_asyscall
42 var asyscall syscallFunc
44 func callasyscall(...) (r1 uintptr, e1 Errno) {
45 r1, _, e1 = syscall6(uintptr(unsafe.Pointer(&libc_asyscall)), "nb_args", ... )
49 zsyscall_aix_ppc64_ggcgo.go
55 func callasyscall(...) (r1 uintptr, e1 Errno) {
56 r1 = uintptr(C.asyscall(...))
57 e1 = syscall.GetErrno()
75 b32 = flag.Bool("b32", false, "32bit big-endian")
76 l32 = flag.Bool("l32", false, "32bit little-endian")
77 aix = flag.Bool("aix", false, "aix")
78 tags = flag.String("tags", "", "build tags")
81 // cmdLine returns this programs's commandline arguments
82 func cmdLine() string {
83 return "go run mksyscall_aix_ppc64.go " + strings.Join(os.Args[1:], " ")
86 // buildTags returns build tags
87 func buildTags() string {
91 // Param is function parameter
97 // usage prints the program usage
99 fmt.Fprintf(os.Stderr, "usage: go run mksyscall_aix_ppc64.go [-b32 | -l32] [-tags x,y] [file ...]\n")
103 // parseParamList parses parameter list and returns a slice of parameters
104 func parseParamList(list string) []string {
105 list = strings.TrimSpace(list)
109 return regexp.MustCompile(`\s*,\s*`).Split(list, -1)
112 // parseParam splits a parameter into name and type
113 func parseParam(p string) Param {
114 ps := regexp.MustCompile(`^(\S*) (\S*)$`).FindStringSubmatch(p)
116 fmt.Fprintf(os.Stderr, "malformed parameter: %s\n", p)
119 return Param{ps[1], ps[2]}
125 if len(flag.Args()) <= 0 {
126 fmt.Fprintf(os.Stderr, "no files to parse provided\n")
132 endianness = "big-endian"
134 endianness = "little-endian"
140 cExtern := "/*\n#include <stdint.h>\n"
148 for _, path := range flag.Args() {
149 file, err := os.Open(path)
151 fmt.Fprintf(os.Stderr, err.Error())
154 s := bufio.NewScanner(file)
157 t = strings.TrimSpace(t)
158 t = regexp.MustCompile(`\s+`).ReplaceAllString(t, ` `)
159 if p := regexp.MustCompile(`^package (\S+)$`).FindStringSubmatch(t); p != nil && pack == "" {
162 nonblock := regexp.MustCompile(`^\/\/sysnb `).FindStringSubmatch(t)
163 if regexp.MustCompile(`^\/\/sys `).FindStringSubmatch(t) == nil && nonblock == nil {
167 // Line must be of the form
168 // func Open(path string, mode int, perm int) (fd int, err error)
169 // Split into name, in params, out params.
170 f := regexp.MustCompile(`^\/\/sys(nb)? (\w+)\(([^()]*)\)\s*(?:\(([^()]+)\))?\s*(?:=\s*(?:(\w*)\.)?(\w*))?$`).FindStringSubmatch(t)
172 fmt.Fprintf(os.Stderr, "%s:%s\nmalformed //sys declaration\n", path, t)
175 funct, inps, outps, modname, sysname := f[2], f[3], f[4], f[5], f[6]
177 // Split argument lists on comma.
178 in := parseParamList(inps)
179 out := parseParamList(outps)
181 inps = strings.Join(in, ", ")
182 outps = strings.Join(out, ", ")
189 if funct == "readlen" || funct == "writelen" || funct == "FcntlInt" || funct == "FcntlFlock" {
190 // This function call another syscall which is already implemented.
191 // Therefore, the gc and gccgo part must not be generated.
195 // Try in vain to keep people from editing this file.
196 // The theory is that they jump into the middle of the file
197 // without reading the header.
199 textcommon += "// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT\n\n"
201 textgccgo += "// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT\n\n"
202 textgc += "// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT\n\n"
205 // Check if value return, err return available
208 for _, param := range out {
209 p := parseParam(param)
210 if p.Type == "error" {
217 sysname = regexp.MustCompile(`([a-z])([A-Z])`).ReplaceAllString(sysname, `${1}_$2`)
218 sysname = strings.ToLower(sysname) // All libc functions are lowercase.
220 // GCCGO Prototype return type
222 if rettype == "unsafe.Pointer" {
223 cRettype = "uintptr_t"
224 } else if rettype == "uintptr" {
225 cRettype = "uintptr_t"
226 } else if regexp.MustCompile(`^_`).FindStringSubmatch(rettype) != nil {
227 cRettype = "uintptr_t"
228 } else if rettype == "int" {
230 } else if rettype == "int32" {
232 } else if rettype == "int64" {
233 cRettype = "long long"
234 } else if rettype == "uint32" {
235 cRettype = "unsigned int"
236 } else if rettype == "uint64" {
237 cRettype = "unsigned long long"
241 if sysname == "exit" {
245 // GCCGO Prototype arguments type
247 for i, param := range in {
248 p := parseParam(param)
249 if regexp.MustCompile(`^\*`).FindStringSubmatch(p.Type) != nil {
250 cIn = append(cIn, "uintptr_t")
251 } else if p.Type == "string" {
252 cIn = append(cIn, "uintptr_t")
253 } else if regexp.MustCompile(`^\[\](.*)`).FindStringSubmatch(p.Type) != nil {
254 cIn = append(cIn, "uintptr_t", "size_t")
255 } else if p.Type == "unsafe.Pointer" {
256 cIn = append(cIn, "uintptr_t")
257 } else if p.Type == "uintptr" {
258 cIn = append(cIn, "uintptr_t")
259 } else if regexp.MustCompile(`^_`).FindStringSubmatch(p.Type) != nil {
260 cIn = append(cIn, "uintptr_t")
261 } else if p.Type == "int" {
262 if (i == 0 || i == 2) && funct == "fcntl" {
263 // These fcntl arguments needs to be uintptr to be able to call FcntlInt and FcntlFlock
264 cIn = append(cIn, "uintptr_t")
266 cIn = append(cIn, "int")
269 } else if p.Type == "int32" {
270 cIn = append(cIn, "int")
271 } else if p.Type == "int64" {
272 cIn = append(cIn, "long long")
273 } else if p.Type == "uint32" {
274 cIn = append(cIn, "unsigned int")
275 } else if p.Type == "uint64" {
276 cIn = append(cIn, "unsigned long long")
278 cIn = append(cIn, "int")
283 // GCCGO Prototype Generation
284 // Imports of system calls from libc
285 if sysname == "select" {
286 // select is a keyword of Go. Its name is
287 // changed to c_select.
288 cExtern += "#define c_select select\n"
290 cExtern += fmt.Sprintf("%s %s", cRettype, sysname)
291 cIn := strings.Join(cIn, ", ")
292 cExtern += fmt.Sprintf("(%s);\n", cIn)
296 modname = "libc.a/shr_64.o"
298 fmt.Fprintf(os.Stderr, "%s: only syscall using libc are available\n", funct)
301 sysvarname := fmt.Sprintf("libc_%s", sysname)
304 // GC Runtime import of function to allow cross-platform builds.
305 dynimports += fmt.Sprintf("//go:cgo_import_dynamic %s %s \"%s\"\n", sysvarname, sysname, modname)
306 // GC Link symbol to proc address variable.
307 linknames += fmt.Sprintf("//go:linkname %s %s\n", sysvarname, sysvarname)
308 // GC Library proc address variable.
309 vars = append(vars, sysvarname)
312 strconvfunc := "BytePtrFromString"
313 strconvtype := "*byte"
315 // Go function header.
317 outps = fmt.Sprintf(" (%s)", outps)
319 if textcommon != "" {
323 textcommon += fmt.Sprintf("func %s(%s)%s {\n", funct, strings.Join(in, ", "), outps)
325 // Prepare arguments tocall.
326 var argscommon []string // Arguments in the common part
327 var argscall []string // Arguments for call prototype
328 var argsgc []string // Arguments for gc call (with syscall6)
329 var argsgccgo []string // Arguments for gccgo call (with C.name_of_syscall)
332 for _, param := range in {
333 p := parseParam(param)
334 if regexp.MustCompile(`^\*`).FindStringSubmatch(p.Type) != nil {
335 argscommon = append(argscommon, fmt.Sprintf("uintptr(unsafe.Pointer(%s))", p.Name))
336 argscall = append(argscall, fmt.Sprintf("%s uintptr", p.Name))
337 argsgc = append(argsgc, p.Name)
338 argsgccgo = append(argsgccgo, fmt.Sprintf("C.uintptr_t(%s)", p.Name))
339 } else if p.Type == "string" && errvar != "" {
340 textcommon += fmt.Sprintf("\tvar _p%d %s\n", n, strconvtype)
341 textcommon += fmt.Sprintf("\t_p%d, %s = %s(%s)\n", n, errvar, strconvfunc, p.Name)
342 textcommon += fmt.Sprintf("\tif %s != nil {\n\t\treturn\n\t}\n", errvar)
344 argscommon = append(argscommon, fmt.Sprintf("uintptr(unsafe.Pointer(_p%d))", n))
345 argscall = append(argscall, fmt.Sprintf("_p%d uintptr ", n))
346 argsgc = append(argsgc, fmt.Sprintf("_p%d", n))
347 argsgccgo = append(argsgccgo, fmt.Sprintf("C.uintptr_t(_p%d)", n))
349 } else if p.Type == "string" {
350 fmt.Fprintf(os.Stderr, path+":"+funct+" uses string arguments, but has no error return\n")
351 textcommon += fmt.Sprintf("\tvar _p%d %s\n", n, strconvtype)
352 textcommon += fmt.Sprintf("\t_p%d, %s = %s(%s)\n", n, errvar, strconvfunc, p.Name)
353 textcommon += fmt.Sprintf("\tif %s != nil {\n\t\treturn\n\t}\n", errvar)
355 argscommon = append(argscommon, fmt.Sprintf("uintptr(unsafe.Pointer(_p%d))", n))
356 argscall = append(argscall, fmt.Sprintf("_p%d uintptr", n))
357 argsgc = append(argsgc, fmt.Sprintf("_p%d", n))
358 argsgccgo = append(argsgccgo, fmt.Sprintf("C.uintptr_t(_p%d)", n))
360 } else if m := regexp.MustCompile(`^\[\](.*)`).FindStringSubmatch(p.Type); m != nil {
361 // Convert slice into pointer, length.
362 // Have to be careful not to take address of &a[0] if len == 0:
363 // pass nil in that case.
364 textcommon += fmt.Sprintf("\tvar _p%d *%s\n", n, m[1])
365 textcommon += fmt.Sprintf("\tif len(%s) > 0 {\n\t\t_p%d = &%s[0]\n\t}\n", p.Name, n, p.Name)
366 argscommon = append(argscommon, fmt.Sprintf("uintptr(unsafe.Pointer(_p%d))", n), fmt.Sprintf("len(%s)", p.Name))
367 argscall = append(argscall, fmt.Sprintf("_p%d uintptr", n), fmt.Sprintf("_lenp%d int", n))
368 argsgc = append(argsgc, fmt.Sprintf("_p%d", n), fmt.Sprintf("uintptr(_lenp%d)", n))
369 argsgccgo = append(argsgccgo, fmt.Sprintf("C.uintptr_t(_p%d)", n), fmt.Sprintf("C.size_t(_lenp%d)", n))
371 } else if p.Type == "int64" && endianness != "" {
372 fmt.Fprintf(os.Stderr, path+":"+funct+" uses int64 with 32 bits mode. Case not yet implemented\n")
373 } else if p.Type == "bool" {
374 fmt.Fprintf(os.Stderr, path+":"+funct+" uses bool. Case not yet implemented\n")
375 } else if regexp.MustCompile(`^_`).FindStringSubmatch(p.Type) != nil || p.Type == "unsafe.Pointer" {
376 argscommon = append(argscommon, fmt.Sprintf("uintptr(%s)", p.Name))
377 argscall = append(argscall, fmt.Sprintf("%s uintptr", p.Name))
378 argsgc = append(argsgc, p.Name)
379 argsgccgo = append(argsgccgo, fmt.Sprintf("C.uintptr_t(%s)", p.Name))
380 } else if p.Type == "int" {
381 if (argN == 0 || argN == 2) && ((funct == "fcntl") || (funct == "FcntlInt") || (funct == "FcntlFlock")) {
382 // These fcntl arguments need to be uintptr to be able to call FcntlInt and FcntlFlock
383 argscommon = append(argscommon, fmt.Sprintf("uintptr(%s)", p.Name))
384 argscall = append(argscall, fmt.Sprintf("%s uintptr", p.Name))
385 argsgc = append(argsgc, p.Name)
386 argsgccgo = append(argsgccgo, fmt.Sprintf("C.uintptr_t(%s)", p.Name))
389 argscommon = append(argscommon, p.Name)
390 argscall = append(argscall, fmt.Sprintf("%s int", p.Name))
391 argsgc = append(argsgc, fmt.Sprintf("uintptr(%s)", p.Name))
392 argsgccgo = append(argsgccgo, fmt.Sprintf("C.int(%s)", p.Name))
394 } else if p.Type == "int32" {
395 argscommon = append(argscommon, p.Name)
396 argscall = append(argscall, fmt.Sprintf("%s int32", p.Name))
397 argsgc = append(argsgc, fmt.Sprintf("uintptr(%s)", p.Name))
398 argsgccgo = append(argsgccgo, fmt.Sprintf("C.int(%s)", p.Name))
399 } else if p.Type == "int64" {
400 argscommon = append(argscommon, p.Name)
401 argscall = append(argscall, fmt.Sprintf("%s int64", p.Name))
402 argsgc = append(argsgc, fmt.Sprintf("uintptr(%s)", p.Name))
403 argsgccgo = append(argsgccgo, fmt.Sprintf("C.longlong(%s)", p.Name))
404 } else if p.Type == "uint32" {
405 argscommon = append(argscommon, p.Name)
406 argscall = append(argscall, fmt.Sprintf("%s uint32", p.Name))
407 argsgc = append(argsgc, fmt.Sprintf("uintptr(%s)", p.Name))
408 argsgccgo = append(argsgccgo, fmt.Sprintf("C.uint(%s)", p.Name))
409 } else if p.Type == "uint64" {
410 argscommon = append(argscommon, p.Name)
411 argscall = append(argscall, fmt.Sprintf("%s uint64", p.Name))
412 argsgc = append(argsgc, fmt.Sprintf("uintptr(%s)", p.Name))
413 argsgccgo = append(argsgccgo, fmt.Sprintf("C.ulonglong(%s)", p.Name))
414 } else if p.Type == "uintptr" {
415 argscommon = append(argscommon, p.Name)
416 argscall = append(argscall, fmt.Sprintf("%s uintptr", p.Name))
417 argsgc = append(argsgc, p.Name)
418 argsgccgo = append(argsgccgo, fmt.Sprintf("C.uintptr_t(%s)", p.Name))
420 argscommon = append(argscommon, fmt.Sprintf("int(%s)", p.Name))
421 argscall = append(argscall, fmt.Sprintf("%s int", p.Name))
422 argsgc = append(argsgc, fmt.Sprintf("uintptr(%s)", p.Name))
423 argsgccgo = append(argsgccgo, fmt.Sprintf("C.int(%s)", p.Name))
429 // COMMON function generation
430 argscommonlist := strings.Join(argscommon, ", ")
431 callcommon := fmt.Sprintf("call%s(%s)", sysname, argscommonlist)
432 ret := []string{"_", "_"}
435 for i := 0; i < len(out); i++ {
436 p := parseParam(out[i])
446 if p.Type == "bool" {
447 reg = fmt.Sprintf("%s != 0", reg)
450 body += fmt.Sprintf("\t%s = %s(%s)\n", p.Name, p.Type, reg)
453 if ret[0] == "_" && ret[1] == "_" {
454 textcommon += fmt.Sprintf("\t%s\n", callcommon)
456 textcommon += fmt.Sprintf("\t%s, %s := %s\n", ret[0], ret[1], callcommon)
461 textcommon += "\tif e1 != 0 {\n"
462 textcommon += "\t\terr = errnoErr(e1)\n"
463 textcommon += "\t}\n"
465 textcommon += "\treturn\n"
473 callProto := fmt.Sprintf("func call%s(%s) (r1 uintptr, e1 Errno) {\n", sysname, strings.Join(argscall, ", "))
475 // GC function generation
481 if len(argsgc) <= 6 {
482 for len(argsgc) < 6 {
483 argsgc = append(argsgc, "0")
486 fmt.Fprintf(os.Stderr, "%s: too many arguments to system call", funct)
489 argsgclist := strings.Join(argsgc, ", ")
490 callgc := fmt.Sprintf("%s(uintptr(unsafe.Pointer(&%s)), %d, %s)", asm, sysvarname, nargs, argsgclist)
493 textgc += fmt.Sprintf("\tr1, _, e1 = %s\n", callgc)
494 textgc += "\treturn\n}\n"
496 // GCCGO function generation
497 argsgccgolist := strings.Join(argsgccgo, ", ")
499 if sysname == "select" {
500 // select is a keyword of Go. Its name is
501 // changed to c_select.
502 callgccgo = fmt.Sprintf("C.c_%s(%s)", sysname, argsgccgolist)
504 callgccgo = fmt.Sprintf("C.%s(%s)", sysname, argsgccgolist)
506 textgccgo += callProto
507 textgccgo += fmt.Sprintf("\tr1 = uintptr(%s)\n", callgccgo)
508 textgccgo += "\te1 = syscall.GetErrno()\n"
509 textgccgo += "\treturn\n}\n"
511 if err := s.Err(); err != nil {
512 fmt.Fprintf(os.Stderr, err.Error())
519 imp = "import \"golang.org/x/sys/unix\"\n"
523 // Print zsyscall_aix_ppc64.go
524 err := ioutil.WriteFile("zsyscall_aix_ppc64.go",
525 []byte(fmt.Sprintf(srcTemplate1, cmdLine(), buildTags(), pack, imp, textcommon)),
528 fmt.Fprintf(os.Stderr, err.Error())
532 // Print zsyscall_aix_ppc64_gc.go
533 vardecls := "\t" + strings.Join(vars, ",\n\t")
534 vardecls += " syscallFunc"
535 err = ioutil.WriteFile("zsyscall_aix_ppc64_gc.go",
536 []byte(fmt.Sprintf(srcTemplate2, cmdLine(), buildTags(), pack, imp, dynimports, linknames, vardecls, textgc)),
539 fmt.Fprintf(os.Stderr, err.Error())
543 // Print zsyscall_aix_ppc64_gccgo.go
544 err = ioutil.WriteFile("zsyscall_aix_ppc64_gccgo.go",
545 []byte(fmt.Sprintf(srcTemplate3, cmdLine(), buildTags(), pack, cExtern, imp, textgccgo)),
548 fmt.Fprintf(os.Stderr, err.Error())
553 const srcTemplate1 = `// %s
554 // Code generated by the command above; see README.md. DO NOT EDIT.
569 const srcTemplate2 = `// %s
570 // Code generated by the command above; see README.md. DO NOT EDIT.
583 type syscallFunc uintptr
589 // Implemented in runtime/syscall_aix.go.
590 func rawSyscall6(trap, nargs, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
591 func syscall6(trap, nargs, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err Errno)
595 const srcTemplate3 = `// %s
596 // Code generated by the command above; see README.md. DO NOT EDIT.