1// 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//go:build ignore
6
7// mkasm.go generates assembly trampolines to call library routines from Go.
8// This program must be run after mksyscall.pl.
9package main
10
11import (
12	"bytes"
13	"fmt"
14	"log"
15	"os"
16	"strings"
17)
18
19func main() {
20	if len(os.Args) != 3 {
21		log.Fatalf("Usage: %s <goos> <arch>", os.Args[0])
22	}
23	goos, arch := os.Args[1], os.Args[2]
24
25	syscallFilename := fmt.Sprintf("syscall_%s.go", goos)
26	syscallArchFilename := fmt.Sprintf("syscall_%s_%s.go", goos, arch)
27
28	in1, err := os.ReadFile(syscallFilename)
29	if err != nil {
30		log.Fatalf("can't open syscall file: %s", err)
31	}
32	in2, err := os.ReadFile(syscallArchFilename)
33	if err != nil {
34		log.Fatalf("can't open syscall file: %s", err)
35	}
36	in3, err := os.ReadFile("z" + syscallArchFilename)
37	if err != nil {
38		log.Fatalf("can't open syscall file: %s", err)
39	}
40	in := string(in1) + string(in2) + string(in3)
41
42	trampolines := map[string]bool{}
43
44	var out bytes.Buffer
45
46	fmt.Fprintf(&out, "// go run mkasm.go %s\n", strings.Join(os.Args[1:], " "))
47	fmt.Fprintf(&out, "// Code generated by the command above; DO NOT EDIT.\n")
48	fmt.Fprintf(&out, "#include \"textflag.h\"\n")
49	for _, line := range strings.Split(in, "\n") {
50		if !strings.HasPrefix(line, "func ") || !strings.HasSuffix(line, "_trampoline()") {
51			continue
52		}
53		fn := line[5 : len(line)-13]
54		if !trampolines[fn] {
55			trampolines[fn] = true
56			fmt.Fprintf(&out, "TEXT ·%s_trampoline(SB),NOSPLIT,$0-0\n", fn)
57			if goos == "openbsd" && arch == "ppc64" {
58				fmt.Fprintf(&out, "\tCALL\t%s(SB)\n", fn)
59				fmt.Fprintf(&out, "\tRET\n")
60			} else {
61				fmt.Fprintf(&out, "\tJMP\t%s(SB)\n", fn)
62			}
63		}
64	}
65	err = os.WriteFile(fmt.Sprintf("zsyscall_%s_%s.s", goos, arch), out.Bytes(), 0644)
66	if err != nil {
67		log.Fatalf("can't write syscall file: %s", err)
68	}
69}
70