Skip to content

Commit 5abb736

Browse files
committed
Added B instruction
1 parent 78bac4b commit 5abb736

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

instructions/b.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package instructions
2+
3+
import (
4+
"fmt"
5+
6+
"alon.kr/x/aarch64codegen/immediates"
7+
)
8+
9+
type Branch uint32
10+
11+
func B(offset immediates.Offset26Align4) Branch {
12+
return Branch(0b101<<26 | offset.Binary())
13+
}
14+
15+
func (i Branch) Offset() immediates.Offset26Align4 {
16+
return immediates.Offset26Align4(i & 0x3FFFFFF)
17+
}
18+
19+
func (i Branch) String() string {
20+
return fmt.Sprintf("B %s", i.Offset())
21+
}
22+
23+
func (i Branch) Binary() uint32 {
24+
return uint32(i)
25+
}

instructions/b_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package instructions_test
2+
3+
import (
4+
"testing"
5+
6+
"alon.kr/x/aarch64codegen/immediates"
7+
"alon.kr/x/aarch64codegen/instructions"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func AssertExpectedBranchInstruction(t *testing.T, expected string, offset int32) {
12+
off26, err := immediates.NewOffset26Align4(offset)
13+
assert.NoError(t, err)
14+
instruction := instructions.B(off26)
15+
AssertExpectedInstruction(t, expected, instruction)
16+
}
17+
18+
func TestBranch0(t *testing.T) {
19+
AssertExpectedBranchInstruction(t, "B #0", 0)
20+
}
21+
22+
func TestBranch1(t *testing.T) {
23+
AssertExpectedBranchInstruction(t, "B #4", 4)
24+
}
25+
26+
func TestBranchMinus1(t *testing.T) {
27+
AssertExpectedBranchInstruction(t, "B #-4", -4)
28+
}
29+
30+
func TestBranchMax(t *testing.T) {
31+
AssertExpectedBranchInstruction(t, "B #134217724", 0x7FFFFFC)
32+
}
33+
34+
func TestBranchMin(t *testing.T) {
35+
AssertExpectedBranchInstruction(t, "B #-134217728", -0x8000000)
36+
}

0 commit comments

Comments
 (0)