diff --git a/src/cmd/compile/internal/loong64/ssa.go b/src/cmd/compile/internal/loong64/ssa.go index 100ebf04567..bd0d96a6954 100644 --- a/src/cmd/compile/internal/loong64/ssa.go +++ b/src/cmd/compile/internal/loong64/ssa.go @@ -1266,6 +1266,29 @@ func ssaGenBlock(s *ssagen.State, b, next *ssa.Block) { p.From.Reg = b.Controls[0].Reg() } } + case ssa.BlockLOONG64JUMPTABLE: + // ALSLV $3, Rarg0, Rarg1, REGTMP + // MOVV (REGTMP), REGTMP + // JMP (REGTMP) + p := s.Prog(loong64.AALSLV) + p.From.Type = obj.TYPE_CONST + p.From.Offset = 3 // idx*8 + p.Reg = b.Controls[0].Reg() + p.AddRestSourceReg(b.Controls[1].Reg()) + p.To.Type = obj.TYPE_REG + p.To.Reg = loong64.REGTMP + p1 := s.Prog(loong64.AMOVV) + p1.From.Type = obj.TYPE_MEM + p1.From.Reg = loong64.REGTMP + p1.From.Offset = 0 + p1.To.Type = obj.TYPE_REG + p1.To.Reg = loong64.REGTMP + p2 := s.Prog(obj.AJMP) + p2.To.Type = obj.TYPE_MEM + p2.To.Reg = loong64.REGTMP + // Save jump tables for later resolution of the target blocks. + s.JumpTables = append(s.JumpTables, b) + default: b.Fatalf("branch not implemented: %s", b.LongString()) } diff --git a/src/cmd/compile/internal/ssa/_gen/LOONG64.rules b/src/cmd/compile/internal/ssa/_gen/LOONG64.rules index 0d2384143c4..287eedee374 100644 --- a/src/cmd/compile/internal/ssa/_gen/LOONG64.rules +++ b/src/cmd/compile/internal/ssa/_gen/LOONG64.rules @@ -504,6 +504,8 @@ (MOVBUreg x:((SGT|SGTU) _ _)) => x (MOVBUreg x:(XOR (MOVVconst [1]) ((SGT|SGTU) _ _))) => x +(JumpTable idx) => (JUMPTABLE {makeJumpTableSym(b)} idx (MOVVaddr {makeJumpTableSym(b)} (SB))) + // Write barrier. (WB ...) => (LoweredWB ...) diff --git a/src/cmd/compile/internal/ssa/_gen/LOONG64Ops.go b/src/cmd/compile/internal/ssa/_gen/LOONG64Ops.go index a3db4def569..a85a566660e 100644 --- a/src/cmd/compile/internal/ssa/_gen/LOONG64Ops.go +++ b/src/cmd/compile/internal/ssa/_gen/LOONG64Ops.go @@ -577,6 +577,12 @@ func init() { {name: "BLT", controls: 2}, // controls[0] < controls[1] {name: "BGEU", controls: 2}, // controls[0] >= controls[1], unsigned {name: "BLTU", controls: 2}, // controls[0] < controls[1], unsigned + + // JUMPTABLE implements jump tables. + // Aux is the symbol (an *obj.LSym) for the jump table. + // control[0] is the index into the jump table. + // control[1] is the address of the jump table (the address of the symbol stored in Aux). + {name: "JUMPTABLE", controls: 2, aux: "Sym"}, } archs = append(archs, arch{ diff --git a/src/cmd/compile/internal/ssa/opGen.go b/src/cmd/compile/internal/ssa/opGen.go index b4aae50b895..92adf5341b2 100644 --- a/src/cmd/compile/internal/ssa/opGen.go +++ b/src/cmd/compile/internal/ssa/opGen.go @@ -108,6 +108,7 @@ const ( BlockLOONG64BLT BlockLOONG64BGEU BlockLOONG64BLTU + BlockLOONG64JUMPTABLE BlockMIPSEQ BlockMIPSNE @@ -250,20 +251,21 @@ var blockString = [...]string{ BlockARM64GEnoov: "GEnoov", BlockARM64JUMPTABLE: "JUMPTABLE", - BlockLOONG64EQZ: "EQZ", - BlockLOONG64NEZ: "NEZ", - BlockLOONG64LTZ: "LTZ", - BlockLOONG64LEZ: "LEZ", - BlockLOONG64GTZ: "GTZ", - BlockLOONG64GEZ: "GEZ", - BlockLOONG64FPT: "FPT", - BlockLOONG64FPF: "FPF", - BlockLOONG64BEQ: "BEQ", - BlockLOONG64BNE: "BNE", - BlockLOONG64BGE: "BGE", - BlockLOONG64BLT: "BLT", - BlockLOONG64BGEU: "BGEU", - BlockLOONG64BLTU: "BLTU", + BlockLOONG64EQZ: "EQZ", + BlockLOONG64NEZ: "NEZ", + BlockLOONG64LTZ: "LTZ", + BlockLOONG64LEZ: "LEZ", + BlockLOONG64GTZ: "GTZ", + BlockLOONG64GEZ: "GEZ", + BlockLOONG64FPT: "FPT", + BlockLOONG64FPF: "FPF", + BlockLOONG64BEQ: "BEQ", + BlockLOONG64BNE: "BNE", + BlockLOONG64BGE: "BGE", + BlockLOONG64BLT: "BLT", + BlockLOONG64BGEU: "BGEU", + BlockLOONG64BLTU: "BLTU", + BlockLOONG64JUMPTABLE: "JUMPTABLE", BlockMIPSEQ: "EQ", BlockMIPSNE: "NE", diff --git a/src/cmd/compile/internal/ssa/rewriteLOONG64.go b/src/cmd/compile/internal/ssa/rewriteLOONG64.go index 3990b2833b2..3fc57e9f497 100644 --- a/src/cmd/compile/internal/ssa/rewriteLOONG64.go +++ b/src/cmd/compile/internal/ssa/rewriteLOONG64.go @@ -12148,6 +12148,19 @@ func rewriteBlockLOONG64(b *Block) bool { b.resetWithControl(BlockLOONG64NEZ, v0) return true } + case BlockJumpTable: + // match: (JumpTable idx) + // result: (JUMPTABLE {makeJumpTableSym(b)} idx (MOVVaddr {makeJumpTableSym(b)} (SB))) + for { + idx := b.Controls[0] + v0 := b.NewValue0(b.Pos, OpLOONG64MOVVaddr, typ.Uintptr) + v0.Aux = symToAux(makeJumpTableSym(b)) + v1 := b.NewValue0(b.Pos, OpSB, typ.Uintptr) + v0.AddArg(v1) + b.resetWithControl2(BlockLOONG64JUMPTABLE, idx, v0) + b.Aux = symToAux(makeJumpTableSym(b)) + return true + } case BlockLOONG64LEZ: // match: (LEZ (MOVVconst [c]) yes no) // cond: c <= 0 diff --git a/src/cmd/internal/obj/loong64/asm.go b/src/cmd/internal/obj/loong64/asm.go index e20ceaae959..ca6e2be4aa9 100644 --- a/src/cmd/internal/obj/loong64/asm.go +++ b/src/cmd/internal/obj/loong64/asm.go @@ -707,6 +707,15 @@ func span0(ctxt *obj.Link, cursym *obj.LSym, newprog obj.ProgAlloc) { // so instruction sequences that use REGTMP are unsafe to // preempt asynchronously. obj.MarkUnsafePoints(c.ctxt, c.cursym.Func().Text, c.newprog, c.isUnsafePoint, c.isRestartable) + + // Now that we know byte offsets, we can generate jump table entries. + for _, jt := range cursym.Func().JumpTables { + for i, p := range jt.Targets { + // The ith jumptable entry points to the p.Pc'th + // byte in the function symbol s. + jt.Sym.WriteAddr(ctxt, int64(i)*8, 8, cursym, p.Pc) + } + } } // isUnsafePoint returns whether p is an unsafe point. diff --git a/src/cmd/internal/sys/arch.go b/src/cmd/internal/sys/arch.go index 484538f28f0..3c92a6bbf25 100644 --- a/src/cmd/internal/sys/arch.go +++ b/src/cmd/internal/sys/arch.go @@ -145,6 +145,7 @@ var ArchLoong64 = &Arch{ MinLC: 4, Alignment: 8, // Unaligned accesses are not guaranteed to be fast CanMergeLoads: true, + CanJumpTable: true, HasLR: true, FixedFrameSize: 8, // LR } diff --git a/test/codegen/switch.go b/test/codegen/switch.go index 509343110a3..1a66a5ddf84 100644 --- a/test/codegen/switch.go +++ b/test/codegen/switch.go @@ -25,6 +25,7 @@ func f(x string) int { func square(x int) int { // amd64:`JMP\s\(.*\)\(.*\)$` // arm64:`MOVD\s\(R.*\)\(R.*<<3\)`,`JMP\s\(R.*\)$` + // loong64: `ALSLV`,`MOVV`,`JMP` switch x { case 1: return 1 @@ -51,6 +52,7 @@ func square(x int) int { func length(x string) int { // amd64:`JMP\s\(.*\)\(.*\)$` // arm64:`MOVD\s\(R.*\)\(R.*<<3\)`,`JMP\s\(R.*\)$` + // loong64:`ALSLV`,`MOVV`,`JMP` switch x { case "a": return 1