github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/compile/ssa/_gen/RISCV64Ops.go (about)

     1  // Copyright 2016 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  package main
     6  
     7  import (
     8  	"fmt"
     9  )
    10  
    11  // Notes:
    12  //  - Boolean types occupy the entire register. 0=false, 1=true.
    13  
    14  // Suffixes encode the bit width of various instructions:
    15  //
    16  // D (double word) = 64 bit int
    17  // W (word)        = 32 bit int
    18  // H (half word)   = 16 bit int
    19  // B (byte)        = 8 bit int
    20  // S (single)      = 32 bit float
    21  // D (double)      = 64 bit float
    22  // L               = 64 bit int, used when the opcode starts with F
    23  
    24  const (
    25  	riscv64REG_G    = 27
    26  	riscv64REG_CTXT = 26
    27  	riscv64REG_LR   = 1
    28  	riscv64REG_SP   = 2
    29  	riscv64REG_GP   = 3
    30  	riscv64REG_TP   = 4
    31  	riscv64REG_TMP  = 31
    32  	riscv64REG_ZERO = 0
    33  )
    34  
    35  func riscv64RegName(r int) string {
    36  	switch {
    37  	case r == riscv64REG_G:
    38  		return "g"
    39  	case r == riscv64REG_SP:
    40  		return "SP"
    41  	case 0 <= r && r <= 31:
    42  		return fmt.Sprintf("X%d", r)
    43  	case 32 <= r && r <= 63:
    44  		return fmt.Sprintf("F%d", r-32)
    45  	default:
    46  		panic(fmt.Sprintf("unknown register %d", r))
    47  	}
    48  }
    49  
    50  func init() {
    51  	var regNamesRISCV64 []string
    52  	var gpMask, fpMask, gpgMask, gpspMask, gpspsbMask, gpspsbgMask regMask
    53  	regNamed := make(map[string]regMask)
    54  
    55  	// Build the list of register names, creating an appropriately indexed
    56  	// regMask for the gp and fp registers as we go.
    57  	//
    58  	// If name is specified, use it rather than the riscv reg number.
    59  	addreg := func(r int, name string) regMask {
    60  		mask := regMask(1) << uint(len(regNamesRISCV64))
    61  		if name == "" {
    62  			name = riscv64RegName(r)
    63  		}
    64  		regNamesRISCV64 = append(regNamesRISCV64, name)
    65  		regNamed[name] = mask
    66  		return mask
    67  	}
    68  
    69  	// General purpose registers.
    70  	for r := 0; r <= 31; r++ {
    71  		if r == riscv64REG_LR {
    72  			// LR is not used by regalloc, so we skip it to leave
    73  			// room for pseudo-register SB.
    74  			continue
    75  		}
    76  
    77  		mask := addreg(r, "")
    78  
    79  		// Add general purpose registers to gpMask.
    80  		switch r {
    81  		// ZERO, GP, TP and TMP are not in any gp mask.
    82  		case riscv64REG_ZERO, riscv64REG_GP, riscv64REG_TP, riscv64REG_TMP:
    83  		case riscv64REG_G:
    84  			gpgMask |= mask
    85  			gpspsbgMask |= mask
    86  		case riscv64REG_SP:
    87  			gpspMask |= mask
    88  			gpspsbMask |= mask
    89  			gpspsbgMask |= mask
    90  		default:
    91  			gpMask |= mask
    92  			gpgMask |= mask
    93  			gpspMask |= mask
    94  			gpspsbMask |= mask
    95  			gpspsbgMask |= mask
    96  		}
    97  	}
    98  
    99  	// Floating pointer registers.
   100  	for r := 32; r <= 63; r++ {
   101  		mask := addreg(r, "")
   102  		fpMask |= mask
   103  	}
   104  
   105  	// Pseudo-register: SB
   106  	mask := addreg(-1, "SB")
   107  	gpspsbMask |= mask
   108  	gpspsbgMask |= mask
   109  
   110  	if len(regNamesRISCV64) > 64 {
   111  		// regMask is only 64 bits.
   112  		panic("Too many RISCV64 registers")
   113  	}
   114  
   115  	regCtxt := regNamed["X26"]
   116  	callerSave := gpMask | fpMask | regNamed["g"]
   117  
   118  	var (
   119  		gpstore  = regInfo{inputs: []regMask{gpspsbMask, gpspMask, 0}} // SB in first input so we can load from a global, but not in second to avoid using SB as a temporary register
   120  		gpstore0 = regInfo{inputs: []regMask{gpspsbMask}}
   121  		gp01     = regInfo{outputs: []regMask{gpMask}}
   122  		gp11     = regInfo{inputs: []regMask{gpMask}, outputs: []regMask{gpMask}}
   123  		gp21     = regInfo{inputs: []regMask{gpMask, gpMask}, outputs: []regMask{gpMask}}
   124  		gp22     = regInfo{inputs: []regMask{gpMask, gpMask}, outputs: []regMask{gpMask, gpMask}}
   125  		gpload   = regInfo{inputs: []regMask{gpspsbMask, 0}, outputs: []regMask{gpMask}}
   126  		gp11sb   = regInfo{inputs: []regMask{gpspsbMask}, outputs: []regMask{gpMask}}
   127  		gpxchg   = regInfo{inputs: []regMask{gpspsbgMask, gpgMask}, outputs: []regMask{gpMask}}
   128  		gpcas    = regInfo{inputs: []regMask{gpspsbgMask, gpgMask, gpgMask}, outputs: []regMask{gpMask}}
   129  		gpatomic = regInfo{inputs: []regMask{gpspsbgMask, gpgMask}}
   130  
   131  		fp11    = regInfo{inputs: []regMask{fpMask}, outputs: []regMask{fpMask}}
   132  		fp21    = regInfo{inputs: []regMask{fpMask, fpMask}, outputs: []regMask{fpMask}}
   133  		fp31    = regInfo{inputs: []regMask{fpMask, fpMask, fpMask}, outputs: []regMask{fpMask}}
   134  		gpfp    = regInfo{inputs: []regMask{gpMask}, outputs: []regMask{fpMask}}
   135  		fpgp    = regInfo{inputs: []regMask{fpMask}, outputs: []regMask{gpMask}}
   136  		fpstore = regInfo{inputs: []regMask{gpspsbMask, fpMask, 0}}
   137  		fpload  = regInfo{inputs: []regMask{gpspsbMask, 0}, outputs: []regMask{fpMask}}
   138  		fp2gp   = regInfo{inputs: []regMask{fpMask, fpMask}, outputs: []regMask{gpMask}}
   139  
   140  		call        = regInfo{clobbers: callerSave}
   141  		callClosure = regInfo{inputs: []regMask{gpspMask, regCtxt, 0}, clobbers: callerSave}
   142  		callInter   = regInfo{inputs: []regMask{gpMask}, clobbers: callerSave}
   143  	)
   144  
   145  	RISCV64ops := []opData{
   146  		{name: "ADD", argLength: 2, reg: gp21, asm: "ADD", commutative: true}, // arg0 + arg1
   147  		{name: "ADDI", argLength: 1, reg: gp11sb, asm: "ADDI", aux: "Int64"},  // arg0 + auxint
   148  		{name: "ADDIW", argLength: 1, reg: gp11, asm: "ADDIW", aux: "Int64"},  // 32 low bits of arg0 + auxint, sign extended to 64 bits
   149  		{name: "NEG", argLength: 1, reg: gp11, asm: "NEG"},                    // -arg0
   150  		{name: "NEGW", argLength: 1, reg: gp11, asm: "NEGW"},                  // -arg0 of 32 bits, sign extended to 64 bits
   151  		{name: "SUB", argLength: 2, reg: gp21, asm: "SUB"},                    // arg0 - arg1
   152  		{name: "SUBW", argLength: 2, reg: gp21, asm: "SUBW"},                  // 32 low bits of arg 0 - 32 low bits of arg 1, sign extended to 64 bits
   153  
   154  		// M extension. H means high (i.e., it returns the top bits of
   155  		// the result). U means unsigned. W means word (i.e., 32-bit).
   156  		{name: "MUL", argLength: 2, reg: gp21, asm: "MUL", commutative: true, typ: "Int64"}, // arg0 * arg1
   157  		{name: "MULW", argLength: 2, reg: gp21, asm: "MULW", commutative: true, typ: "Int32"},
   158  		{name: "MULH", argLength: 2, reg: gp21, asm: "MULH", commutative: true, typ: "Int64"},
   159  		{name: "MULHU", argLength: 2, reg: gp21, asm: "MULHU", commutative: true, typ: "UInt64"},
   160  		{name: "LoweredMuluhilo", argLength: 2, reg: gp22, resultNotInArgs: true}, // arg0 * arg1, return (hi, lo)
   161  		{name: "LoweredMuluover", argLength: 2, reg: gp22, resultNotInArgs: true}, // arg0 * arg1, return (64 bits of arg0*arg1, overflow)
   162  
   163  		{name: "DIV", argLength: 2, reg: gp21, asm: "DIV", typ: "Int64"}, // arg0 / arg1
   164  		{name: "DIVU", argLength: 2, reg: gp21, asm: "DIVU", typ: "UInt64"},
   165  		{name: "DIVW", argLength: 2, reg: gp21, asm: "DIVW", typ: "Int32"},
   166  		{name: "DIVUW", argLength: 2, reg: gp21, asm: "DIVUW", typ: "UInt32"},
   167  		{name: "REM", argLength: 2, reg: gp21, asm: "REM", typ: "Int64"}, // arg0 % arg1
   168  		{name: "REMU", argLength: 2, reg: gp21, asm: "REMU", typ: "UInt64"},
   169  		{name: "REMW", argLength: 2, reg: gp21, asm: "REMW", typ: "Int32"},
   170  		{name: "REMUW", argLength: 2, reg: gp21, asm: "REMUW", typ: "UInt32"},
   171  
   172  		{name: "MOVaddr", argLength: 1, reg: gp11sb, asm: "MOV", aux: "SymOff", rematerializeable: true, symEffect: "Addr"}, // arg0 + auxint + offset encoded in aux
   173  		// auxint+aux == add auxint and the offset of the symbol in aux (if any) to the effective address
   174  
   175  		{name: "MOVDconst", reg: gp01, asm: "MOV", typ: "UInt64", aux: "Int64", rematerializeable: true}, // auxint
   176  
   177  		// Loads: load <size> bits from arg0+auxint+aux and extend to 64 bits; arg1=mem
   178  		{name: "MOVBload", argLength: 2, reg: gpload, asm: "MOVB", aux: "SymOff", typ: "Int8", faultOnNilArg0: true, symEffect: "Read"},     //  8 bits, sign extend
   179  		{name: "MOVHload", argLength: 2, reg: gpload, asm: "MOVH", aux: "SymOff", typ: "Int16", faultOnNilArg0: true, symEffect: "Read"},    // 16 bits, sign extend
   180  		{name: "MOVWload", argLength: 2, reg: gpload, asm: "MOVW", aux: "SymOff", typ: "Int32", faultOnNilArg0: true, symEffect: "Read"},    // 32 bits, sign extend
   181  		{name: "MOVDload", argLength: 2, reg: gpload, asm: "MOV", aux: "SymOff", typ: "Int64", faultOnNilArg0: true, symEffect: "Read"},     // 64 bits
   182  		{name: "MOVBUload", argLength: 2, reg: gpload, asm: "MOVBU", aux: "SymOff", typ: "UInt8", faultOnNilArg0: true, symEffect: "Read"},  //  8 bits, zero extend
   183  		{name: "MOVHUload", argLength: 2, reg: gpload, asm: "MOVHU", aux: "SymOff", typ: "UInt16", faultOnNilArg0: true, symEffect: "Read"}, // 16 bits, zero extend
   184  		{name: "MOVWUload", argLength: 2, reg: gpload, asm: "MOVWU", aux: "SymOff", typ: "UInt32", faultOnNilArg0: true, symEffect: "Read"}, // 32 bits, zero extend
   185  
   186  		// Stores: store <size> lowest bits in arg1 to arg0+auxint+aux; arg2=mem
   187  		{name: "MOVBstore", argLength: 3, reg: gpstore, asm: "MOVB", aux: "SymOff", typ: "Mem", faultOnNilArg0: true, symEffect: "Write"}, //  8 bits
   188  		{name: "MOVHstore", argLength: 3, reg: gpstore, asm: "MOVH", aux: "SymOff", typ: "Mem", faultOnNilArg0: true, symEffect: "Write"}, // 16 bits
   189  		{name: "MOVWstore", argLength: 3, reg: gpstore, asm: "MOVW", aux: "SymOff", typ: "Mem", faultOnNilArg0: true, symEffect: "Write"}, // 32 bits
   190  		{name: "MOVDstore", argLength: 3, reg: gpstore, asm: "MOV", aux: "SymOff", typ: "Mem", faultOnNilArg0: true, symEffect: "Write"},  // 64 bits
   191  
   192  		// Stores: store <size> of zero in arg0+auxint+aux; arg1=mem
   193  		{name: "MOVBstorezero", argLength: 2, reg: gpstore0, aux: "SymOff", asm: "MOVB", typ: "Mem", faultOnNilArg0: true, symEffect: "Write"}, //  8 bits
   194  		{name: "MOVHstorezero", argLength: 2, reg: gpstore0, aux: "SymOff", asm: "MOVH", typ: "Mem", faultOnNilArg0: true, symEffect: "Write"}, // 16 bits
   195  		{name: "MOVWstorezero", argLength: 2, reg: gpstore0, aux: "SymOff", asm: "MOVW", typ: "Mem", faultOnNilArg0: true, symEffect: "Write"}, // 32 bits
   196  		{name: "MOVDstorezero", argLength: 2, reg: gpstore0, aux: "SymOff", asm: "MOV", typ: "Mem", faultOnNilArg0: true, symEffect: "Write"},  // 64 bits
   197  
   198  		// Conversions
   199  		{name: "MOVBreg", argLength: 1, reg: gp11, asm: "MOVB"},   // move from arg0, sign-extended from byte
   200  		{name: "MOVHreg", argLength: 1, reg: gp11, asm: "MOVH"},   // move from arg0, sign-extended from half
   201  		{name: "MOVWreg", argLength: 1, reg: gp11, asm: "MOVW"},   // move from arg0, sign-extended from word
   202  		{name: "MOVDreg", argLength: 1, reg: gp11, asm: "MOV"},    // move from arg0
   203  		{name: "MOVBUreg", argLength: 1, reg: gp11, asm: "MOVBU"}, // move from arg0, unsign-extended from byte
   204  		{name: "MOVHUreg", argLength: 1, reg: gp11, asm: "MOVHU"}, // move from arg0, unsign-extended from half
   205  		{name: "MOVWUreg", argLength: 1, reg: gp11, asm: "MOVWU"}, // move from arg0, unsign-extended from word
   206  
   207  		{name: "MOVDnop", argLength: 1, reg: regInfo{inputs: []regMask{gpMask}, outputs: []regMask{gpMask}}, resultInArg0: true}, // nop, return arg0 in same register
   208  
   209  		// Shift ops
   210  		{name: "SLL", argLength: 2, reg: gp21, asm: "SLL"},                   // arg0 << (aux1 & 63)
   211  		{name: "SRA", argLength: 2, reg: gp21, asm: "SRA"},                   // arg0 >> (aux1 & 63), signed
   212  		{name: "SRAW", argLength: 2, reg: gp21, asm: "SRAW"},                 // arg0 >> (aux1 & 31), signed
   213  		{name: "SRL", argLength: 2, reg: gp21, asm: "SRL"},                   // arg0 >> (aux1 & 63), unsigned
   214  		{name: "SRLW", argLength: 2, reg: gp21, asm: "SRLW"},                 // arg0 >> (aux1 & 31), unsigned
   215  		{name: "SLLI", argLength: 1, reg: gp11, asm: "SLLI", aux: "Int64"},   // arg0 << auxint, shift amount 0-63
   216  		{name: "SRAI", argLength: 1, reg: gp11, asm: "SRAI", aux: "Int64"},   // arg0 >> auxint, signed, shift amount 0-63
   217  		{name: "SRAIW", argLength: 1, reg: gp11, asm: "SRAIW", aux: "Int64"}, // arg0 >> auxint, signed, shift amount 0-31
   218  		{name: "SRLI", argLength: 1, reg: gp11, asm: "SRLI", aux: "Int64"},   // arg0 >> auxint, unsigned, shift amount 0-63
   219  		{name: "SRLIW", argLength: 1, reg: gp11, asm: "SRLIW", aux: "Int64"}, // arg0 >> auxint, unsigned, shift amount 0-31
   220  
   221  		// Bitwise ops
   222  		{name: "XOR", argLength: 2, reg: gp21, asm: "XOR", commutative: true}, // arg0 ^ arg1
   223  		{name: "XORI", argLength: 1, reg: gp11, asm: "XORI", aux: "Int64"},    // arg0 ^ auxint
   224  		{name: "OR", argLength: 2, reg: gp21, asm: "OR", commutative: true},   // arg0 | arg1
   225  		{name: "ORI", argLength: 1, reg: gp11, asm: "ORI", aux: "Int64"},      // arg0 | auxint
   226  		{name: "AND", argLength: 2, reg: gp21, asm: "AND", commutative: true}, // arg0 & arg1
   227  		{name: "ANDI", argLength: 1, reg: gp11, asm: "ANDI", aux: "Int64"},    // arg0 & auxint
   228  		{name: "NOT", argLength: 1, reg: gp11, asm: "NOT"},                    // ^arg0
   229  
   230  		// Generate boolean values
   231  		{name: "SEQZ", argLength: 1, reg: gp11, asm: "SEQZ"},                 // arg0 == 0, result is 0 or 1
   232  		{name: "SNEZ", argLength: 1, reg: gp11, asm: "SNEZ"},                 // arg0 != 0, result is 0 or 1
   233  		{name: "SLT", argLength: 2, reg: gp21, asm: "SLT"},                   // arg0 < arg1, result is 0 or 1
   234  		{name: "SLTI", argLength: 1, reg: gp11, asm: "SLTI", aux: "Int64"},   // arg0 < auxint, result is 0 or 1
   235  		{name: "SLTU", argLength: 2, reg: gp21, asm: "SLTU"},                 // arg0 < arg1, unsigned, result is 0 or 1
   236  		{name: "SLTIU", argLength: 1, reg: gp11, asm: "SLTIU", aux: "Int64"}, // arg0 < auxint, unsigned, result is 0 or 1
   237  
   238  		// Round ops to block fused-multiply-add extraction.
   239  		{name: "LoweredRound32F", argLength: 1, reg: fp11, resultInArg0: true},
   240  		{name: "LoweredRound64F", argLength: 1, reg: fp11, resultInArg0: true},
   241  
   242  		// Calls
   243  		{name: "CALLstatic", argLength: -1, reg: call, aux: "CallOff", call: true},               // call static function aux.(*gc.Sym). last arg=mem, auxint=argsize, returns mem
   244  		{name: "CALLtail", argLength: -1, reg: call, aux: "CallOff", call: true, tailCall: true}, // tail call static function aux.(*gc.Sym). last arg=mem, auxint=argsize, returns mem
   245  		{name: "CALLclosure", argLength: -1, reg: callClosure, aux: "CallOff", call: true},       // call function via closure. arg0=codeptr, arg1=closure, last arg=mem, auxint=argsize, returns mem
   246  		{name: "CALLinter", argLength: -1, reg: callInter, aux: "CallOff", call: true},           // call fn by pointer. arg0=codeptr, last arg=mem, auxint=argsize, returns mem
   247  
   248  		// duffzero
   249  		// arg0 = address of memory to zero (in X25, changed as side effect)
   250  		// arg1 = mem
   251  		// auxint = offset into duffzero code to start executing
   252  		// X1 (link register) changed because of function call
   253  		// returns mem
   254  		{
   255  			name:      "DUFFZERO",
   256  			aux:       "Int64",
   257  			argLength: 2,
   258  			reg: regInfo{
   259  				inputs:   []regMask{regNamed["X25"]},
   260  				clobbers: regNamed["X1"] | regNamed["X25"],
   261  			},
   262  			typ:            "Mem",
   263  			faultOnNilArg0: true,
   264  		},
   265  
   266  		// duffcopy
   267  		// arg0 = address of dst memory (in X25, changed as side effect)
   268  		// arg1 = address of src memory (in X24, changed as side effect)
   269  		// arg2 = mem
   270  		// auxint = offset into duffcopy code to start executing
   271  		// X1 (link register) changed because of function call
   272  		// returns mem
   273  		{
   274  			name:      "DUFFCOPY",
   275  			aux:       "Int64",
   276  			argLength: 3,
   277  			reg: regInfo{
   278  				inputs:   []regMask{regNamed["X25"], regNamed["X24"]},
   279  				clobbers: regNamed["X1"] | regNamed["X24"] | regNamed["X25"],
   280  			},
   281  			typ:            "Mem",
   282  			faultOnNilArg0: true,
   283  			faultOnNilArg1: true,
   284  		},
   285  
   286  		// Generic moves and zeros
   287  
   288  		// general unaligned zeroing
   289  		// arg0 = address of memory to zero (in X5, changed as side effect)
   290  		// arg1 = address of the last element to zero (inclusive)
   291  		// arg2 = mem
   292  		// auxint = element size
   293  		// returns mem
   294  		//	mov	ZERO, (X5)
   295  		//	ADD	$sz, X5
   296  		//	BGEU	Rarg1, X5, -2(PC)
   297  		{
   298  			name:      "LoweredZero",
   299  			aux:       "Int64",
   300  			argLength: 3,
   301  			reg: regInfo{
   302  				inputs:   []regMask{regNamed["X5"], gpMask},
   303  				clobbers: regNamed["X5"],
   304  			},
   305  			typ:            "Mem",
   306  			faultOnNilArg0: true,
   307  		},
   308  
   309  		// general unaligned move
   310  		// arg0 = address of dst memory (in X5, changed as side effect)
   311  		// arg1 = address of src memory (in X6, changed as side effect)
   312  		// arg2 = address of the last element of src (can't be X7 as we clobber it before using arg2)
   313  		// arg3 = mem
   314  		// auxint = alignment
   315  		// clobbers X7 as a tmp register.
   316  		// returns mem
   317  		//	mov	(X6), X7
   318  		//	mov	X7, (X5)
   319  		//	ADD	$sz, X5
   320  		//	ADD	$sz, X6
   321  		//	BGEU	Rarg2, X5, -4(PC)
   322  		{
   323  			name:      "LoweredMove",
   324  			aux:       "Int64",
   325  			argLength: 4,
   326  			reg: regInfo{
   327  				inputs:   []regMask{regNamed["X5"], regNamed["X6"], gpMask &^ regNamed["X7"]},
   328  				clobbers: regNamed["X5"] | regNamed["X6"] | regNamed["X7"],
   329  			},
   330  			typ:            "Mem",
   331  			faultOnNilArg0: true,
   332  			faultOnNilArg1: true,
   333  		},
   334  
   335  		// Atomic loads.
   336  		// load from arg0. arg1=mem.
   337  		// returns <value,memory> so they can be properly ordered with other loads.
   338  		{name: "LoweredAtomicLoad8", argLength: 2, reg: gpload, faultOnNilArg0: true},
   339  		{name: "LoweredAtomicLoad32", argLength: 2, reg: gpload, faultOnNilArg0: true},
   340  		{name: "LoweredAtomicLoad64", argLength: 2, reg: gpload, faultOnNilArg0: true},
   341  
   342  		// Atomic stores.
   343  		// store arg1 to *arg0. arg2=mem. returns memory.
   344  		{name: "LoweredAtomicStore8", argLength: 3, reg: gpstore, faultOnNilArg0: true, hasSideEffects: true},
   345  		{name: "LoweredAtomicStore32", argLength: 3, reg: gpstore, faultOnNilArg0: true, hasSideEffects: true},
   346  		{name: "LoweredAtomicStore64", argLength: 3, reg: gpstore, faultOnNilArg0: true, hasSideEffects: true},
   347  
   348  		// Atomic exchange.
   349  		// store arg1 to *arg0. arg2=mem. returns <old content of *arg0, memory>.
   350  		{name: "LoweredAtomicExchange32", argLength: 3, reg: gpxchg, resultNotInArgs: true, faultOnNilArg0: true, hasSideEffects: true},
   351  		{name: "LoweredAtomicExchange64", argLength: 3, reg: gpxchg, resultNotInArgs: true, faultOnNilArg0: true, hasSideEffects: true},
   352  
   353  		// Atomic add.
   354  		// *arg0 += arg1. arg2=mem. returns <new content of *arg0, memory>.
   355  		{name: "LoweredAtomicAdd32", argLength: 3, reg: gpxchg, resultNotInArgs: true, faultOnNilArg0: true, hasSideEffects: true, unsafePoint: true},
   356  		{name: "LoweredAtomicAdd64", argLength: 3, reg: gpxchg, resultNotInArgs: true, faultOnNilArg0: true, hasSideEffects: true, unsafePoint: true},
   357  
   358  		// Atomic compare and swap.
   359  		// arg0 = pointer, arg1 = old value, arg2 = new value, arg3 = memory.
   360  		// if *arg0 == arg1 {
   361  		//   *arg0 = arg2
   362  		//   return (true, memory)
   363  		// } else {
   364  		//   return (false, memory)
   365  		// }
   366  		// MOV  $0, Rout
   367  		// LR	(Rarg0), Rtmp
   368  		// BNE	Rtmp, Rarg1, 3(PC)
   369  		// SC	Rarg2, (Rarg0), Rtmp
   370  		// BNE	Rtmp, ZERO, -3(PC)
   371  		// MOV  $1, Rout
   372  		{name: "LoweredAtomicCas32", argLength: 4, reg: gpcas, resultNotInArgs: true, faultOnNilArg0: true, hasSideEffects: true, unsafePoint: true},
   373  		{name: "LoweredAtomicCas64", argLength: 4, reg: gpcas, resultNotInArgs: true, faultOnNilArg0: true, hasSideEffects: true, unsafePoint: true},
   374  
   375  		// Atomic 32 bit AND/OR.
   376  		// *arg0 &= (|=) arg1. arg2=mem. returns nil.
   377  		{name: "LoweredAtomicAnd32", argLength: 3, reg: gpatomic, asm: "AMOANDW", faultOnNilArg0: true, hasSideEffects: true},
   378  		{name: "LoweredAtomicOr32", argLength: 3, reg: gpatomic, asm: "AMOORW", faultOnNilArg0: true, hasSideEffects: true},
   379  
   380  		// Lowering pass-throughs
   381  		{name: "LoweredNilCheck", argLength: 2, faultOnNilArg0: true, nilCheck: true, reg: regInfo{inputs: []regMask{gpspMask}}}, // arg0=ptr,arg1=mem, returns void.  Faults if ptr is nil.
   382  		{name: "LoweredGetClosurePtr", reg: regInfo{outputs: []regMask{regCtxt}}},                                                // scheduler ensures only at beginning of entry block
   383  
   384  		// LoweredGetCallerSP returns the SP of the caller of the current function. arg0=mem.
   385  		{name: "LoweredGetCallerSP", argLength: 1, reg: gp01, rematerializeable: true},
   386  
   387  		// LoweredGetCallerPC evaluates to the PC to which its "caller" will return.
   388  		// I.e., if f calls g "calls" getcallerpc,
   389  		// the result should be the PC within f that g will return to.
   390  		// See runtime/stubs.go for a more detailed discussion.
   391  		{name: "LoweredGetCallerPC", reg: gp01, rematerializeable: true},
   392  
   393  		// LoweredWB invokes runtime.gcWriteBarrier. arg0=mem, auxint=# of buffer entries needed
   394  		// It saves all GP registers if necessary,
   395  		// but clobbers RA (LR) because it's a call
   396  		// and T6 (REG_TMP).
   397  		// Returns a pointer to a write barrier buffer in X24.
   398  		{name: "LoweredWB", argLength: 1, reg: regInfo{clobbers: (callerSave &^ (gpMask | regNamed["g"])) | regNamed["X1"], outputs: []regMask{regNamed["X24"]}}, clobberFlags: true, aux: "Int64"},
   399  
   400  		// Do data barrier. arg0=memorys
   401  		{name: "LoweredPubBarrier", argLength: 1, asm: "FENCE", hasSideEffects: true},
   402  
   403  		// There are three of these functions so that they can have three different register inputs.
   404  		// When we check 0 <= c <= cap (A), then 0 <= b <= c (B), then 0 <= a <= b (C), we want the
   405  		// default registers to match so we don't need to copy registers around unnecessarily.
   406  		{name: "LoweredPanicBoundsA", argLength: 3, aux: "Int64", reg: regInfo{inputs: []regMask{regNamed["X7"], regNamed["X28"]}}, typ: "Mem", call: true}, // arg0=idx, arg1=len, arg2=mem, returns memory. AuxInt contains report code (see PanicBounds in genericOps.go).
   407  		{name: "LoweredPanicBoundsB", argLength: 3, aux: "Int64", reg: regInfo{inputs: []regMask{regNamed["X6"], regNamed["X7"]}}, typ: "Mem", call: true},  // arg0=idx, arg1=len, arg2=mem, returns memory. AuxInt contains report code (see PanicBounds in genericOps.go).
   408  		{name: "LoweredPanicBoundsC", argLength: 3, aux: "Int64", reg: regInfo{inputs: []regMask{regNamed["X5"], regNamed["X6"]}}, typ: "Mem", call: true},  // arg0=idx, arg1=len, arg2=mem, returns memory. AuxInt contains report code (see PanicBounds in genericOps.go).
   409  
   410  		// F extension.
   411  		{name: "FADDS", argLength: 2, reg: fp21, asm: "FADDS", commutative: true, typ: "Float32"},                                           // arg0 + arg1
   412  		{name: "FSUBS", argLength: 2, reg: fp21, asm: "FSUBS", commutative: false, typ: "Float32"},                                          // arg0 - arg1
   413  		{name: "FMULS", argLength: 2, reg: fp21, asm: "FMULS", commutative: true, typ: "Float32"},                                           // arg0 * arg1
   414  		{name: "FDIVS", argLength: 2, reg: fp21, asm: "FDIVS", commutative: false, typ: "Float32"},                                          // arg0 / arg1
   415  		{name: "FMADDS", argLength: 3, reg: fp31, asm: "FMADDS", commutative: true, typ: "Float32"},                                         // (arg0 * arg1) + arg2
   416  		{name: "FMSUBS", argLength: 3, reg: fp31, asm: "FMSUBS", commutative: true, typ: "Float32"},                                         // (arg0 * arg1) - arg2
   417  		{name: "FNMADDS", argLength: 3, reg: fp31, asm: "FNMADDS", commutative: true, typ: "Float32"},                                       // -(arg0 * arg1) + arg2
   418  		{name: "FNMSUBS", argLength: 3, reg: fp31, asm: "FNMSUBS", commutative: true, typ: "Float32"},                                       // -(arg0 * arg1) - arg2
   419  		{name: "FSQRTS", argLength: 1, reg: fp11, asm: "FSQRTS", typ: "Float32"},                                                            // sqrt(arg0)
   420  		{name: "FNEGS", argLength: 1, reg: fp11, asm: "FNEGS", typ: "Float32"},                                                              // -arg0
   421  		{name: "FMVSX", argLength: 1, reg: gpfp, asm: "FMVSX", typ: "Float32"},                                                              // reinterpret arg0 as float
   422  		{name: "FCVTSW", argLength: 1, reg: gpfp, asm: "FCVTSW", typ: "Float32"},                                                            // float32(low 32 bits of arg0)
   423  		{name: "FCVTSL", argLength: 1, reg: gpfp, asm: "FCVTSL", typ: "Float32"},                                                            // float32(arg0)
   424  		{name: "FCVTWS", argLength: 1, reg: fpgp, asm: "FCVTWS", typ: "Int32"},                                                              // int32(arg0)
   425  		{name: "FCVTLS", argLength: 1, reg: fpgp, asm: "FCVTLS", typ: "Int64"},                                                              // int64(arg0)
   426  		{name: "FMOVWload", argLength: 2, reg: fpload, asm: "MOVF", aux: "SymOff", typ: "Float32", faultOnNilArg0: true, symEffect: "Read"}, // load float32 from arg0+auxint+aux
   427  		{name: "FMOVWstore", argLength: 3, reg: fpstore, asm: "MOVF", aux: "SymOff", typ: "Mem", faultOnNilArg0: true, symEffect: "Write"},  // store float32 to arg0+auxint+aux
   428  		{name: "FEQS", argLength: 2, reg: fp2gp, asm: "FEQS", commutative: true},                                                            // arg0 == arg1
   429  		{name: "FNES", argLength: 2, reg: fp2gp, asm: "FNES", commutative: true},                                                            // arg0 != arg1
   430  		{name: "FLTS", argLength: 2, reg: fp2gp, asm: "FLTS"},                                                                               // arg0 < arg1
   431  		{name: "FLES", argLength: 2, reg: fp2gp, asm: "FLES"},                                                                               // arg0 <= arg1
   432  
   433  		// D extension.
   434  		{name: "FADDD", argLength: 2, reg: fp21, asm: "FADDD", commutative: true, typ: "Float64"},                                           // arg0 + arg1
   435  		{name: "FSUBD", argLength: 2, reg: fp21, asm: "FSUBD", commutative: false, typ: "Float64"},                                          // arg0 - arg1
   436  		{name: "FMULD", argLength: 2, reg: fp21, asm: "FMULD", commutative: true, typ: "Float64"},                                           // arg0 * arg1
   437  		{name: "FDIVD", argLength: 2, reg: fp21, asm: "FDIVD", commutative: false, typ: "Float64"},                                          // arg0 / arg1
   438  		{name: "FMADDD", argLength: 3, reg: fp31, asm: "FMADDD", commutative: true, typ: "Float64"},                                         // (arg0 * arg1) + arg2
   439  		{name: "FMSUBD", argLength: 3, reg: fp31, asm: "FMSUBD", commutative: true, typ: "Float64"},                                         // (arg0 * arg1) - arg2
   440  		{name: "FNMADDD", argLength: 3, reg: fp31, asm: "FNMADDD", commutative: true, typ: "Float64"},                                       // -(arg0 * arg1) + arg2
   441  		{name: "FNMSUBD", argLength: 3, reg: fp31, asm: "FNMSUBD", commutative: true, typ: "Float64"},                                       // -(arg0 * arg1) - arg2
   442  		{name: "FSQRTD", argLength: 1, reg: fp11, asm: "FSQRTD", typ: "Float64"},                                                            // sqrt(arg0)
   443  		{name: "FNEGD", argLength: 1, reg: fp11, asm: "FNEGD", typ: "Float64"},                                                              // -arg0
   444  		{name: "FABSD", argLength: 1, reg: fp11, asm: "FABSD", typ: "Float64"},                                                              // abs(arg0)
   445  		{name: "FSGNJD", argLength: 2, reg: fp21, asm: "FSGNJD", typ: "Float64"},                                                            // copy sign of arg1 to arg0
   446  		{name: "FMVDX", argLength: 1, reg: gpfp, asm: "FMVDX", typ: "Float64"},                                                              // reinterpret arg0 as float
   447  		{name: "FCVTDW", argLength: 1, reg: gpfp, asm: "FCVTDW", typ: "Float64"},                                                            // float64(low 32 bits of arg0)
   448  		{name: "FCVTDL", argLength: 1, reg: gpfp, asm: "FCVTDL", typ: "Float64"},                                                            // float64(arg0)
   449  		{name: "FCVTWD", argLength: 1, reg: fpgp, asm: "FCVTWD", typ: "Int32"},                                                              // int32(arg0)
   450  		{name: "FCVTLD", argLength: 1, reg: fpgp, asm: "FCVTLD", typ: "Int64"},                                                              // int64(arg0)
   451  		{name: "FCVTDS", argLength: 1, reg: fp11, asm: "FCVTDS", typ: "Float64"},                                                            // float64(arg0)
   452  		{name: "FCVTSD", argLength: 1, reg: fp11, asm: "FCVTSD", typ: "Float32"},                                                            // float32(arg0)
   453  		{name: "FMOVDload", argLength: 2, reg: fpload, asm: "MOVD", aux: "SymOff", typ: "Float64", faultOnNilArg0: true, symEffect: "Read"}, // load float64 from arg0+auxint+aux
   454  		{name: "FMOVDstore", argLength: 3, reg: fpstore, asm: "MOVD", aux: "SymOff", typ: "Mem", faultOnNilArg0: true, symEffect: "Write"},  // store float6 to arg0+auxint+aux
   455  		{name: "FEQD", argLength: 2, reg: fp2gp, asm: "FEQD", commutative: true},                                                            // arg0 == arg1
   456  		{name: "FNED", argLength: 2, reg: fp2gp, asm: "FNED", commutative: true},                                                            // arg0 != arg1
   457  		{name: "FLTD", argLength: 2, reg: fp2gp, asm: "FLTD"},                                                                               // arg0 < arg1
   458  		{name: "FLED", argLength: 2, reg: fp2gp, asm: "FLED"},                                                                               // arg0 <= arg1
   459  	}
   460  
   461  	RISCV64blocks := []blockData{
   462  		{name: "BEQ", controls: 2},
   463  		{name: "BNE", controls: 2},
   464  		{name: "BLT", controls: 2},
   465  		{name: "BGE", controls: 2},
   466  		{name: "BLTU", controls: 2},
   467  		{name: "BGEU", controls: 2},
   468  
   469  		{name: "BEQZ", controls: 1},
   470  		{name: "BNEZ", controls: 1},
   471  		{name: "BLEZ", controls: 1},
   472  		{name: "BGEZ", controls: 1},
   473  		{name: "BLTZ", controls: 1},
   474  		{name: "BGTZ", controls: 1},
   475  	}
   476  
   477  	archs = append(archs, arch{
   478  		name:            "RISCV64",
   479  		pkg:             "github.com/go-asm/go/cmd/obj/riscv",
   480  		genfile:         "../../riscv64/ssa.go",
   481  		ops:             RISCV64ops,
   482  		blocks:          RISCV64blocks,
   483  		regnames:        regNamesRISCV64,
   484  		gpregmask:       gpMask,
   485  		fpregmask:       fpMask,
   486  		framepointerreg: -1, // not used
   487  		// Integer parameters passed in register X10-X17, X8-X9, X18-X23
   488  		ParamIntRegNames: "X10 X11 X12 X13 X14 X15 X16 X17 X8 X9 X18 X19 X20 X21 X22 X23",
   489  		// Float parameters passed in register F10-F17, F8-F9, F18-F23
   490  		ParamFloatRegNames: "F10 F11 F12 F13 F14 F15 F16 F17 F8 F9 F18 F19 F20 F21 F22 F23",
   491  	})
   492  }