committed by
GitHub
parent
cde7e9483b
commit
ff7a160610
42
tests/fixedbugs/issue122/asm.go
Normal file
42
tests/fixedbugs/issue122/asm.go
Normal file
@@ -0,0 +1,42 @@
|
||||
// +build ignore
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
. "github.com/mmcloughlin/avo/build"
|
||||
. "github.com/mmcloughlin/avo/operand"
|
||||
)
|
||||
|
||||
func main() {
|
||||
TEXT("Triangle", NOSPLIT, "func(n uint64) uint64")
|
||||
Doc("Triangle computes the nth triangle number.")
|
||||
n := Load(Param("n"), GP64())
|
||||
|
||||
Comment("Initialize sum register to zero.")
|
||||
s := GP64()
|
||||
XORQ(s, s)
|
||||
|
||||
// Use two labels for the top of the loop.
|
||||
Label("loop_even")
|
||||
Label("loop_odd")
|
||||
Comment("Loop until n is zero.")
|
||||
CMPQ(n, Imm(0))
|
||||
JE(LabelRef("done"))
|
||||
|
||||
Comment("Add n to sum.")
|
||||
ADDQ(n, s)
|
||||
|
||||
Comment("Decrement n.")
|
||||
DECQ(n)
|
||||
|
||||
Comment("Jump to one of the loop labels depending on parity.")
|
||||
TESTQ(U32(1), n)
|
||||
JZ(LabelRef("loop_even"))
|
||||
JMP(LabelRef("loop_odd"))
|
||||
|
||||
Label("done")
|
||||
Comment("Store sum to return value.")
|
||||
Store(s, ReturnIndex(0))
|
||||
RET()
|
||||
Generate()
|
||||
}
|
||||
2
tests/fixedbugs/issue122/doc.go
Normal file
2
tests/fixedbugs/issue122/doc.go
Normal file
@@ -0,0 +1,2 @@
|
||||
// Package issue122 tests consecutive labels.
|
||||
package issue122
|
||||
32
tests/fixedbugs/issue122/issue122.s
Normal file
32
tests/fixedbugs/issue122/issue122.s
Normal file
@@ -0,0 +1,32 @@
|
||||
// Code generated by command: go run asm.go -out issue122.s -stubs stub.go. DO NOT EDIT.
|
||||
|
||||
#include "textflag.h"
|
||||
|
||||
// func Triangle(n uint64) uint64
|
||||
TEXT ·Triangle(SB), NOSPLIT, $0-16
|
||||
MOVQ n+0(FP), AX
|
||||
|
||||
// Initialize sum register to zero.
|
||||
XORQ CX, CX
|
||||
|
||||
loop_even:
|
||||
loop_odd:
|
||||
// Loop until n is zero.
|
||||
CMPQ AX, $0x00
|
||||
JE done
|
||||
|
||||
// Add n to sum.
|
||||
ADDQ AX, CX
|
||||
|
||||
// Decrement n.
|
||||
DECQ AX
|
||||
|
||||
// Jump to one of the loop labels depending on parity.
|
||||
TESTQ $0x00000001, AX
|
||||
JZ loop_even
|
||||
JMP loop_odd
|
||||
|
||||
done:
|
||||
// Store sum to return value.
|
||||
MOVQ CX, ret+8(FP)
|
||||
RET
|
||||
16
tests/fixedbugs/issue122/issue122_test.go
Normal file
16
tests/fixedbugs/issue122/issue122_test.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package issue122
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
//go:generate go run asm.go -out issue122.s -stubs stub.go
|
||||
|
||||
func TestTriangle(t *testing.T) {
|
||||
expect := func(n uint64) uint64 { return n * (n + 1) / 2 }
|
||||
for n := uint64(1); n < 42; n++ {
|
||||
if got := Triangle(n); expect(n) != got {
|
||||
t.Fatalf("Triangle(%v) = %v; expect %v", n, got, expect(n))
|
||||
}
|
||||
}
|
||||
}
|
||||
6
tests/fixedbugs/issue122/stub.go
Normal file
6
tests/fixedbugs/issue122/stub.go
Normal file
@@ -0,0 +1,6 @@
|
||||
// Code generated by command: go run asm.go -out issue122.s -stubs stub.go. DO NOT EDIT.
|
||||
|
||||
package issue122
|
||||
|
||||
// Triangle computes the nth triangle number.
|
||||
func Triangle(n uint64) uint64
|
||||
@@ -12,7 +12,8 @@ func main() {
|
||||
TEXT("Labels", NOSPLIT, "func() uint64")
|
||||
XORQ(RAX, RAX)
|
||||
INCQ(RAX)
|
||||
Label("neverused")
|
||||
Label("never_used")
|
||||
Label("consecutive_label_also_never_used")
|
||||
INCQ(RAX)
|
||||
INCQ(RAX)
|
||||
INCQ(RAX)
|
||||
|
||||
Reference in New Issue
Block a user