committed by
GitHub
parent
cde7e9483b
commit
ff7a160610
22
pass/cfg.go
22
pass/cfg.go
@@ -11,26 +11,22 @@ import (
|
|||||||
// label name to the following instruction.
|
// label name to the following instruction.
|
||||||
func LabelTarget(fn *ir.Function) error {
|
func LabelTarget(fn *ir.Function) error {
|
||||||
target := map[ir.Label]*ir.Instruction{}
|
target := map[ir.Label]*ir.Instruction{}
|
||||||
var empty ir.Label
|
var pending []ir.Label
|
||||||
pending := empty
|
|
||||||
for _, node := range fn.Nodes {
|
for _, node := range fn.Nodes {
|
||||||
switch n := node.(type) {
|
switch n := node.(type) {
|
||||||
case ir.Label:
|
case ir.Label:
|
||||||
if pending != empty {
|
if _, found := target[n]; found {
|
||||||
return fmt.Errorf("expected instruction following label %q", pending)
|
return fmt.Errorf("duplicate label \"%s\"", n)
|
||||||
}
|
|
||||||
pending = n
|
|
||||||
if _, found := target[pending]; found {
|
|
||||||
return fmt.Errorf("duplicate label \"%s\"", pending)
|
|
||||||
}
|
}
|
||||||
|
pending = append(pending, n)
|
||||||
case *ir.Instruction:
|
case *ir.Instruction:
|
||||||
if pending != empty {
|
for _, label := range pending {
|
||||||
target[pending] = n
|
target[label] = n
|
||||||
pending = empty
|
}
|
||||||
|
pending = nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
if len(pending) != 0 {
|
||||||
if pending != empty {
|
|
||||||
return errors.New("function ends with label")
|
return errors.New("function ends with label")
|
||||||
}
|
}
|
||||||
fn.LabelTarget = target
|
fn.LabelTarget = target
|
||||||
|
|||||||
@@ -58,16 +58,25 @@ func TestLabelTargetEndsWithLabel(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLabelTargetInstructionFollowLabel(t *testing.T) {
|
func TestLabelTargetConsecutiveLabels(t *testing.T) {
|
||||||
f := ir.NewFunction("expectinstafterlabel")
|
i := &ir.Instruction{Opcode: "A"}
|
||||||
|
|
||||||
|
f := ir.NewFunction("consecutivelabels")
|
||||||
f.AddLabel(ir.Label("lblA"))
|
f.AddLabel(ir.Label("lblA"))
|
||||||
f.AddLabel(ir.Label("lblB"))
|
f.AddLabel(ir.Label("lblB"))
|
||||||
f.AddInstruction(&ir.Instruction{Opcode: "A"})
|
f.AddInstruction(i)
|
||||||
|
|
||||||
err := LabelTarget(f)
|
expect := map[ir.Label]*ir.Instruction{
|
||||||
|
"lblA": i,
|
||||||
|
"lblB": i,
|
||||||
|
}
|
||||||
|
|
||||||
if err == nil || err.Error() != "expected instruction following label \"lblA\"" {
|
if err := LabelTarget(f); err != nil {
|
||||||
t.Fatalf("expected error when label is not followed by instruction; got %v", err)
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(expect, f.LabelTarget) {
|
||||||
|
t.Fatalf("incorrect LabelTarget value\ngot=%#v\nexpext=%#v\n", f.LabelTarget, expect)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
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")
|
TEXT("Labels", NOSPLIT, "func() uint64")
|
||||||
XORQ(RAX, RAX)
|
XORQ(RAX, RAX)
|
||||||
INCQ(RAX)
|
INCQ(RAX)
|
||||||
Label("neverused")
|
Label("never_used")
|
||||||
|
Label("consecutive_label_also_never_used")
|
||||||
INCQ(RAX)
|
INCQ(RAX)
|
||||||
INCQ(RAX)
|
INCQ(RAX)
|
||||||
INCQ(RAX)
|
INCQ(RAX)
|
||||||
|
|||||||
Reference in New Issue
Block a user