Source file src/runtime/mkduff.go

     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  //go: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 the uses in cmd/compile/internal/*/*.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 the uses in cmd/compile/internal/*/*.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  	"log"
    31  	"os"
    32  )
    33  
    34  func main() {
    35  	gen("386", notags, zero386, copy386)
    36  	gen("arm", notags, zeroARM, copyARM)
    37  	gen("loong64", notags, zeroLOONG64, copyLOONG64)
    38  	gen("ppc64x", tagsPPC64x, zeroPPC64x, copyPPC64x)
    39  	gen("mips64x", tagsMIPS64x, zeroMIPS64x, copyMIPS64x)
    40  	gen("riscv64", notags, zeroRISCV64, copyRISCV64)
    41  }
    42  
    43  func gen(arch string, tags, zero, copy func(io.Writer)) {
    44  	var buf bytes.Buffer
    45  
    46  	fmt.Fprintln(&buf, "// Code generated by mkduff.go; DO NOT EDIT.")
    47  	fmt.Fprintln(&buf, "// Run go generate from src/runtime to update.")
    48  	fmt.Fprintln(&buf, "// See mkduff.go for comments.")
    49  	tags(&buf)
    50  	fmt.Fprintln(&buf, "#include \"textflag.h\"")
    51  	fmt.Fprintln(&buf)
    52  	zero(&buf)
    53  	fmt.Fprintln(&buf)
    54  	copy(&buf)
    55  
    56  	if err := os.WriteFile("duff_"+arch+".s", buf.Bytes(), 0644); err != nil {
    57  		log.Fatalln(err)
    58  	}
    59  }
    60  
    61  func notags(w io.Writer) { fmt.Fprintln(w) }
    62  
    63  func zeroAMD64(w io.Writer) {
    64  	// X15: zero
    65  	// DI: ptr to memory to be zeroed
    66  	// DI is updated as a side effect.
    67  	fmt.Fprintln(w, "TEXT runtime·duffzero<ABIInternal>(SB), NOSPLIT|NOFRAME, $0-0")
    68  	for i := 0; i < 16; i++ {
    69  		fmt.Fprintln(w, "\tMOVUPS\tX15,(DI)")
    70  		fmt.Fprintln(w, "\tMOVUPS\tX15,16(DI)")
    71  		fmt.Fprintln(w, "\tMOVUPS\tX15,32(DI)")
    72  		fmt.Fprintln(w, "\tMOVUPS\tX15,48(DI)")
    73  		fmt.Fprintln(w, "\tLEAQ\t64(DI),DI") // We use lea instead of add, to avoid clobbering flags
    74  		fmt.Fprintln(w)
    75  	}
    76  	fmt.Fprintln(w, "\tRET")
    77  }
    78  
    79  func copyAMD64(w io.Writer) {
    80  	// SI: ptr to source memory
    81  	// DI: ptr to destination memory
    82  	// SI and DI are updated as a side effect.
    83  	//
    84  	// This is equivalent to a sequence of MOVSQ but
    85  	// for some reason that is 3.5x slower than this code.
    86  	fmt.Fprintln(w, "TEXT runtime·duffcopy<ABIInternal>(SB), NOSPLIT|NOFRAME, $0-0")
    87  	for i := 0; i < 64; i++ {
    88  		fmt.Fprintln(w, "\tMOVUPS\t(SI), X0")
    89  		fmt.Fprintln(w, "\tADDQ\t$16, SI")
    90  		fmt.Fprintln(w, "\tMOVUPS\tX0, (DI)")
    91  		fmt.Fprintln(w, "\tADDQ\t$16, DI")
    92  		fmt.Fprintln(w)
    93  	}
    94  	fmt.Fprintln(w, "\tRET")
    95  }
    96  
    97  func zero386(w io.Writer) {
    98  	// AX: zero
    99  	// DI: ptr to memory to be zeroed
   100  	// DI is updated as a side effect.
   101  	fmt.Fprintln(w, "TEXT runtime·duffzero(SB), NOSPLIT, $0-0")
   102  	for i := 0; i < 128; i++ {
   103  		fmt.Fprintln(w, "\tSTOSL")
   104  	}
   105  	fmt.Fprintln(w, "\tRET")
   106  }
   107  
   108  func copy386(w io.Writer) {
   109  	// SI: ptr to source memory
   110  	// DI: ptr to destination memory
   111  	// SI and DI are updated as a side effect.
   112  	//
   113  	// This is equivalent to a sequence of MOVSL but
   114  	// for some reason MOVSL is really slow.
   115  	fmt.Fprintln(w, "TEXT runtime·duffcopy(SB), NOSPLIT, $0-0")
   116  	for i := 0; i < 128; i++ {
   117  		fmt.Fprintln(w, "\tMOVL\t(SI), CX")
   118  		fmt.Fprintln(w, "\tADDL\t$4, SI")
   119  		fmt.Fprintln(w, "\tMOVL\tCX, (DI)")
   120  		fmt.Fprintln(w, "\tADDL\t$4, DI")
   121  		fmt.Fprintln(w)
   122  	}
   123  	fmt.Fprintln(w, "\tRET")
   124  }
   125  
   126  func zeroARM(w io.Writer) {
   127  	// R0: zero
   128  	// R1: ptr to memory to be zeroed
   129  	// R1 is updated as a side effect.
   130  	fmt.Fprintln(w, "TEXT runtime·duffzero(SB), NOSPLIT, $0-0")
   131  	for i := 0; i < 128; i++ {
   132  		fmt.Fprintln(w, "\tMOVW.P\tR0, 4(R1)")
   133  	}
   134  	fmt.Fprintln(w, "\tRET")
   135  }
   136  
   137  func copyARM(w io.Writer) {
   138  	// R0: scratch space
   139  	// R1: ptr to source memory
   140  	// R2: ptr to destination memory
   141  	// R1 and R2 are updated as a side effect
   142  	fmt.Fprintln(w, "TEXT runtime·duffcopy(SB), NOSPLIT, $0-0")
   143  	for i := 0; i < 128; i++ {
   144  		fmt.Fprintln(w, "\tMOVW.P\t4(R1), R0")
   145  		fmt.Fprintln(w, "\tMOVW.P\tR0, 4(R2)")
   146  		fmt.Fprintln(w)
   147  	}
   148  	fmt.Fprintln(w, "\tRET")
   149  }
   150  
   151  func zeroARM64(w io.Writer) {
   152  	// ZR: always zero
   153  	// R20: ptr to memory to be zeroed
   154  	// On return, R20 points to the last zeroed dword.
   155  	fmt.Fprintln(w, "TEXT runtime·duffzero<ABIInternal>(SB), NOSPLIT|NOFRAME, $0-0")
   156  	for i := 0; i < 63; i++ {
   157  		fmt.Fprintln(w, "\tSTP.P\t(ZR, ZR), 16(R20)")
   158  	}
   159  	fmt.Fprintln(w, "\tSTP\t(ZR, ZR), (R20)")
   160  	fmt.Fprintln(w, "\tRET")
   161  }
   162  
   163  func copyARM64(w io.Writer) {
   164  	// R20: ptr to source memory
   165  	// R21: ptr to destination memory
   166  	// R26, R27 (aka REGTMP): scratch space
   167  	// R20 and R21 are updated as a side effect
   168  	fmt.Fprintln(w, "TEXT runtime·duffcopy<ABIInternal>(SB), NOSPLIT|NOFRAME, $0-0")
   169  
   170  	for i := 0; i < 64; i++ {
   171  		fmt.Fprintln(w, "\tLDP.P\t16(R20), (R26, R27)")
   172  		fmt.Fprintln(w, "\tSTP.P\t(R26, R27), 16(R21)")
   173  		fmt.Fprintln(w)
   174  	}
   175  	fmt.Fprintln(w, "\tRET")
   176  }
   177  
   178  func zeroLOONG64(w io.Writer) {
   179  	// R0: always zero
   180  	// R20: ptr to memory to be zeroed
   181  	// On return, R20 points to the last zeroed dword.
   182  	fmt.Fprintln(w, "TEXT runtime·duffzero<ABIInternal>(SB), NOSPLIT|NOFRAME, $0-0")
   183  	for i := 0; i < 128; i++ {
   184  		fmt.Fprintln(w, "\tMOVV\tR0, (R20)")
   185  		fmt.Fprintln(w, "\tADDV\t$8, R20")
   186  	}
   187  	fmt.Fprintln(w, "\tRET")
   188  }
   189  
   190  func copyLOONG64(w io.Writer) {
   191  	fmt.Fprintln(w, "TEXT runtime·duffcopy<ABIInternal>(SB), NOSPLIT|NOFRAME, $0-0")
   192  	for i := 0; i < 128; i++ {
   193  		fmt.Fprintln(w, "\tMOVV\t(R20), R30")
   194  		fmt.Fprintln(w, "\tADDV\t$8, R20")
   195  		fmt.Fprintln(w, "\tMOVV\tR30, (R21)")
   196  		fmt.Fprintln(w, "\tADDV\t$8, R21")
   197  		fmt.Fprintln(w)
   198  	}
   199  	fmt.Fprintln(w, "\tRET")
   200  }
   201  
   202  func tagsPPC64x(w io.Writer) {
   203  	fmt.Fprintln(w)
   204  	fmt.Fprintln(w, "//go:build ppc64 || ppc64le")
   205  	fmt.Fprintln(w)
   206  }
   207  
   208  func zeroPPC64x(w io.Writer) {
   209  	// R0: always zero
   210  	// R3 (aka REGRT1): ptr to memory to be zeroed - 8
   211  	// On return, R3 points to the last zeroed dword.
   212  	fmt.Fprintln(w, "TEXT runtime·duffzero<ABIInternal>(SB), NOSPLIT|NOFRAME, $0-0")
   213  	for i := 0; i < 128; i++ {
   214  		fmt.Fprintln(w, "\tMOVDU\tR0, 8(R20)")
   215  	}
   216  	fmt.Fprintln(w, "\tRET")
   217  }
   218  
   219  func copyPPC64x(w io.Writer) {
   220  	// duffcopy is not used on PPC64.
   221  	fmt.Fprintln(w, "TEXT runtime·duffcopy<ABIInternal>(SB), NOSPLIT|NOFRAME, $0-0")
   222  	for i := 0; i < 128; i++ {
   223  		fmt.Fprintln(w, "\tMOVDU\t8(R20), R5")
   224  		fmt.Fprintln(w, "\tMOVDU\tR5, 8(R21)")
   225  	}
   226  	fmt.Fprintln(w, "\tRET")
   227  }
   228  
   229  func tagsMIPS64x(w io.Writer) {
   230  	fmt.Fprintln(w)
   231  	fmt.Fprintln(w, "//go:build mips64 || mips64le")
   232  	fmt.Fprintln(w)
   233  }
   234  
   235  func zeroMIPS64x(w io.Writer) {
   236  	// R0: always zero
   237  	// R1 (aka REGRT1): ptr to memory to be zeroed - 8
   238  	// On return, R1 points to the last zeroed dword.
   239  	fmt.Fprintln(w, "TEXT runtime·duffzero(SB), NOSPLIT|NOFRAME, $0-0")
   240  	for i := 0; i < 128; i++ {
   241  		fmt.Fprintln(w, "\tMOVV\tR0, 8(R1)")
   242  		fmt.Fprintln(w, "\tADDV\t$8, R1")
   243  	}
   244  	fmt.Fprintln(w, "\tRET")
   245  }
   246  
   247  func copyMIPS64x(w io.Writer) {
   248  	fmt.Fprintln(w, "TEXT runtime·duffcopy(SB), NOSPLIT|NOFRAME, $0-0")
   249  	for i := 0; i < 128; i++ {
   250  		fmt.Fprintln(w, "\tMOVV\t(R1), R23")
   251  		fmt.Fprintln(w, "\tADDV\t$8, R1")
   252  		fmt.Fprintln(w, "\tMOVV\tR23, (R2)")
   253  		fmt.Fprintln(w, "\tADDV\t$8, R2")
   254  		fmt.Fprintln(w)
   255  	}
   256  	fmt.Fprintln(w, "\tRET")
   257  }
   258  
   259  func zeroRISCV64(w io.Writer) {
   260  	// ZERO: always zero
   261  	// X25: ptr to memory to be zeroed
   262  	// X25 is updated as a side effect.
   263  	fmt.Fprintln(w, "TEXT runtime·duffzero<ABIInternal>(SB), NOSPLIT|NOFRAME, $0-0")
   264  	for i := 0; i < 128; i++ {
   265  		fmt.Fprintln(w, "\tMOV\tZERO, (X25)")
   266  		fmt.Fprintln(w, "\tADD\t$8, X25")
   267  	}
   268  	fmt.Fprintln(w, "\tRET")
   269  }
   270  
   271  func copyRISCV64(w io.Writer) {
   272  	// X24: ptr to source memory
   273  	// X25: ptr to destination memory
   274  	// X24 and X25 are updated as a side effect
   275  	fmt.Fprintln(w, "TEXT runtime·duffcopy<ABIInternal>(SB), NOSPLIT|NOFRAME, $0-0")
   276  	for i := 0; i < 128; i++ {
   277  		fmt.Fprintln(w, "\tMOV\t(X24), X31")
   278  		fmt.Fprintln(w, "\tADD\t$8, X24")
   279  		fmt.Fprintln(w, "\tMOV\tX31, (X25)")
   280  		fmt.Fprintln(w, "\tADD\t$8, X25")
   281  		fmt.Fprintln(w)
   282  	}
   283  	fmt.Fprintln(w, "\tRET")
   284  }
   285  

View as plain text