1// Copyright 2014 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// Generate Windows callback assembly file.
8
9package main
10
11import (
12	"bytes"
13	"fmt"
14	"io/ioutil"
15	"os"
16)
17
18const maxCallback = 2000
19
20func genasm386Amd64() {
21	var buf bytes.Buffer
22
23	buf.WriteString(`// Code generated by wincallback.go using 'go generate'. DO NOT EDIT.
24
25// +build 386 amd64
26// runtime·callbackasm is called by external code to
27// execute Go implemented callback function. It is not
28// called from the start, instead runtime·compilecallback
29// always returns address into runtime·callbackasm offset
30// appropriately so different callbacks start with different
31// CALL instruction in runtime·callbackasm. This determines
32// which Go callback function is executed later on.
33
34TEXT runtime·callbackasm(SB),7,$0
35`)
36	for i := 0; i < maxCallback; i++ {
37		buf.WriteString("\tCALL\truntime·callbackasm1(SB)\n")
38	}
39
40	filename := fmt.Sprintf("zcallback_windows.s")
41	err := ioutil.WriteFile(filename, buf.Bytes(), 0666)
42	if err != nil {
43		fmt.Fprintf(os.Stderr, "wincallback: %s\n", err)
44		os.Exit(2)
45	}
46}
47
48func genasmArm() {
49	var buf bytes.Buffer
50
51	buf.WriteString(`// Code generated by wincallback.go using 'go generate'. DO NOT EDIT.
52
53// External code calls into callbackasm at an offset corresponding
54// to the callback index. Callbackasm is a table of MOV and B instructions.
55// The MOV instruction loads R12 with the callback index, and the
56// B instruction branches to callbackasm1.
57// callbackasm1 takes the callback index from R12 and
58// indexes into an array that stores information about each callback.
59// It then calls the Go implementation for that callback.
60#include "textflag.h"
61
62TEXT runtime·callbackasm(SB),NOSPLIT|NOFRAME,$0
63`)
64	for i := 0; i < maxCallback; i++ {
65		buf.WriteString(fmt.Sprintf("\tMOVW\t$%d, R12\n", i))
66		buf.WriteString("\tB\truntime·callbackasm1(SB)\n")
67	}
68
69	err := ioutil.WriteFile("zcallback_windows_arm.s", buf.Bytes(), 0666)
70	if err != nil {
71		fmt.Fprintf(os.Stderr, "wincallback: %s\n", err)
72		os.Exit(2)
73	}
74}
75
76func gengo() {
77	var buf bytes.Buffer
78
79	buf.WriteString(fmt.Sprintf(`// Code generated by wincallback.go using 'go generate'. DO NOT EDIT.
80
81package runtime
82
83const cb_max = %d // maximum number of windows callbacks allowed
84`, maxCallback))
85	err := ioutil.WriteFile("zcallback_windows.go", buf.Bytes(), 0666)
86	if err != nil {
87		fmt.Fprintf(os.Stderr, "wincallback: %s\n", err)
88		os.Exit(2)
89	}
90}
91
92func main() {
93	genasm386Amd64()
94	genasmArm()
95	gengo()
96}
97