blob: 6f7bb6edfb9dbcdfb45b0f015f6e43b4ea707efb [file] [log] [blame]
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +00001// Copyright 2018 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.
4
5// +build ignore
6
7// mkasm_darwin.go generates assembly trampolines to call libSystem routines from Go.
8//This program must be run after mksyscall.go.
9package main
10
11import (
12 "bytes"
13 "fmt"
14 "io/ioutil"
15 "log"
16 "os"
17 "strings"
18)
19
20func writeASMFile(in string, fileName string, buildTags string) {
21 trampolines := map[string]bool{}
22
23 var out bytes.Buffer
24
25 fmt.Fprintf(&out, "// go run mkasm_darwin.go %s\n", strings.Join(os.Args[1:], " "))
26 fmt.Fprintf(&out, "// Code generated by the command above; DO NOT EDIT.\n")
27 fmt.Fprintf(&out, "\n")
28 fmt.Fprintf(&out, "// +build %s\n", buildTags)
29 fmt.Fprintf(&out, "\n")
30 fmt.Fprintf(&out, "#include \"textflag.h\"\n")
31 for _, line := range strings.Split(in, "\n") {
32 if !strings.HasPrefix(line, "func ") || !strings.HasSuffix(line, "_trampoline()") {
33 continue
34 }
35 fn := line[5 : len(line)-13]
36 if !trampolines[fn] {
37 trampolines[fn] = true
38 fmt.Fprintf(&out, "TEXT ยท%s_trampoline(SB),NOSPLIT,$0-0\n", fn)
39 fmt.Fprintf(&out, "\tJMP\t%s(SB)\n", fn)
40 }
41 }
42 err := ioutil.WriteFile(fileName, out.Bytes(), 0644)
43 if err != nil {
44 log.Fatalf("can't write %s: %s", fileName, err)
45 }
46}
47
48func main() {
49 in1, err := ioutil.ReadFile("syscall_darwin.go")
50 if err != nil {
51 log.Fatalf("can't open syscall_darwin.go: %s", err)
52 }
53 arch := os.Args[1]
54 in2, err := ioutil.ReadFile(fmt.Sprintf("syscall_darwin_%s.go", arch))
55 if err != nil {
56 log.Fatalf("can't open syscall_darwin_%s.go: %s", arch, err)
57 }
58 in3, err := ioutil.ReadFile(fmt.Sprintf("zsyscall_darwin_%s.go", arch))
59 if err != nil {
60 log.Fatalf("can't open zsyscall_darwin_%s.go: %s", arch, err)
61 }
62 in := string(in1) + string(in2) + string(in3)
63
64 writeASMFile(in, fmt.Sprintf("zsyscall_darwin_%s.s", arch), "go1.12")
65
66 in1, err = ioutil.ReadFile("syscall_darwin.1_13.go")
67 if err != nil {
68 log.Fatalf("can't open syscall_darwin.1_13.go: %s", err)
69 }
70 in2, err = ioutil.ReadFile(fmt.Sprintf("zsyscall_darwin_%s.1_13.go", arch))
71 if err != nil {
72 log.Fatalf("can't open zsyscall_darwin_%s.1_13.go: %s", arch, err)
73 }
74
75 in = string(in1) + string(in2)
76
77 writeASMFile(in, fmt.Sprintf("zsyscall_darwin_%s.1_13.s", arch), "go1.13")
78}