File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments