Source file src/cmd/compile/internal/ssa/gen/RISCV64Ops.go

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

View as plain text