github.com/4ad/go@v0.0.0-20161219182952-69a12818b605/src/runtime/mkduff.go (about)

     1  // Copyright 2015 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  // runtime·duffzero is a Duff's device for zeroing memory.
     8  // The compiler jumps to computed addresses within
     9  // the routine to zero chunks of memory.
    10  // Do not change duffzero without also
    11  // changing clearfat in cmd/?g/ggen.go.
    12  
    13  // runtime·duffcopy is a Duff's device for copying memory.
    14  // The compiler jumps to computed addresses within
    15  // the routine to copy chunks of memory.
    16  // Source and destination must not overlap.
    17  // Do not change duffcopy without also
    18  // changing blockcopy in cmd/?g/cgen.go.
    19  
    20  // See the zero* and copy* generators below
    21  // for architecture-specific comments.
    22  
    23  // mkduff generates duff_*.s.
    24  package main
    25  
    26  import (
    27  	"bytes"
    28  	"fmt"
    29  	"io"
    30  	"io/ioutil"
    31  	"log"
    32  )
    33  
    34  func main() {
    35  	gen("amd64", notags, zeroAMD64, copyAMD64)
    36  	gen("386", notags, zero386, copy386)
    37  	gen("arm", notags, zeroARM, copyARM)
    38  	gen("arm64", notags, zeroARM64, copyARM64)
    39  	gen("ppc64x", tagsPPC64x, zeroPPC64x, copyPPC64x)
    40  	gen("mips64x", tagsMIPS64x, zeroMIPS64x, copyMIPS64x)
    41  	gen("sparc64", notags, zeroSPARC64, copySPARC64)
    42  }
    43  
    44  func gen(arch string, tags, zero, copy func(io.Writer)) {
    45  	var buf bytes.Buffer
    46  
    47  	fmt.Fprintln(&buf, "// AUTO-GENERATED by mkduff.go")
    48  	fmt.Fprintln(&buf, "// Run go generate from src/runtime to update.")
    49  	fmt.Fprintln(&buf, "// See mkduff.go for comments.")
    50  	tags(&buf)
    51  	fmt.Fprintln(&buf, "#include \"textflag.h\"")
    52  	fmt.Fprintln(&buf)
    53  	zero(&buf)
    54  	fmt.Fprintln(&buf)
    55  	copy(&buf)
    56  
    57  	if err := ioutil.WriteFile("duff_"+arch+".s", buf.Bytes(), 0644); err != nil {
    58  		log.Fatalln(err)
    59  	}
    60  }
    61  
    62  func notags(w io.Writer) { fmt.Fprintln(w) }
    63  
    64  func zeroAMD64(w io.Writer) {
    65  	// X0: zero
    66  	// DI: ptr to memory to be zeroed
    67  	// DI is updated as a side effect.
    68  	fmt.Fprintln(w, "TEXT runtime·duffzero(SB), NOSPLIT, $0-0")
    69  	for i := 0; i < 16; i++ {
    70  		fmt.Fprintln(w, "\tMOVUPS\tX0,(DI)")
    71  		fmt.Fprintln(w, "\tMOVUPS\tX0,16(DI)")
    72  		fmt.Fprintln(w, "\tMOVUPS\tX0,32(DI)")
    73  		fmt.Fprintln(w, "\tMOVUPS\tX0,48(DI)")
    74  		fmt.Fprintln(w, "\tADDQ\t$64,DI")
    75  		fmt.Fprintln(w)
    76  	}
    77  	fmt.Fprintln(w, "\tRET")
    78  }
    79  
    80  func copyAMD64(w io.Writer) {
    81  	// SI: ptr to source memory
    82  	// DI: ptr to destination memory
    83  	// SI and DI are updated as a side effect.
    84  	//
    85  	// This is equivalent to a sequence of MOVSQ but
    86  	// for some reason that is 3.5x slower than this code.
    87  	// The STOSQ in duffzero seem fine, though.
    88  	fmt.Fprintln(w, "TEXT runtime·duffcopy(SB), NOSPLIT, $0-0")
    89  	for i := 0; i < 64; i++ {
    90  		fmt.Fprintln(w, "\tMOVUPS\t(SI), X0")
    91  		fmt.Fprintln(w, "\tADDQ\t$16, SI")
    92  		fmt.Fprintln(w, "\tMOVUPS\tX0, (DI)")
    93  		fmt.Fprintln(w, "\tADDQ\t$16, DI")
    94  		fmt.Fprintln(w)
    95  	}
    96  	fmt.Fprintln(w, "\tRET")
    97  }
    98  
    99  func zero386(w io.Writer) {
   100  	// AX: zero
   101  	// DI: ptr to memory to be zeroed
   102  	// DI is updated as a side effect.
   103  	fmt.Fprintln(w, "TEXT runtime·duffzero(SB), NOSPLIT, $0-0")
   104  	for i := 0; i < 128; i++ {
   105  		fmt.Fprintln(w, "\tSTOSL")
   106  	}
   107  	fmt.Fprintln(w, "\tRET")
   108  }
   109  
   110  func copy386(w io.Writer) {
   111  	// SI: ptr to source memory
   112  	// DI: ptr to destination memory
   113  	// SI and DI are updated as a side effect.
   114  	//
   115  	// This is equivalent to a sequence of MOVSL but
   116  	// for some reason MOVSL is really slow.
   117  	fmt.Fprintln(w, "TEXT runtime·duffcopy(SB), NOSPLIT, $0-0")
   118  	for i := 0; i < 128; i++ {
   119  		fmt.Fprintln(w, "\tMOVL\t(SI), CX")
   120  		fmt.Fprintln(w, "\tADDL\t$4, SI")
   121  		fmt.Fprintln(w, "\tMOVL\tCX, (DI)")
   122  		fmt.Fprintln(w, "\tADDL\t$4, DI")
   123  		fmt.Fprintln(w)
   124  	}
   125  	fmt.Fprintln(w, "\tRET")
   126  }
   127  
   128  func zeroARM(w io.Writer) {
   129  	// R0: zero
   130  	// R1: ptr to memory to be zeroed
   131  	// R1 is updated as a side effect.
   132  	fmt.Fprintln(w, "TEXT runtime·duffzero(SB), NOSPLIT, $0-0")
   133  	for i := 0; i < 128; i++ {
   134  		fmt.Fprintln(w, "\tMOVW.P\tR0, 4(R1)")
   135  	}
   136  	fmt.Fprintln(w, "\tRET")
   137  }
   138  
   139  func copyARM(w io.Writer) {
   140  	// R0: scratch space
   141  	// R1: ptr to source memory
   142  	// R2: ptr to destination memory
   143  	// R1 and R2 are updated as a side effect
   144  	fmt.Fprintln(w, "TEXT runtime·duffcopy(SB), NOSPLIT, $0-0")
   145  	for i := 0; i < 128; i++ {
   146  		fmt.Fprintln(w, "\tMOVW.P\t4(R1), R0")
   147  		fmt.Fprintln(w, "\tMOVW.P\tR0, 4(R2)")
   148  		fmt.Fprintln(w)
   149  	}
   150  	fmt.Fprintln(w, "\tRET")
   151  }
   152  
   153  func zeroARM64(w io.Writer) {
   154  	// ZR: always zero
   155  	// R16 (aka REGRT1): ptr to memory to be zeroed - 8
   156  	// On return, R16 points to the last zeroed dword.
   157  	fmt.Fprintln(w, "TEXT runtime·duffzero(SB), NOSPLIT, $-8-0")
   158  	for i := 0; i < 128; i++ {
   159  		fmt.Fprintln(w, "\tMOVD.W\tZR, 8(R16)")
   160  	}
   161  	fmt.Fprintln(w, "\tRET")
   162  }
   163  
   164  func copyARM64(w io.Writer) {
   165  	fmt.Fprintln(w, "// TODO: Implement runtime·duffcopy.")
   166  }
   167  
   168  func tagsPPC64x(w io.Writer) {
   169  	fmt.Fprintln(w)
   170  	fmt.Fprintln(w, "// +build ppc64 ppc64le")
   171  	fmt.Fprintln(w)
   172  }
   173  
   174  func zeroPPC64x(w io.Writer) {
   175  	// R0: always zero
   176  	// R3 (aka REGRT1): ptr to memory to be zeroed - 8
   177  	// On return, R3 points to the last zeroed dword.
   178  	fmt.Fprintln(w, "TEXT runtime·duffzero(SB), NOSPLIT|NOFRAME, $0-0")
   179  	for i := 0; i < 128; i++ {
   180  		fmt.Fprintln(w, "\tMOVDU\tR0, 8(R3)")
   181  	}
   182  	fmt.Fprintln(w, "\tRET")
   183  }
   184  
   185  func copyPPC64x(w io.Writer) {
   186  	fmt.Fprintln(w, "// TODO: Implement runtime·duffcopy.")
   187  }
   188  
   189  func zeroSPARC64(w io.Writer) {
   190  	// ZR (aka G0): always zero
   191  	// RT1 (aka G1): ptr to memory to be zeroed - 8
   192  	// On return, RT1 points to the last zeroed dword.
   193  	fmt.Fprintln(w, "TEXT runtime·duffzero(SB), NOSPLIT|NOFRAME, $0-0")
   194  	for i := 0; i < 128; i++ {
   195  		fmt.Fprintln(w, "\tMOVD\tZR, 8(RT1)")
   196  		fmt.Fprintln(w, "\tADD\t$8, RT1")
   197  	}
   198  	fmt.Fprintln(w, "\tRET")
   199  }
   200  
   201  func copySPARC64(w io.Writer) {
   202  	fmt.Fprintln(w, "// TODO: Implement runtime·duffcopy.")
   203  }
   204  
   205  func tagsMIPS64x(w io.Writer) {
   206  	fmt.Fprintln(w)
   207  	fmt.Fprintln(w, "// +build mips64 mips64le")
   208  	fmt.Fprintln(w)
   209  }
   210  
   211  func zeroMIPS64x(w io.Writer) {
   212  	// R0: always zero
   213  	// R1 (aka REGRT1): ptr to memory to be zeroed - 8
   214  	// On return, R1 points to the last zeroed dword.
   215  	fmt.Fprintln(w, "TEXT runtime·duffzero(SB), NOSPLIT, $-8-0")
   216  	for i := 0; i < 128; i++ {
   217  		fmt.Fprintln(w, "\tMOVV\tR0, 8(R1)")
   218  		fmt.Fprintln(w, "\tADDV\t$8, R1")
   219  	}
   220  	fmt.Fprintln(w, "\tRET")
   221  }
   222  
   223  func copyMIPS64x(w io.Writer) {
   224  	fmt.Fprintln(w, "// TODO: Implement runtime·duffcopy.")
   225  }