Skip to content

Commit

Permalink
tests/alloc/upper32: test for 32-bit operands
Browse files Browse the repository at this point in the history
Test to ensure that `avo` correctly accounts for zero-extension of
32-bit operands to 64 bits.

Updates #121
  • Loading branch information
mmcloughlin committed Jan 21, 2020
1 parent de139fc commit e4203cf
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 0 deletions.
57 changes: 57 additions & 0 deletions tests/alloc/upper32/asm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// +build ignore

package main

import (
"strconv"

. "github.com/mmcloughlin/avo/build"
. "github.com/mmcloughlin/avo/operand"
. "github.com/mmcloughlin/avo/reg"
)

// The goal is to test for correct handling of 32-bit operands in 64-bit mode,
// specifically that writes are zero-extended to 64 bits. This test is
// constructed such that the register allocator would fail if this feature is
// not accounted for. It consists of multiple copies of a 32-bit write followed
// by a 64-bit read of the same register. Without special treatment liveness
// analysis would consider the upper 32 bits to still be live prior to the
// write. Therefore if we stack up enough copies of this, we could cause the
// register allocator to fail.

func main() {
const (
n = 14 // number of registers
m = 3 // number of iterations
)

TEXT("Upper32", NOSPLIT, "func() uint64")
Doc("Upper32 computes the sum 1+2+...+" + strconv.Itoa(n*m) + ".")

Comment("Initialize sum.")
s := GP64()
XORQ(s, s)

k := 1
for i := 0; i < m; i++ {
Commentf("Iteration %d.", i+1)

// Allocate n 64-bit registers and write to their 32-bit aliases.
x := make([]GPVirtual, n)
for i := 0; i < n; i++ {
x[i] = GP64()
MOVL(U32(k), x[i].As32())
k++
}

// Sum them up.
for i := 0; i < n; i++ {
ADDQ(x[i], s)
}
}

Store(s, ReturnIndex(0))
RET()

Generate()
}
2 changes: 2 additions & 0 deletions tests/alloc/upper32/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package upper32 tests liveness analysis of 32-bit operations on 64-bit registers.
package upper32
6 changes: 6 additions & 0 deletions tests/alloc/upper32/stub.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

100 changes: 100 additions & 0 deletions tests/alloc/upper32/upper32.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Code generated by command: go run asm.go -out upper32.s -stubs stub.go. DO NOT EDIT.

#include "textflag.h"

// func Upper32() uint64
TEXT ·Upper32(SB), NOSPLIT, $0-8
// Initialize sum.
XORQ AX, AX

// Iteration 1.
MOVQ $0x00000001, CX
MOVQ $0x00000002, DX
MOVQ $0x00000003, BX
MOVQ $0x00000004, BP
MOVQ $0x00000005, SI
MOVQ $0x00000006, DI
MOVQ $0x00000007, R8
MOVQ $0x00000008, R9
MOVQ $0x00000009, R10
MOVQ $0x0000000a, R11
MOVQ $0x0000000b, R12
MOVQ $0x0000000c, R13
MOVQ $0x0000000d, R14
MOVQ $0x0000000e, R15
ADDQ CX, AX
ADDQ DX, AX
ADDQ BX, AX
ADDQ BP, AX
ADDQ SI, AX
ADDQ DI, AX
ADDQ R8, AX
ADDQ R9, AX
ADDQ R10, AX
ADDQ R11, AX
ADDQ R12, AX
ADDQ R13, AX
ADDQ R14, AX
ADDQ R15, AX

// Iteration 2.
MOVQ $0x0000000f, CX
MOVQ $0x00000010, DX
MOVQ $0x00000011, BX
MOVQ $0x00000012, BP
MOVQ $0x00000013, SI
MOVQ $0x00000014, DI
MOVQ $0x00000015, R8
MOVQ $0x00000016, R9
MOVQ $0x00000017, R10
MOVQ $0x00000018, R11
MOVQ $0x00000019, R12
MOVQ $0x0000001a, R13
MOVQ $0x0000001b, R14
MOVQ $0x0000001c, R15
ADDQ CX, AX
ADDQ DX, AX
ADDQ BX, AX
ADDQ BP, AX
ADDQ SI, AX
ADDQ DI, AX
ADDQ R8, AX
ADDQ R9, AX
ADDQ R10, AX
ADDQ R11, AX
ADDQ R12, AX
ADDQ R13, AX
ADDQ R14, AX
ADDQ R15, AX

// Iteration 3.
MOVQ $0x0000001d, CX
MOVQ $0x0000001e, DX
MOVQ $0x0000001f, BX
MOVQ $0x00000020, BP
MOVQ $0x00000021, SI
MOVQ $0x00000022, DI
MOVQ $0x00000023, R8
MOVQ $0x00000024, R9
MOVQ $0x00000025, R10
MOVQ $0x00000026, R11
MOVQ $0x00000027, R12
MOVQ $0x00000028, R13
MOVQ $0x00000029, R14
MOVQ $0x0000002a, R15
ADDQ CX, AX
ADDQ DX, AX
ADDQ BX, AX
ADDQ BP, AX
ADDQ SI, AX
ADDQ DI, AX
ADDQ R8, AX
ADDQ R9, AX
ADDQ R10, AX
ADDQ R11, AX
ADDQ R12, AX
ADDQ R13, AX
ADDQ R14, AX
ADDQ R15, AX
MOVQ AX, ret+0(FP)
RET
15 changes: 15 additions & 0 deletions tests/alloc/upper32/upper32_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package upper32

import (
"testing"
)

//go:generate go run asm.go -out upper32.s -stubs stub.go

func TestUpper32(t *testing.T) {
const n = 14 * 3
const expect = n * (n + 1) / 2
if got := Upper32(); got != expect {
t.Fatalf("Upper32() = %v; expect %v", got, expect)
}
}

0 comments on commit e4203cf

Please sign in to comment.