Skip to content

Commit

Permalink
unix: make mkasm_darwin.go usable with other operating systems
Browse files Browse the repository at this point in the history
Rename mkasm_darwin.go to mkasm.go and pass the operating system as an
argument. This will allow mkasm to be used to generate assembly for
OpenBSD.

Updates golang/go#36435

Change-Id: I42f54f5c6edc3a18a156e0e8e3c38d07ecf26d0b
Reviewed-on: https://go-review.googlesource.com/c/sys/+/421795
Run-TryBot: Joel Sing <joel@sing.id.au>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Reviewed-by: Than McIntosh <thanm@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
  • Loading branch information
4a6f656c authored and gopherbot committed Aug 8, 2022
1 parent 3d627bb commit 1c4a2a7
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 27 deletions.
2 changes: 1 addition & 1 deletion unix/darwin_amd64_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion unix/darwin_arm64_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions unix/mkall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ aix_ppc64)
darwin_amd64)
mkerrors="$mkerrors -m64"
mktypes="GOARCH=$GOARCH go tool cgo -godefs"
mkasm="go run mkasm_darwin.go"
mkasm="go run mkasm.go"
;;
darwin_arm64)
mkerrors="$mkerrors -m64"
mktypes="GOARCH=$GOARCH go tool cgo -godefs"
mkasm="go run mkasm_darwin.go"
mkasm="go run mkasm.go"
;;
dragonfly_amd64)
mkerrors="$mkerrors -m64"
Expand Down Expand Up @@ -232,5 +232,5 @@ esac
if [ -n "$mksysctl" ]; then echo "$mksysctl |gofmt >$zsysctl"; fi
if [ -n "$mksysnum" ]; then echo "$mksysnum |gofmt >zsysnum_$GOOSARCH.go"; fi
if [ -n "$mktypes" ]; then echo "$mktypes types_$GOOS.go | go run mkpost.go > ztypes_$GOOSARCH.go"; fi
if [ -n "$mkasm" ]; then echo "$mkasm $GOARCH"; fi
if [ -n "$mkasm" ]; then echo "$mkasm $GOOS $GOARCH"; fi
) | $run
64 changes: 46 additions & 18 deletions unix/mkasm_darwin.go → unix/mkasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//go:build ignore
// +build ignore

// mkasm_darwin.go generates assembly trampolines to call libSystem routines from Go.
// mkasm.go generates assembly trampolines to call library routines from Go.
// This program must be run after mksyscall.go.
package main

Expand All @@ -19,9 +19,19 @@ import (
"strings"
)

const ptrsize = 8 // Pointer size. All supported platforms are 64-bit.
func archPtrSize(arch string) int {
switch arch {
case "386", "arm":
return 4
case "amd64", "arm64", "mips64":
return 8
default:
log.Fatalf("Unknown arch %q", arch)
return 0
}
}

func generateASMFile(inFileNames []string, outFileName string, buildTags string) map[string]bool {
func generateASMFile(arch string, inFileNames []string, outFileName, buildTags string) map[string]bool {
trampolines := map[string]bool{}
var orderedTrampolines []string
for _, inFileName := range inFileNames {
Expand All @@ -43,19 +53,23 @@ func generateASMFile(inFileNames []string, outFileName string, buildTags string)
}
}

ptrSize := archPtrSize(arch)

var out bytes.Buffer
fmt.Fprintf(&out, "// go run mkasm_darwin.go %s\n", strings.Join(os.Args[1:], " "))
fmt.Fprintf(&out, "// go run mkasm.go %s\n", strings.Join(os.Args[1:], " "))
fmt.Fprintf(&out, "// Code generated by the command above; DO NOT EDIT.\n")
fmt.Fprintf(&out, "\n")
fmt.Fprintf(&out, "//go:build %s\n", buildTags)
fmt.Fprintf(&out, "// +build %s\n", buildTags)
fmt.Fprintf(&out, "\n")
if buildTags != "" {
fmt.Fprintf(&out, "//go:build %s\n", buildTags)
fmt.Fprintf(&out, "// +build %s\n", buildTags)
fmt.Fprintf(&out, "\n")
}
fmt.Fprintf(&out, "#include \"textflag.h\"\n")
for _, fn := range orderedTrampolines {
fmt.Fprintf(&out, "\nTEXT %s_trampoline<>(SB),NOSPLIT,$0-0\n", fn)
fmt.Fprintf(&out, "\tJMP\t%s(SB)\n\n", fn)
fmt.Fprintf(&out, "GLOBL\t·%s_trampoline_addr(SB), RODATA, $%d\n", fn, ptrsize)
fmt.Fprintf(&out, "DATA\t·%s_trampoline_addr(SB)/%d, $%s_trampoline<>(SB)\n", fn, ptrsize, fn)
fmt.Fprintf(&out, "GLOBL\t·%s_trampoline_addr(SB), RODATA, $%d\n", fn, ptrSize)
fmt.Fprintf(&out, "DATA\t·%s_trampoline_addr(SB)/%d, $%s_trampoline<>(SB)\n", fn, ptrSize, fn)
}

if err := ioutil.WriteFile(outFileName, out.Bytes(), 0644); err != nil {
Expand All @@ -65,7 +79,7 @@ func generateASMFile(inFileNames []string, outFileName string, buildTags string)
return trampolines
}

const darwinTestTemplate = `// go run mkasm_darwin.go %s
const darwinTestTemplate = `// go run mkasm.go %s
// Code generated by the command above; DO NOT EDIT.
//go:build darwin && go1.12
Expand Down Expand Up @@ -102,23 +116,37 @@ func writeDarwinTest(trampolines map[string]bool, fileName, arch string) {
}

func main() {
if len(os.Args) != 2 {
log.Fatalf("Usage: %s <arch>", os.Args[0])
if len(os.Args) != 3 {
log.Fatalf("Usage: %s <goos> <arch>", os.Args[0])
}
arch := os.Args[1]
goos, arch := os.Args[1], os.Args[2]

buildTags := ""
syscallFilename := fmt.Sprintf("syscall_%s.go", goos)
syscallArchFilename := fmt.Sprintf("syscall_%s_%s.go", goos, arch)
zsyscallArchFilename := fmt.Sprintf("zsyscall_%s_%s.go", goos, arch)
zsyscallASMFileName := fmt.Sprintf("zsyscall_%s_%s.s", goos, arch)

inFileNames := []string{
"syscall_darwin.go",
fmt.Sprintf("syscall_darwin_%s.go", arch),
fmt.Sprintf("zsyscall_darwin_%s.go", arch),
syscallFilename,
syscallArchFilename,
zsyscallArchFilename,
}

if goos == "darwin" {
buildTags = "go1.12"
}
trampolines := generateASMFile(arch, inFileNames, zsyscallASMFileName, buildTags)

if goos != "darwin" {
return
}
trampolines := generateASMFile(inFileNames, fmt.Sprintf("zsyscall_darwin_%s.s", arch), "go1.12")

inFileNames = []string{
"syscall_darwin.1_13.go",
fmt.Sprintf("zsyscall_darwin_%s.1_13.go", arch),
}
trampolines2 := generateASMFile(inFileNames, fmt.Sprintf("zsyscall_darwin_%s.1_13.s", arch), "go1.13")
trampolines2 := generateASMFile(arch, inFileNames, fmt.Sprintf("zsyscall_darwin_%s.1_13.s", arch), "go1.13")

// merge trampolines
for trampoline := range trampolines2 {
Expand Down
2 changes: 1 addition & 1 deletion unix/zsyscall_darwin_amd64.1_13.s
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// go run mkasm_darwin.go amd64
// go run mkasm.go darwin amd64
// Code generated by the command above; DO NOT EDIT.

//go:build go1.13
Expand Down
2 changes: 1 addition & 1 deletion unix/zsyscall_darwin_amd64.s
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// go run mkasm_darwin.go amd64
// go run mkasm.go darwin amd64
// Code generated by the command above; DO NOT EDIT.

//go:build go1.12
Expand Down
2 changes: 1 addition & 1 deletion unix/zsyscall_darwin_arm64.1_13.s
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// go run mkasm_darwin.go arm64
// go run mkasm.go darwin arm64
// Code generated by the command above; DO NOT EDIT.

//go:build go1.13
Expand Down
2 changes: 1 addition & 1 deletion unix/zsyscall_darwin_arm64.s
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// go run mkasm_darwin.go arm64
// go run mkasm.go darwin arm64
// Code generated by the command above; DO NOT EDIT.

//go:build go1.12
Expand Down

0 comments on commit 1c4a2a7

Please sign in to comment.