pass: allow consecutive labels (#123)

Fixes #122
This commit is contained in:
Michael McLoughlin
2020-01-19 22:06:52 -08:00
committed by GitHub
parent cde7e9483b
commit ff7a160610
8 changed files with 123 additions and 19 deletions

View 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()
}

View File

@@ -0,0 +1,2 @@
// Package issue122 tests consecutive labels.
package issue122

View 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

View 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))
}
}
}

View 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