blob: 2e30ba5969f572c3b71e0d78b07b3abb3ad03e4d [file] [log] [blame]
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build ignore
// Parses syscalls.abigen of Fuchsia's zircon kernel and produces Go assembly
// for calling the system calls.
//
// Regenerate syscall/zx with:
//
// go run mkfuchsia.go -abigen_path=$FUCHSIA/out/default.zircon/gen/syscalls.abigen -stubs > syscalls_fuchsia.go
// go run mkfuchsia.go -abigen_path=$FUCHSIA/out/default.zircon/gen/syscalls.abigen -goarch=amd64 > syscalls_fuchsia_amd64.s
// go run mkfuchsia.go -abigen_path=$FUCHSIA/out/default.zircon/gen/syscalls.abigen -goarch=arm64 > syscalls_fuchsia_arm64.s
// gofmt -w syscalls_fuchsia.go
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"internal/fuchsia/abigen"
)
var (
stubs = flag.Bool("stubs", false, "print only Go function stubs")
goarch = flag.String("goarch", "amd64", "arch to print asm for")
abigenPath = flag.String("abigen_path", "", "path to syscalls.abigen")
compiler = flag.String("compiler", "clang", "compiler used to build zircon")
)
func main() {
flag.Parse()
switch *goarch {
case "amd64":
case "arm64":
default:
log.Fatalf("GOARCH=%s not supported", *goarch)
}
b, err := ioutil.ReadFile(*abigenPath)
if err != nil {
log.Fatal(err)
}
p := abigen.NewParser(b, *abigenPath)
defs, err := p.Parse()
if err != nil {
log.Fatal(err)
}
buf := new(bytes.Buffer)
if *stubs {
fmt.Fprint(buf, stubsHeader[1:])
for _, def := range defs {
fmt.Fprint(buf, "//go:noescape\n")
fmt.Fprint(buf, "//go:nosplit\n")
printStub(buf, def)
fmt.Fprint(buf, "\n")
}
} else {
fmt.Fprint(buf, asmHeader[1:])
for _, def := range defs {
fmt.Fprint(buf, "// ")
printStub(buf, def)
printAsm(buf, def)
fmt.Fprint(buf, "\n")
}
}
buf.WriteTo(os.Stdout)
}
const stubsHeader = `
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Zircon system calls for the Fuchsia OS.
// Generated by mkfuchsia.go, do not edit.
package zx
import "unsafe"
`
const asmHeader = `
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Zircon system calls for the Fuchsia OS.
// Generated by mkfuchsia.go, do not edit.
#include "textflag.h"
`
func printStub(buf *bytes.Buffer, def abigen.SysDef) {
fmt.Fprintf(buf, "func %s(", def.GoName())
for i, arg := range def.Args {
if arg.Type == (abigen.SysType{}) {
continue
}
if i > 0 {
fmt.Fprint(buf, ", ")
}
fmt.Fprintf(buf, "%s ", arg.Name)
fmt.Fprintf(buf, "%s", arg.Type.GoType())
}
fmt.Fprint(buf, ")")
if def.Ret != (abigen.SysType{}) {
fmt.Fprint(buf, " ")
fmt.Fprintf(buf, "%s", def.Ret.GoType())
}
fmt.Fprint(buf, "\n")
}
func printAsm(buf *bytes.Buffer, def abigen.SysDef) {
fmt.Fprintf(buf, "TEXT ·%s(SB),NOSPLIT,$0\n", def.GoName())
fmt.Fprintf(buf, "\tJMP runtime·vdsoCall_zx_%s(SB)\n", def.Name)
}