github.com/AESNooper/go/src@v0.0.0-20220218095104-b56a4ab1bbbb/runtime/wincallback.go (about)

     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  //go:build ignore
     6  
     7  // Generate Windows callback assembly file.
     8  
     9  package main
    10  
    11  import (
    12  	"bytes"
    13  	"fmt"
    14  	"os"
    15  )
    16  
    17  const maxCallback = 2000
    18  
    19  func genasm386Amd64() {
    20  	var buf bytes.Buffer
    21  
    22  	buf.WriteString(`// Code generated by wincallback.go using 'go generate'. DO NOT EDIT.
    23  
    24  //go:build 386 || amd64
    25  
    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  
    34  TEXT 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 := os.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  
    48  func 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  
    62  TEXT 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 := os.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  
    76  func genasmArm64() {
    77  	var buf bytes.Buffer
    78  
    79  	buf.WriteString(`// Code generated by wincallback.go using 'go generate'. DO NOT EDIT.
    80  
    81  // External code calls into callbackasm at an offset corresponding
    82  // to the callback index. Callbackasm is a table of MOV and B instructions.
    83  // The MOV instruction loads R12 with the callback index, and the
    84  // B instruction branches to callbackasm1.
    85  // callbackasm1 takes the callback index from R12 and
    86  // indexes into an array that stores information about each callback.
    87  // It then calls the Go implementation for that callback.
    88  #include "textflag.h"
    89  
    90  TEXT runtime·callbackasm(SB),NOSPLIT|NOFRAME,$0
    91  `)
    92  	for i := 0; i < maxCallback; i++ {
    93  		buf.WriteString(fmt.Sprintf("\tMOVD\t$%d, R12\n", i))
    94  		buf.WriteString("\tB\truntime·callbackasm1(SB)\n")
    95  	}
    96  
    97  	err := os.WriteFile("zcallback_windows_arm64.s", buf.Bytes(), 0666)
    98  	if err != nil {
    99  		fmt.Fprintf(os.Stderr, "wincallback: %s\n", err)
   100  		os.Exit(2)
   101  	}
   102  }
   103  
   104  func gengo() {
   105  	var buf bytes.Buffer
   106  
   107  	buf.WriteString(fmt.Sprintf(`// Code generated by wincallback.go using 'go generate'. DO NOT EDIT.
   108  
   109  package runtime
   110  
   111  const cb_max = %d // maximum number of windows callbacks allowed
   112  `, maxCallback))
   113  	err := os.WriteFile("zcallback_windows.go", buf.Bytes(), 0666)
   114  	if err != nil {
   115  		fmt.Fprintf(os.Stderr, "wincallback: %s\n", err)
   116  		os.Exit(2)
   117  	}
   118  }
   119  
   120  func main() {
   121  	genasm386Amd64()
   122  	genasmArm()
   123  	genasmArm64()
   124  	gengo()
   125  }